<?php 

class Event{

    private $name;
    private $date;
    private $description;

    public function __construct($name,$date,$description="")
    {
    $this->name = $name;
    $this->date = $date;
    $this->description = $description;
    }


    public static function  getEventsFromMonth(string $startDate): array {
        $maxResults = 50;
        $apiKey = 'AIzaSyCPIPX6BGSLhv6CP_XZIkn8KgWFyqae0Ew';
        $calendarId = '231de13b1940c22c2b94b00de2a45ac5a8f69e0ffeb330327668ba5d1472c34a@group.calendar.google.com';
        // Convertit la date de début en format ISO 8601
        $start = new DateTime($startDate);
        $start->modify('first day of this month 00:00:00');
        $end = clone $start;
        $end->modify('first day of next month 00:00:00');

        // Formate les dates pour l’API
        $timeMin = urlencode($start->format(DateTime::ATOM)); // ex: 2025-07-01T00:00:00+00:00
        $timeMax = urlencode($end->format(DateTime::ATOM));   // ex: 2025-08-01T00:00:00+00:00

        $calendarIdEncoded = urlencode($calendarId);
        $url = "https://www.googleapis.com/calendar/v3/calendars/$calendarIdEncoded/events"
            . "?key=$apiKey"
            . "&orderBy=startTime"
            . "&singleEvents=true"
            . "&maxResults=$maxResults"
            . "&timeMin=$timeMin"
            . "&timeMax=$timeMax";

        // Requête
        $response = @file_get_contents($url);
        if ($response === false) {
            return ["error" => "Échec de la récupération des événements."];
        }

        $data = json_decode($response, true);
        //return $data['items'] ?? [];
//var_dump($url);
        //var_dump($results);
        foreach($data['items'] as $e){
            
            $start = $e['start']['dateTime'];
            if (empty($start)) {
                $start = $e['start']['dateTime'];
            }
            $events[]= new event($e['summary'],$start,$e['summary']);
        }   
        //var_dump($events);
        return $events;
    }

    public function setname($name) {
        $this->name = $name;
    }
    public function setdate($date) {
        $this->date = $date;
    }
    public function setdescription($description) {
        $this->description = $description;
    }

    public function getname() {
        return $this->name;
    }
    public function getdate() {
        return $this->date;
    }
    public function getdescription() {
        return $this->description;
    }




    
}

?>