<?php

declare(strict_types=1);

require_once __DIR__ . '/../../includes/bootstrap.php';

$config = require __DIR__ . '/../../config/config.php';

auth_start_session();
auth_require_role(['staff']);

$user = auth_user();
$title = 'Overview';

$staffId = (int)($user['user_id'] ?? 0);

$error = '';

$counts = [
    'tasks_today' => 0,
    'tasks_assigned' => 0,
    'tasks_completed' => 0,
    'tasks_pending' => 0,
    'tasks_in_progress' => 0,
    'total_customers' => 0,
];

try {
    $pdo = Database::pdo();

    // Today's tasks
    $stmt = $pdo->prepare(
        "SELECT COUNT(*) 
         FROM tasks 
         WHERE assigned_staff_id = :sid 
           AND scheduled_date = CURDATE() 
           AND status <> 'cancelled'"
    );
    $stmt->execute(['sid' => $staffId]);
    $counts['tasks_today'] = (int)$stmt->fetchColumn();

    // Total assigned tasks (not cancelled)
    $stmt = $pdo->prepare(
        "SELECT COUNT(*) 
         FROM tasks 
         WHERE assigned_staff_id = :sid 
           AND status <> 'cancelled'"
    );
    $stmt->execute(['sid' => $staffId]);
    $counts['tasks_assigned'] = (int)$stmt->fetchColumn();

    // Completed tasks
    $stmt = $pdo->prepare(
        "SELECT COUNT(*) 
         FROM tasks 
         WHERE assigned_staff_id = :sid 
           AND status = 'completed'"
    );
    $stmt->execute(['sid' => $staffId]);
    $counts['tasks_completed'] = (int)$stmt->fetchColumn();

    // Pending tasks
    $stmt = $pdo->prepare(
        "SELECT COUNT(*) 
         FROM tasks 
         WHERE assigned_staff_id = :sid 
           AND status = 'pending'"
    );
    $stmt->execute(['sid' => $staffId]);
    $counts['tasks_pending'] = (int)$stmt->fetchColumn();

    // In progress tasks (accepted or started)
    $stmt = $pdo->prepare(
        "SELECT COUNT(*) 
         FROM tasks 
         WHERE assigned_staff_id = :sid 
           AND status IN ('accepted', 'started')"
    );
    $stmt->execute(['sid' => $staffId]);
    $counts['tasks_in_progress'] = (int)$stmt->fetchColumn();

    // Total unique customers served
    $stmt = $pdo->prepare(
        "SELECT COUNT(DISTINCT sr.customer_id)
         FROM tasks t
         JOIN service_requests sr ON t.request_id = sr.request_id
         WHERE t.assigned_staff_id = :sid"
    );
    $stmt->execute(['sid' => $staffId]);
    $counts['total_customers'] = (int)$stmt->fetchColumn();

} catch (Throwable $e) {
    $error = $e->getMessage();
}

$nav = [
    ['label' => 'Overview', 'href' => '/pest/public/staff/overview.php', 'active' => true],
    ['label' => 'My Tasks', 'href' => '/pest/public/staff/index.php', 'active' => false],
    ['label' => 'History', 'href' => '/pest/public/staff/history.php', 'active' => false],
    ['label' => 'Location', 'href' => '/pest/public/staff/location.php', 'active' => false],
];

ob_start();
?>
<?php if ($error !== ''): ?>
    <div class="rounded-xl border border-red-200 bg-red-50 p-4 text-sm text-red-700">
        <?= e($error) ?>
    </div>
<?php endif; ?>

<div class="rounded-2xl bg-gradient-to-br from-slate-900 to-slate-700 p-6 text-white shadow-sm">
    <div class="flex items-center gap-3">
        <svg class="h-12 w-12 opacity-90" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 13.255A23.931 23.931 0 0112 15c-3.183 0-6.22-.62-9-1.745M16 6V4a2 2 0 00-2-2h-4a2 2 0 00-2 2v2m4 6h.01M5 20h14a2 2 0 002-2V8a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" />
        </svg>
        <div class="flex-1">
            <div class="text-xs/5 opacity-90">Welcome</div>
            <div class="mt-1 text-2xl font-semibold tracking-tight"><?= e((string)($user['full_name'] ?? 'Staff Member')) ?></div>
            <div class="mt-1 text-xs/5 opacity-90">User ID: <span class="font-medium"><?= e((string)($user['user_id'] ?? '')) ?></span> • Role: <span class="font-medium">Staff</span></div>
        </div>
    </div>
