<?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'];
$action = isset($_GET['action']) ? $_GET['action'] : '';

if ($action == 'check_status') {
    // Check if there's an active shift
    $stmt = $conn->prepare("
        SELECT s.*, e.name as employee_name 
        FROM shifts s
        JOIN employees e ON s.employee_id = e.employee_id
        WHERE s.employee_id = ? AND s.status = 'active'
    ");
    $stmt->bind_param("i", $employee_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows > 0) {
        $shift = $result->fetch_assoc();
        echo json_encode([
            'active_shift' => true,
            'shift_id' => $shift['shift_id'],
            'start_time' => $shift['start_time'],
            'employee_name' => $shift['employee_name'],
            'starting_cash' => number_format($shift['starting_cash'], 2)
        ]);
    } else {
        echo json_encode(['active_shift' => false]);
    }
    
    $stmt->close();
} elseif ($action == 'get_report' && isset($_GET['shift_id'])) {
    $shift_id = intval($_GET['shift_id']);
    
    // Get shift info
    $stmt = $conn->prepare("
        SELECT s.*, e.name as employee_name 
        FROM shifts s
        JOIN employees e ON s.employee_id = e.employee_id
        WHERE s.shift_id = ?
    ");
    $stmt->bind_param("i", $shift_id);
    $stmt->execute();
    $shift = $stmt->get_result()->fetch_assoc();
    
    if (!$shift) {
        echo json_encode(['success' => false, 'message' => 'Shift not found']);
        exit();
    }
    
    // Get payment methods summary
    $stmt = $conn->prepare("
        SELECT payment_method, SUM(total_amount) as total_amount, SUM(transaction_count) as transaction_count
        FROM shift_summaries
        WHERE shift_id = ?
        GROUP BY payment_method
    ");
    $stmt->bind_param("i", $shift_id);
    $stmt->execute();
    $payment_methods = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
    
    // Calculate totals
    $totals = [
        'total_amount' => 0,
        'transaction_count' => 0
    ];
    
    foreach ($payment_methods as $method) {
        $totals['total_amount'] += $method['total_amount'];
        $totals['transaction_count'] += $method['transaction_count'];
    }
    
    // Get transactions
    $stmt = $conn->prepare("
        SELECT tr.trans_id, tr.seller_name, tr.seller_date, tr.total, p.payment_method, p.amount
        FROM transaction_receipts tr
        JOIN payments p ON tr.trans_id = p.trans_id
        WHERE tr.seller_date >= ? AND tr.seller_date <= ? AND tr.seller_id = ?
        ORDER BY tr.seller_date DESC
    ");
    $stmt->bind_param("ssi", $shift['start_time'], $shift['end_time'], $employee_id);
    $stmt->execute();
    $transactions = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
    
    echo json_encode([
        'success' => true,
        'shift' => $shift,
        'payment_methods' => $payment_methods,
        'totals' => $totals,
        'transactions' => $transactions
    ]);
    
    $stmt->close();
} else {
    echo json_encode(['success' => false, 'message' => 'Invalid action']);
}

$conn->close();
?>