<?php
/**
 * Leave Types API for Auto-fetch
 * Returns active leave types for the current site
 */

require_once __DIR__ . '/../includes/init.php';
requireLogin();

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

try {
    $db = getDB();
    $siteId = getCurrentSiteId();
    
    if (!$siteId) {
        echo json_encode([
            'success' => false,
            'message' => 'No site selected',
            'leave_types' => []
        ]);
        exit;
    }
    
    $stmt = $db->prepare("
        SELECT leave_type_id, leave_type_code, leave_type_name, max_days_per_year
        FROM leave_types 
        WHERE site_id = ? AND leave_type_status = 'active'
        ORDER BY leave_type_name
    ");
    $stmt->execute([$siteId]);
    $leaveTypes = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo json_encode([
        'success' => true,
        'leave_types' => $leaveTypes,
        'count' => count($leaveTypes)
    ]);
    
} catch (Exception $e) {
    error_log("Error fetching leave types: " . $e->getMessage());
    echo json_encode([
        'success' => false,
        'message' => 'Error fetching leave types: ' . $e->getMessage(),
        'leave_types' => []
    ]);
}