</div>

<div class="mt-6 grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
    <a class="block rounded-2xl bg-gradient-to-br from-sky-600 to-cyan-500 p-5 text-white shadow-sm transition hover:opacity-95" href="/pest/public/staff/index.php">
        <div class="flex items-center justify-between">
            <div class="text-xs/5 opacity-90">Today's Tasks</div>
            <svg class="h-5 w-5 opacity-75" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" />
            </svg>
        </div>
        <div class="mt-2 text-2xl font-semibold tracking-tight"><?= e((string)$counts['tasks_today']) ?></div>
        <div class="mt-1 text-xs/5 opacity-90">Scheduled for today</div>
    </a>

    <a class="block rounded-2xl bg-gradient-to-br from-indigo-600 to-violet-600 p-5 text-white shadow-sm transition hover:opacity-95" href="/pest/public/staff/index.php">
        <div class="flex items-center justify-between">
            <div class="text-xs/5 opacity-90">Total Assigned</div>
            <svg class="h-5 w-5 opacity-75" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" />
            </svg>
        </div>
        <div class="mt-2 text-2xl font-semibold tracking-tight"><?= e((string)$counts['tasks_assigned']) ?></div>
        <div class="mt-1 text-xs/5 opacity-90">All assigned tasks</div>
    </a>

    <a class="block rounded-2xl bg-gradient-to-br from-green-600 to-lime-600 p-5 text-white shadow-sm transition hover:opacity-95" href="/pest/public/staff/history.php">
        <div class="flex items-center justify-between">
            <div class="text-xs/5 opacity-90">Completed Tasks</div>
            <svg class="h-5 w-5 opacity-75" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
            </svg>
        </div>
        <div class="mt-2 text-2xl font-semibold tracking-tight"><?= e((string)$counts['tasks_completed']) ?></div>
        <div class="mt-1 text-xs/5 opacity-90">Successfully completed</div>
    </a>

    <a class="block rounded-2xl bg-gradient-to-br from-amber-600 to-orange-600 p-5 text-white shadow-sm transition hover:opacity-95" href="/pest/public/staff/index.php">
        <div class="flex items-center justify-between">
            <div class="text-xs/5 opacity-90">Pending Tasks</div>
            <svg class="h-5 w-5 opacity-75" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z" />
            </svg>
        </div>
        <div class="mt-2 text-2xl font-semibold tracking-tight"><?= e((string)$counts['tasks_pending']) ?></div>
        <div class="mt-1 text-xs/5 opacity-90">Awaiting action</div>
    </a>

    <a class="block rounded-2xl bg-gradient-to-br from-blue-600 to-indigo-600 p-5 text-white shadow-sm transition hover:opacity-95" href="/pest/public/staff/index.php">
        <div class="flex items-center justify-between">
            <div class="text-xs/5 opacity-90">In Progress</div>
            <svg class="h-5 w-5 opacity-75" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z" />
            </svg>
        </div>
        <div class="mt-2 text-2xl font-semibold tracking-tight"><?= e((string)$counts['tasks_in_progress']) ?></div>
        <div class="mt-1 text-xs/5 opacity-90">Currently working on</div>
    </a>

    <a class="block rounded-2xl bg-gradient-to-br from-emerald-600 to-teal-600 p-5 text-white shadow-sm transition hover:opacity-95" href="/pest/public/staff/history.php">
        <div class="flex items-center justify-between">
            <div class="text-xs/5 opacity-90">Customers Served</div>
            <svg class="h-5 w-5 opacity-75" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z" />
            </svg>
        </div>
        <div class="mt-2 text-2xl font-semibold tracking-tight"><?= e((string)$counts['total_customers']) ?></div>
        <div class="mt-1 text-xs/5 opacity-90">Unique customers</div>
    </a>
</div>
<?php
$content = ob_get_clean();

require __DIR__ . '/../../includes/dashboard_layout.php';
