<?php

namespace App\Controllers;

use App\Models\Modality;

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

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

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

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

    public function store()
    {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $abbreviation = $_POST['abbreviation'] ?? '';
            $description = $_POST['description'] ?? '';
    
            if (empty($abbreviation) || empty($description)) {
                $_SESSION['message'] = "Error: Todos los campos son obligatorios.";
                $_SESSION['message_type'] = "danger";
                header("Location: " . BASE_HEADER . "modalities/create");
                exit;
            }
    
            $modality = new Modality();
            $modality->abbreviation = $abbreviation;
            $modality->description = $description;
            $modality->save();
    
            $_SESSION['message'] = "¡Modalidad creada exitosamente!";
            $_SESSION['message_type'] = "success";
            header("Location: " . BASE_HEADER . "modalities");
            exit;
        }
    }

    public function update($id)
    {
        if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['_method']) && $_POST['_method'] === 'PUT') {
            $modality = Modality::find($id);
    
            if (!$modality) {
                $_SESSION['message'] = "Error: Modalidad no encontrada.";
                $_SESSION['message_type'] = "danger";
                header("Location: " . BASE_HEADER . "modalities");
                exit;
            }
    
            $modality->abbreviation = $_POST['abbreviation'] ?? '';
            $modality->description = $_POST['description'] ?? '';
            $modality->active = $_POST['active'] ?? 0;
    
            if (empty($modality->abbreviation) || empty($modality->description)) {
                $_SESSION['message'] = "Error: Todos los campos son obligatorios.";
                $_SESSION['message_type'] = "danger";
                header("Location: " . BASE_HEADER . "modalities/$id/edit");
                exit;
            }
    
            $modality->save();
            $_SESSION['message'] = "¡Modalidad actualizada exitosamente!";
            $_SESSION['message_type'] = "success";
    
            header("Location: " . BASE_HEADER . "modalities");
            exit;
        }
        die("Error: Método no permitido.");
    }

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

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