<?php

namespace App\Controllers;

use App\Models\State;

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

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

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

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

    public function store()
    {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $color = $_POST['color'] ?? '';
            $description = $_POST['description'] ?? '';
            $start_percentage = $_POST['start_percentage'] ?? 0;
            $end_percentage = $_POST['end_percentage'] ?? 100;
            
            if (trim($description) === '' || trim($color) === '' || !isset($start_percentage) || !isset($end_percentage)) {
                $_SESSION['message'] = "Error: Todos los campos son obligatorios.";
                $_SESSION['message_type'] = "danger";
                header("Location: " . BASE_HEADER . "states/create");
                exit;
            }
    
            $state = new State();
            $state->color = $color;
            $state->description = $description;
            $state->start_percentage = $start_percentage;
            $state->end_percentage = $end_percentage;
            $state->save();
    
            $_SESSION['message'] = "Estado de trámite creado exitosamente!";
            $_SESSION['message_type'] = "success";
            header("Location: " . BASE_HEADER . "states");
            exit;
        }
    }

    public function update($id)
    {
        if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['_method']) && $_POST['_method'] === 'PUT') {
            $state = State::find($id);
    
            if (!$state) {
                $_SESSION['message'] = "Error: Estado no encontrado.";
                $_SESSION['message_type'] = "danger";
                header("Location: " . BASE_HEADER . "states");
                exit;
            }
    
            $state->color = $_POST['color'] ?? '';
            $state->description = $_POST['description'] ?? '';
            $state->start_percentage = $_POST['start_percentage'] ?? 0;
            $state->end_percentage = $_POST['end_percentage'] ?? 100;
            $state->active = $_POST['active'] ?? 0;
    
            if (trim($state->description) === '' || trim($state->color) === '' || !isset($state->start_percentage) || !isset($state->end_percentage)) {
                $_SESSION['message'] = "Error: Todos los campos son obligatorios.";
                $_SESSION['message_type'] = "danger";
                header("Location: " . BASE_HEADER . "states/$id/edit");
                exit;
            }
    
            $state->save();
            $_SESSION['message'] = "Estado de trámite actualizado exitosamente!";
            $_SESSION['message_type'] = "success";
    
            header("Location: " . BASE_HEADER . "states");
            exit;
        }
        die("Error: Método no permitido.");
    }

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

            $state->delete($state->id);
            $_SESSION['message'] = "Estado de trámite eliminado exitosamente!";
            $_SESSION['message_type'] = "success";
            header("Location: " . BASE_HEADER . "states");
            exit;
        }
        die("Error: Método no permitido.");
    }
}