<?php

namespace App\Controllers;

use App\Models\Group;
use App\Models\Subgroup;
use App\Services\GroupService;

class SubgroupController extends BaseController
{
    protected GroupService $groupService;

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

        $this->groupService = new GroupService();
    }

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

    public function show($id)
    {
        $subgroup = Subgroup::find($id);
        $this->render('subgroups/show', ['subgroup' => $subgroup]);
    }

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

    public function edit($id)
    {
        $subgroup = Subgroup::find($id);
        $area = $subgroup->getArea();
        if (!$subgroup) {
            $_SESSION['message'] = "Error: Subgrupo no encontrado.";
            $_SESSION['message_type'] = "danger";
            header("Location: " . BASE_HEADER . "areas/$area->id/show-groups");
            exit;
        }
        $this->render('subgroups/subgroups-edit', ['subgroup' => $subgroup, 'area' => $area]);
    }

    public function store()
    {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $group_id = $_POST['group_id'] ?? null;
            $abbreviation = $_POST['subgroup_abbreviation'] ?? null;
            $description = $_POST['subgroup_name'] ?? null;

            $group = Group::find($group_id);
            $area = $this->groupService->getAreaByGroup($group);
    
            if (empty($abbreviation) || empty($description)) {
                $_SESSION['message'] = "Error: Todos los campos son obligatorios.";
                $_SESSION['message_type'] = "danger";
                header("Location: " . BASE_HEADER . "areas/$area->id/show-groups");
                exit;
            }
    
            $subgroup = new Subgroup();
            $subgroup->group_id = $group_id;
            $subgroup->abbreviation = htmlspecialchars($abbreviation);
            $subgroup->description = htmlspecialchars($description);
            $subgroup->save();
    
            $_SESSION['message'] = "¡Subgrupo creado exitosamente!";
            $_SESSION['message_type'] = "success";
            header("Location: " . BASE_HEADER . "areas/$area->id/show-groups");
            exit;
        }
    }

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

    public function delete($id)
    {
        if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['_method']) && $_POST['_method'] === 'DELETE') {
            $subgroup = Subgroup::find($id);
            $area = $subgroup->getArea();
            
            if (!$subgroup) {
                $_SESSION['message'] = "Error: No se pudo eliminar el subgrupo.";
                $_SESSION['message_type'] = "danger";
                header("Location: " . BASE_HEADER . "areas/$area->id/show-groups");
                exit;
            }

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