<?php
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/functions.php';

try {
    // 1. Create a dummy company for testing
    query("INSERT INTO companies (name, email, phone, address) VALUES ('Test Overtime Company', 'test@example.com', '123456', '123 Test St')");
    $company_id = lastInsertId();
    echo "Created dummy company ID: $company_id\n";

    // 2. Create shifts with different working hours
    // Shift A: 9 Hours (9:00 to 18:00)
    query("INSERT INTO shifts (company_id, name, start_time, end_time, working_hours) VALUES (?, '9h Shift', '09:00:00', '18:00:00', 9.00)", [$company_id]);
    $shift_9h_id = lastInsertId();
    
    // Shift B: 7.5 Hours (08:30 to 16:00)
    query("INSERT INTO shifts (company_id, name, start_time, end_time, working_hours) VALUES (?, '7.5h Shift', '08:30:00', '16:00:00', 7.50)", [$company_id]);
    $shift_7_5h_id = lastInsertId();

    echo "Created Shifts: 9h (ID: $shift_9h_id), 7.5h (ID: $shift_7_5h_id)\n";

    // Fetch a valid role ID
    $role_id = fetchValue("SELECT id FROM roles LIMIT 1") ?: 1;

    // 3. Create employees
    // Employee 1: Tagged to 9h shift
    query("INSERT INTO employees (company_id, name, employee_code, nic, shift_id, role_id) VALUES (?, 'Employee Nine', 'EMP999', '111111111V', ?, ?)", [$company_id, $shift_9h_id, $role_id]);
    $emp_9h_id = lastInsertId();

    // Employee 2: Tagged to 7.5h shift
    query("INSERT INTO employees (company_id, name, employee_code, nic, shift_id, role_id) VALUES (?, 'Employee Seven', 'EMP777', '222222222V', ?, ?)", [$company_id, $shift_7_5h_id, $role_id]);
    $emp_7_5h_id = lastInsertId();

    // Employee 3: Tagged to no shift (should fallback to 8h standard)
    query("INSERT INTO employees (company_id, name, employee_code, nic, shift_id, role_id) VALUES (?, 'Employee Default', 'EMP888', '333333333V', NULL, ?)", [$company_id, $role_id]);
    $emp_def_id = lastInsertId();

    echo "Created Employees: Nine (ID: $emp_9h_id), Seven (ID: $emp_7_5h_id), Default (ID: $emp_def_id)\n";

    $test_date = '2026-05-20';

    // Scenario 1: Clock in for 10 hours (e.g. 08:00 to 18:00)
    // For Employee Nine (9h shift): Overtime should be 10 - 9 = 1 hour
    // For Employee Seven (7.5h shift): Overtime should be 10 - 7.5 = 2.5 hours
    // For Employee Default (no shift -> fallback to 8h): Overtime should be 10 - 8 = 2 hours

    echo "\n--- Scenario 1: 10 Hours Worked ---\n";
    $clock_in = "$test_date 08:00:00";
    $clock_out = "$test_date 18:00:00";

    // Test Employee Nine
    query("INSERT INTO attendance (employee_id, company_id, date, clock_in, clock_out, status) VALUES (?, ?, ?, ?, ?, 'present')", [$emp_9h_id, $company_id, $test_date, $clock_in, $clock_out]);
    processDailyOvertime($emp_9h_id, $company_id, $test_date);
    $ot = fetch("SELECT hours FROM overtime_requests WHERE employee_id = ? AND date = ?", [$emp_9h_id, $test_date]);
    echo "Employee Nine Overtime: " . ($ot ? $ot['hours'] . " Hrs" : "None") . " (Expected: 1.00 Hrs)\n";

    // Test Employee Seven
    query("INSERT INTO attendance (employee_id, company_id, date, clock_in, clock_out, status) VALUES (?, ?, ?, ?, ?, 'present')", [$emp_7_5h_id, $company_id, $test_date, $clock_in, $clock_out]);
    processDailyOvertime($emp_7_5h_id, $company_id, $test_date);
    $ot = fetch("SELECT hours FROM overtime_requests WHERE employee_id = ? AND date = ?", [$emp_7_5h_id, $test_date]);
    echo "Employee Seven Overtime: " . ($ot ? $ot['hours'] . " Hrs" : "None") . " (Expected: 2.50  Hrs)\n";

    // Test Employee Default
    query("INSERT INTO attendance (employee_id, company_id, date, clock_in, clock_out, status) VALUES (?, ?, ?, ?, ?, 'present')", [$emp_def_id, $company_id, $test_date, $clock_in, $clock_out]);
    processDailyOvertime($emp_def_id, $company_id, $test_date);
    $ot = fetch("SELECT hours FROM overtime_requests WHERE employee_id = ? AND date = ?", [$emp_def_id, $test_date]);
    echo "Employee Default Overtime: " . ($ot ? $ot['hours'] . " Hrs" : "None") . " (Expected: 2.00 Hrs)\n";


    // Scenario 2: Clock in for 8.5 hours (e.g. 08:00 to 16:30)
    // For Employee Nine (9h shift): Overtime should be deleted/not created
    // For Employee Seven (7.5h shift): Overtime should be 8.5 - 7.5 = 1 hour
    // For Employee Default (no shift -> fallback to 8h): Overtime should be 8.5 - 8 = 0.5 hours

    echo "\n--- Scenario 2: 8.5 Hours Worked (Update/Delete check) ---\n";
    $clock_out_2 = "$test_date 16:30:00";

    // Clean up old attendance and requests first, then insert new ones
    query("DELETE FROM overtime_requests WHERE company_id = ?", [$company_id]);
    query("DELETE FROM attendance WHERE company_id = ?", [$company_id]);

    // Test Employee Nine (8.5h is below 9h, no overtime)
    query("INSERT INTO attendance (employee_id, company_id, date, clock_in, clock_out, status) VALUES (?, ?, ?, ?, ?, 'present')", [$emp_9h_id, $company_id, $test_date, $clock_in, $clock_out_2]);
    processDailyOvertime($emp_9h_id, $company_id, $test_date);
    $ot = fetch("SELECT hours FROM overtime_requests WHERE employee_id = ? AND date = ?", [$emp_9h_id, $test_date]);
    echo "Employee Nine Overtime: " . ($ot ? $ot['hours'] . " Hrs" : "None") . " (Expected: None)\n";

    // Test Employee Seven (8.5h is above 7.5h, 1h overtime)
    query("INSERT INTO attendance (employee_id, company_id, date, clock_in, clock_out, status) VALUES (?, ?, ?, ?, ?, 'present')", [$emp_7_5h_id, $company_id, $test_date, $clock_in, $clock_out_2]);
    processDailyOvertime($emp_7_5h_id, $company_id, $test_date);
    $ot = fetch("SELECT hours FROM overtime_requests WHERE employee_id = ? AND date = ?", [$emp_7_5h_id, $test_date]);
    echo "Employee Seven Overtime: " . ($ot ? $ot['hours'] . " Hrs" : "None") . " (Expected: 1.00 Hrs)\n";

    // Test Employee Default (8.5h is above 8h, 0.5h overtime)
    query("INSERT INTO attendance (employee_id, company_id, date, clock_in, clock_out, status) VALUES (?, ?, ?, ?, ?, 'present')", [$emp_def_id, $company_id, $test_date, $clock_in, $clock_out_2]);
    processDailyOvertime($emp_def_id, $company_id, $test_date);
    $ot = fetch("SELECT hours FROM overtime_requests WHERE employee_id = ? AND date = ?", [$emp_def_id, $test_date]);
    echo "Employee Default Overtime: " . ($ot ? $ot['hours'] . " Hrs" : "None") . " (Expected: 0.50 Hrs)\n";

    // 4. Cleanup dummy data
    echo "\nCleaning up dummy data...\n";
    query("DELETE FROM overtime_requests WHERE company_id = ?", [$company_id]);
    query("DELETE FROM attendance WHERE company_id = ?", [$company_id]);
    query("DELETE FROM employees WHERE company_id = ?", [$company_id]);
    query("DELETE FROM shifts WHERE company_id = ?", [$company_id]);
    query("DELETE FROM companies WHERE id = ?", [$company_id]);
    echo "Cleanup complete.\n";

} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
