<?php

namespace App\Controllers;

use App\Models\Holiday;

class HolidayController extends BaseController
{
    public function __construct() {
        if (session_status() === PHP_SESSION_NONE) {
            session_start();
        }
    }

    public function index()
    {
        $holidays = Holiday::getActives();
        $this->render('holidays/holidays-index', ['holidays' => $holidays]);
    }

    public function create()
    {
        $this->render('holidays/holidays-create'); 
    }

    public function edit($id)
    {
        $holiday = Holiday::find($id);
        if (!$holiday) {
            $_SESSION['message'] = "Error: Feriado no encontrado.";
            $_SESSION['message_type'] = "danger";
            header("Location: " . BASE_HEADER . "holidays");
            exit;
        }
        $this->render('holidays/holidays-edit', ['holiday' => $holiday]);
    }

    public function store()
    {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $description = trim($_POST['description'] ?? '');
            $day = (int) ($_POST['holiday_day'] ?? 0);
            $month = (int) ($_POST['holiday_month'] ?? 0);
            $year = isset($_POST['year']) && $_POST['year'] !== '' ? (int) $_POST['year'] : null;

            if (empty($description) || $day < 1 || $day > 31 || $month < 1 || $month > 12) {
                $_SESSION['message'] = "Error: Todos los campos obligatorios deben estar completos y ser válidos.";
                $_SESSION['message_type'] = "danger";
                header("Location: " . BASE_HEADER . "holidays/create");
                exit;
            }

            $holiday = new Holiday();
            $holiday->holiday_day = $day;
            $holiday->holiday_month = $month;
            $holiday->description = $description;
            $holiday->specific_year = $year;
            $holiday->save();

            $_SESSION['message'] = "¡Feriado creado exitosamente!";
            $_SESSION['message_type'] = "success";
            header("Location: " . BASE_HEADER . "holidays");
            exit;
        }
    }


    public function update($id)
    {
        if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['_method']) && $_POST['_method'] === 'PUT') {
            $holiday = Holiday::find($id);

            if (!$holiday) {
                $_SESSION['message'] = "Error: Feriado no encontrado.";
                $_SESSION['message_type'] = "danger";
                header("Location: " . BASE_HEADER . "holidays");
                exit;
            }

            $description = trim($_POST['description'] ?? '');
            $holiday_day = (int)($_POST['holiday_day'] ?? 0);
            $holiday_month = (int)($_POST['holiday_month'] ?? 0);
            $year = isset($_POST['year']) && $_POST['year'] !== '' ? (int)$_POST['year'] : null;
            $active = isset($_POST['active']) ? (int)$_POST['active'] : 0;

            if (empty($description) || $holiday_day < 1 || $holiday_day > 31 || $holiday_month < 1 || $holiday_month > 12) {
                $_SESSION['message'] = "Error: Complete correctamente todos los campos requeridos.";
                $_SESSION['message_type'] = "danger";
                header("Location: " . BASE_HEADER . "holidays/$id/edit");
                exit;
            }

            $holiday->description = $description;
            $holiday->holiday_day = $holiday_day;
            $holiday->holiday_month = $holiday_month;
            $holiday->specific_year = $year;
            $holiday->active = $active;

            $holiday->save();

            $_SESSION['message'] = "¡Feriado actualizado exitosamente!";
            $_SESSION['message_type'] = "success";
            header("Location: " . BASE_HEADER . "holidays");
            exit;
        }
    }


    public function delete($id)
    {
        if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['_method']) && $_POST['_method'] === 'DELETE') {
            $holiday = Holiday::find($id);
            
            if (!$holiday) {
                $_SESSION['message'] = "Error: No se pudo eliminar el feriado.";
                $_SESSION['message_type'] = "danger";
                header("Location: " . BASE_HEADER . "holidays");
                exit;
            }

            $holiday->delete($holiday->id);
            $_SESSION['message'] = "¡Feriado eliminado exitosamente!";
            $_SESSION['message_type'] = "success";
            header("Location: " . BASE_HEADER . "holidays");
            exit;
        }
        die("Error: Método no permitido.");
    }
}