<?php
require_once './shikanisha.kts.php';

session_start();
if (!isset($_SESSION['employee_id'])) {
    echo json_encode(['success' => false, 'message' => 'Not authorized']);
    exit();
}

$employee_id = $_SESSION['employee_id'];
$starting_cash = isset($_POST['starting_cash']) ? floatval($_POST['starting_cash']) : 0;

// Check if there's already an active shift for this employee
$stmt = $conn->prepare("SELECT * FROM shifts WHERE employee_id = ? AND status = 'active'");
$stmt->bind_param("i", $employee_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) {
    echo json_encode(['success' => false, 'message' => 'You already have an active shift']);
    exit();
}

// Start new shift
$stmt = $conn->prepare("INSERT INTO shifts (employee_id, start_time, starting_cash, status) VALUES (?, NOW(), ?, 'active')");
$stmt->bind_param("id", $employee_id, $starting_cash);

if ($stmt->execute()) {
    echo json_encode(['success' => true, 'message' => 'Shift started successfully']);
} else {
    echo json_encode(['success' => false, 'message' => 'Error starting shift']);
}

$stmt->close();
$conn->close();
?>