<?php

namespace App\Models;

use PDO;
use Config\Database;

class Setting extends BaseModel
{
    protected static $table = 'settings';

    public $id;
    public $value;
    public $description;
    public $created_at;
    public $updated_at;
    public $active;
    public $deleted_at;

    public function __construct($data = [])
    {
        parent::__construct($data);
        $this->active = $data['active'] ?? 1;
    }

    public function save()
    {
        $db = Database::getConnection();
        if ($this->id) {
            $query = "UPDATE settings SET value = :value, description = :description, active = :active WHERE id = :id";
            $stmt = $db->prepare($query);
            $stmt->execute([
                ':id' => $this->id,
                ':value' => $this->value,
                ':description' => $this->description,
                ':active' => $this->active
            ]);
        } else {
            $query = "INSERT INTO settings (value, description, active) VALUES (:value, :description, :active)";
            $stmt = $db->prepare($query);
            $stmt->execute([
                ':value' => $this->value,
                ':description' => $this->description,
                ':active' => $this->active,
            ]);
            $this->id = $db->lastInsertId();
        }
    }

    public static function getYearName()
    {
        $setting = self::findBy('value', 'NOMBRE DEL AÑO');
        
        if($setting)
        {
            return $setting->description;
        }
        return null;
    }

    

}