<?php
require_once 'config/database.php';
require_once 'utils/helpers.php';

try {
    header('Content-Type: application/json');

    $database = new Database();
    $db = $database->getConnection();

    $method = $_SERVER['REQUEST_METHOD'];

    // GET: Retrieve service types (public endpoint for dropdowns)
    if ($method === 'GET') {
        $query = "SELECT * FROM service_types ORDER BY name ASC";
        $stmt = $db->prepare($query);
        $stmt->execute();

        $serviceTypes = $stmt->fetchAll(PDO::FETCH_ASSOC);

        Helpers::sendResponse(true, 'Service types retrieved successfully', [
            'service_types' => $serviceTypes,
            'count' => count($serviceTypes)
        ]);
    } else {
        Helpers::sendResponse(false, 'Method not allowed', null, 405);
    }
} catch (PDOException $e) {
    Helpers::sendResponse(false, 'Database error: ' . $e->getMessage(), null, 500);
} catch (Exception $e) {
    Helpers::sendResponse(false, 'Error: ' . $e->getMessage(), null, 500);
}
