<?php
declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;
use App\Core\Response;
use App\Config\Database;

class StateController extends Controller
{
    /**
     * List all active states
     */
    public function index(): void
    {
        try {
            $db = Database::getInstance();
            $stmt = $db->prepare("SELECT id, name, code FROM states WHERE is_active = 1 ORDER BY name ASC");
            $stmt->execute();
            Response::success($stmt->fetchAll(), "States retrieved successfully");
        } catch (\Exception $e) {
            // Handle table not created yet gracefull (might be before migration)
            Response::error("Failed to load states: " . $e->getMessage(), [], 500);
        }
    }
}
