<?php
include("../../../include/startup.php"); 
if (isset($_REQUEST['action'])) {
    while (ob_get_level()) {
        ob_end_clean(); 
    }
    header('Content-Type: application/json');
    header('Cache-Control: no-cache, must-revalidate');
}


$action = $_REQUEST['action'] ?? '';

if ($action == "check_clock_status") {

    $clock_no = $_POST['clock_no'];

    $sql = "SELECT id FROM admin WHERE clock_no='$clock_no'";
    $res = $conn->query($sql);

    if ($res->num_rows == 0) {
        echo json_encode(["status"=>"invalid"]);
        exit;
    }

    $user = $res->fetch_assoc();
    $user_id = $user['id'];

    $sql = "SELECT * FROM shifts WHERE user_id=$user_id AND status='active'";
    $res = $conn->query($sql);

    if ($res->num_rows == 0) {

        echo json_encode([
            "status"=>"no_shift"
        ]);
        exit;
    }

    $shift = $res->fetch_assoc();
    $shift_id = $shift['id'];

    $sql = "SELECT * FROM transactions 
            WHERE shift_id=$shift_id 
            AND status='active' 
            AND pause_end IS NULL";

    $res = $conn->query($sql);

    if ($res->num_rows > 0) {

        echo json_encode([
            "status"=>"paused",
            "shift_id"=>$shift_id
        ]);

    } else {

        echo json_encode([
            "status"=>"active",
            "shift_id"=>$shift_id
        ]);
    }

    exit;
}


if ($action == "clock_action") {

    $type = $_POST['type'];
    $clock_no = $_POST['clock_no'];
    $current_date = date("Y-m-d H:i:s");

    $sql = "SELECT id FROM admin WHERE clock_no='$clock_no'";
    $res = $conn->query($sql);

    if ($res->num_rows == 0) {
        echo json_encode(["status"=>"error","message"=>"Invalid Clock Code"]);
        exit;
    }

    $user = $res->fetch_assoc();
    $user_id = $user['id'];

    if ($type == "clock_in") {

        $sql = "INSERT INTO shifts(user_id,start_time,status)
                VALUES($user_id,'$current_date','active')";

        $conn->query($sql);

        echo json_encode([
            "status"=>"success",
            "message"=>"Shift started"
        ]);
    }


    if ($type == "pause") {

        $shift = $conn->query("SELECT id FROM shifts WHERE user_id=$user_id AND status='active'")->fetch_assoc();
        $shift_id = $shift['id'];

        $conn->query("INSERT INTO transactions(shift_id,pause_start,status)
                      VALUES($shift_id,'$current_date','active')");

        echo json_encode([
            "status"=>"success",
            "message"=>"Shift paused"
        ]);
    }


    if ($type == "resume") {

        $shift = $conn->query("SELECT id FROM shifts WHERE user_id=$user_id AND status='active'")->fetch_assoc();
        $shift_id = $shift['id'];

        $conn->query("UPDATE transactions 
                      SET pause_end='$current_date',status='inactive'
                      WHERE shift_id=$shift_id AND status='active'");

        echo json_encode([
            "status"=>"success",
            "message"=>"Shift resumed"
        ]);
    }


    if ($type == "end_shift") {

        $shift = $conn->query("SELECT * FROM shifts WHERE user_id=$user_id AND status='active'")->fetch_assoc();
        $shift_id = $shift['id'];

        $conn->query("UPDATE transactions 
                      SET pause_end='$current_date',status='inactive'
                      WHERE shift_id=$shift_id AND status='active'");

        $conn->query("UPDATE shifts 
                      SET end_time='$current_date',status='inactive'
                      WHERE id=$shift_id");

        echo json_encode([
            "status"=>"success",
            "message"=>"Shift ended"
        ]);
    }

}
?>