<?php

class Season {

    private $id ;

    private $name ;

    private $dateSeasonStart ;

    private $dateSeasonEnd ;

    //Load from ID

    public function loadFromID($id)
    {
        global $wpdb;

        $sql = "
            SELECT IDSeason, name, dateSeasonStart, dateSeasonEnd FROM cass_season WHERE IDSeason = %d;
        ";
        $result = $wpdb->get_row($wpdb->prepare($sql, $id));

        if ($result) {
            $this->setId($result->IDSeason);
            $this->setName($result->name);
            $this->setSeasonStart($result->dateSeasonStart);
            $this->setSeasonEnd($result->dateSeasonEnd);
            return true;
        } else {
            return false;
        }
    }

    public function getCurrentSeason()
    {
        global $wpdb;

        // Obtenez la date du jour au format SQL
        $currentDate = date('Y-m-d');

        // Recherchez la saison en cours en fonction de la date actuelle
        $sql = "SELECT IDSeason FROM cass_season WHERE dateSeasonStart <= %s AND dateSeasonEnd >= %s";
        $currentSeason = $wpdb->get_var($wpdb->prepare($sql, $currentDate, $currentDate));

        return $currentSeason;
    }

    public static function selectAll() {
        global $wpdb;

        $sql = "
        SELECT IDSeason, name, dateSeasonStart, dateSeasonEnd FROM cass_season ORDER BY dateSeasonStart ASC;
        ";

        return $wpdb->get_results($sql);
    }

    //Update
    public function update() {
        global $wpdb;

        $sql = "
            UPDATE cass_season
            SET name = %s, dateSeasonStart = %s, dateSeasonEnd = %s
            WHERE IDSeason = %d;
         ";
    
        $wpdb->query($wpdb->prepare($sql, $this->getName(), $this->getSeasonStart(), $this->getSeasonEnd(),$this->getId()));
    }

    //Delete
    public function delete() {
        global $wpdb;

        $sql = "
            DELETE FROM cass_season
            WHERE IDSeason = %d;
         ";
        $wpdb->query($wpdb->prepare($sql, $this->getId()));
    }

    //Insert
    public function create() {
        global $wpdb;

        $sql = "
        INSERT INTO cass_season(name, dateSeasonStart, dateSeasonEnd)
        VALUES(%s,%s,%s);
         ";
        $wpdb->query($wpdb->prepare($sql, $this->getName(), $this->getSeasonStart(), $this->getSeasonEnd()));
    }

    public function sqlWPTable() {
        global $wpdb;

        $sql = "
            SELECT IDSeason, name, dateSeasonStart, dateSeasonEnd from cass_season ORDER BY dateSeasonStart ASC;
             ";

        return $wpdb->get_results(
            $sql,
            ARRAY_A
        );
    }

    // Getter and setters
    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function getSeasonStart() {
        return $this->dateSeasonStart;
    }

    public function getSeasonEnd() {
        return $this->dateSeasonEnd;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function setSeasonStart($dateSeasonStart) {
        $this->dateSeasonStart = $dateSeasonStart;
    }

    public function setSeasonEnd($dateSeasonEnd) {
        $this->dateSeasonEnd = $dateSeasonEnd;
    }

}