<?php

namespace App\Controllers;

use App\Models\ResponseType;

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

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

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

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

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

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

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

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