<?php

namespace App\Controllers;

use App\Models\PersonType;

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

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

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

    public function edit($id)
    {
        $personType = PersonType::find($id);
        if (!$personType) {
            $_SESSION['message'] = "Error: Tipo de persona no encontrado.";
            $_SESSION['message_type'] = "danger";
            header("Location: " . BASE_HEADER . "persontypes");
            exit;
        }
        $this->render('persontypes/persontypes-edit', ['personType' => $personType]);
    }

    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 . "persontypes/create");
                exit;
            }
    
            $personType = new PersonType();
            $personType->abbreviation = $abbreviation;
            $personType->description = $description;
            $personType->save();
    
            $_SESSION['message'] = "¡Tipo de persona creado exitosamente!";
            $_SESSION['message_type'] = "success";
            header("Location: " . BASE_HEADER . "persontypes");
            exit;
        }
    }

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

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

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