<?php
/**
 * Get Complete Employee Details API
 * Returns employee information including salary structure
 */

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

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

$employeeId = $_GET['employee_id'] ?? null;
$siteId = getCurrentSiteId();

if (!$employeeId) {
    echo json_encode(['success' => false, 'message' => 'Employee ID required']);
    exit;
}

$db = getDB();

try {
    // Fetch employee details with salary structure
    $stmt = $db->prepare("
        SELECT 
            e.employee_id,
            e.employee_code,
            CONCAT(e.first_name, ' ', IFNULL(e.middle_name, ''), ' ', e.last_name) as full_name,
            e.first_name,
            e.middle_name,
            e.last_name,
            e.employment_status,
            e.employment_type,
            e.phone,
            e.email,
            e.salary as basic_salary,
            d.designation_name,
            dept.department_name
        FROM employees e
        LEFT JOIN designations d ON e.designation_id = d.designation_id
        LEFT JOIN departments dept ON e.department_id = dept.department_id
        WHERE e.employee_id = ? AND e.site_id = ?
    ");
    
    $stmt->execute([$employeeId, $siteId]);
    $employee = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$employee) {
        echo json_encode([
            'success' => false,
            'message' => 'Employee not found'
        ]);
        exit;
    }
    
    // Try to get salary structure from employee_master_sheet if exists
    try {
        $salaryStmt = $db->prepare("
            SELECT 
                basic_salary,
                hra,
                vda,
                special_allowance,
                other_allowance,
                pf_percentage,
                esi_percentage
            FROM employee_master_sheet
            WHERE employee_id = ?
        ");
        $salaryStmt->execute([$employeeId]);
        $salaryData = $salaryStmt->fetch(PDO::FETCH_ASSOC);
        
        if ($salaryData) {
            $employee = array_merge($employee, $salaryData);
        }
    } catch (Exception $e) {
        // Table might not exist, use defaults
    }
    
    // Calculate defaults if not found
    $basic = $employee['basic_salary'] ?? 15000;
    $vda = $employee['vda'] ?? 0;
    $special_allowance = $employee['special_allowance'] ?? 0;
    $other_allowance = $employee['other_allowance'] ?? 3000;
    $hra = $employee['hra'] ?? 0;
    
    // Calculate PF and ESI based on basic
    $pf = round($basic * 0.12, 2);
    $esi = round($basic * 0.0325, 2);
    $pt = 125; // Professional Tax
    
    // Calculate totals
    $total_earning = $basic + $vda + $special_allowance + $other_allowance + $hra;
    
    // Provident Fund (12% on basic)
    $provident_fund = round($basic * 0.12, 2);
    $esic = round($basic * 0.0325, 2);
    
    echo json_encode([
        'success' => true,
        'employee' => [
            'employee_id' => $employee['employee_id'],
            'employee_code' => $employee['employee_code'],
            'full_name' => $employee['full_name'],
            'designation' => $employee['designation_name'] ?? '',
            'department' => $employee['department_name'] ?? '',
            'employment_type' => $employee['employment_type'] ?? 'full_time',
            'employment_status' => $employee['employment_status'],
            'phone' => $employee['phone'] ?? '',
            'email' => $employee['email'] ?? ''
        ],
        'salary_structure' => [
            'basic' => $basic,
            'vda' => $vda,
            'special_allowance' => $special_allowance,
            'other_allowance' => $other_allowance,
            'hra' => $hra,
            'total_earning' => $total_earning,
            'pf' => $pf,
            'esi' => $esi,
            'pt' => $pt,
            'provident_fund' => $provident_fund,
            'esic' => $esic
        ]
    ]);
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage()
    ]);
}
