<?php

namespace App\Controllers;

use App\Models\Area;
use App\Models\GroupType;
use App\Services\AreaService;

class AreaController extends BaseController
{
    protected AreaService $areaService;

    public function __construct() {
        if (session_status() === PHP_SESSION_NONE) {
            session_start();
        }

        $this->areaService = new AreaService();
    }

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

    public function show($id)
    {
        $area = Area::find($id);
        $this->render('areas/show', ['area' => $area]);
    }

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

    public function edit($id)
    {
        $area = Area::find($id);
        if (!$area) {
            $_SESSION['message'] = "Error: Área no encontrada.";
            $_SESSION['message_type'] = "danger";
            header("Location: " . BASE_HEADER . "areas");
            exit;
        }
        $this->render('areas/areas-edit', ['area' => $area]);
    }

    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 . "areas/create");
                exit;
            }
    
            $area = new Area();
            $area->abbreviation = $abbreviation;
            $area->description = $description;
            $area->save();
    
            $_SESSION['message'] = "¡Área creada exitosamente!";
            $_SESSION['message_type'] = "success";
            header("Location: " . BASE_HEADER . "areas");
            exit;
        }
    }

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

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

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

    public function groups($id)
    {
        $area = $this->areaService->getWithRelations($id);

        if (!$area) {
            $_SESSION['message'] = "Error: Área no encontrada.";
            $_SESSION['message_type'] = "danger";
            header("Location: " . BASE_HEADER . "areas");
            exit;
        }

        $groupTypes = GroupType::getActives();

        $this->render('areas/areas-groups', [
            'area' => $area,
            'availableGroupTypes' => $groupTypes
        ]);
    }

}
