<?php

declare(strict_types=1);

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

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

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

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

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

$error = '';

$counts = [
    'requests_total' => 0,
    'services_completed' => 0,
    'payments_pending' => 0,
    'ipm_maps' => 0,
    'gap_reports' => 0,
    'trend_reports' => 0,
];

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

    // Requests
    $stmt = $pdo->prepare('SELECT COUNT(*) FROM service_requests WHERE customer_id = :cid');
    $stmt->execute(['cid' => $customerId]);
    $counts['requests_total'] = (int)$stmt->fetchColumn();

    // Completed services (tasks)
    $stmt = $pdo->prepare(
        "SELECT COUNT(*)
         FROM tasks t
         JOIN service_requests sr ON t.request_id = sr.request_id
         WHERE sr.customer_id = :cid AND t.status = 'completed'"
    );
    $stmt->execute(['cid' => $customerId]);
    $counts['services_completed'] = (int)$stmt->fetchColumn();

    // Pending/processing payments
    $stmt = $pdo->prepare(
        "SELECT COUNT(*)
         FROM payments
         WHERE customer_id = :cid AND payment_status IN ('pending','processing')"
    );
    $stmt->execute(['cid' => $customerId]);
    $counts['payments_pending'] = (int)$stmt->fetchColumn();

    // Uploaded customer reports
    CustomerReportsService::ensureTable($pdo);

    $stmt = $pdo->prepare('SELECT COUNT(*) FROM customer_reports WHERE customer_id = :cid AND report_type = :t');

    $stmt->execute(['cid' => $customerId, 't' => CustomerReportsService::TYPE_IPM_MAP]);
    $counts['ipm_maps'] = (int)$stmt->fetchColumn();

    $stmt->execute(['cid' => $customerId, 't' => CustomerReportsService::TYPE_GAP_ANALYSIS]);
    $counts['gap_reports'] = (int)$stmt->fetchColumn();

    $stmt->execute(['cid' => $customerId, 't' => CustomerReportsService::TYPE_TREND_ANALYSIS]);
    $counts['trend_reports'] = (int)$stmt->fetchColumn();
} catch (Throwable $e) {
    $error = $e->getMessage();
}

$nav = [
    ['label' => 'Overview', 'href' => '/pest/public/customer/overview.php', 'active' => true],
    ['label' => 'My Requests', 'href' => '/pest/public/customer/index.php', 'active' => false],
    ['label' => 'Request Service', 'href' => '/pest/public/customer/request_service.php', 'active' => false],
    ['label' => 'My Services', 'href' => '/pest/public/customer/my_services.php', 'active' => false],
    ['label' => 'Service Reports', 'href' => '/pest/public/customer/reports.php', 'active' => false],
    ['label' => 'IPM Map', 'href' => '/pest/public/customer/ipm_map.php', 'active' => false],
    ['label' => 'Gap Analysis Report', 'href' => '/pest/public/customer/gap_analysis_report.php', 'active' => false],
    ['label' => 'Trend Analysis Report', 'href' => '/pest/public/customer/trend_analysis_report.php', 'active' => false],
    ['label' => 'Complaints', 'href' => '/pest/public/customer/complaints.php', 'active' => false],
    ['label' => 'Payments', 'href' => '/pest/public/customer/payments.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="text-xs/5 opacity-90">Welcome</div>
    <div class="mt-2 text-2xl font-semibold tracking-tight"><?= e((string)($user['full_name'] ?? 'Customer')) ?></div>
    <div class="mt-1 text-xs/5 opacity-90">User ID: <span class="font-medium"><?= e((string)($user['user_id'] ?? '')) ?></span></div>
</div>

<div class="mt-6 grid grid-cols-1 gap-4 sm:grid-cols-2 xl:grid-cols-3">
    <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/customer/ipm_map.php">
        <div class="text-xs/5 opacity-90">IPM Maps</div>
        <div class="mt-2 text-2xl font-semibold tracking-tight"><?= e((string)$counts['ipm_maps']) ?></div>
        <div class="mt-1 text-xs/5 opacity-90">Uploaded maps available</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/customer/gap_analysis_report.php">
        <div class="text-xs/5 opacity-90">Gap Analysis Reports</div>
        <div class="mt-2 text-2xl font-semibold tracking-tight"><?= e((string)$counts['gap_reports']) ?></div>
        <div class="mt-1 text-xs/5 opacity-90">Monthly reports available</div>
    </a>

    <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/customer/trend_analysis_report.php">
        <div class="text-xs/5 opacity-90">Trend Analysis Reports</div>
        <div class="mt-2 text-2xl font-semibold tracking-tight"><?= e((string)$counts['trend_reports']) ?></div>
        <div class="mt-1 text-xs/5 opacity-90">Monthly reports available</div>
    </a>

    <a class="block rounded-2xl bg-gradient-to-br from-amber-500 to-orange-500 p-5 text-white shadow-sm transition hover:opacity-95" href="/pest/public/customer/index.php">
        <div class="text-xs/5 opacity-90">Service Requests</div>
        <div class="mt-2 text-2xl font-semibold tracking-tight"><?= e((string)$counts['requests_total']) ?></div>
        <div class="mt-1 text-xs/5 opacity-90">Total requests submitted</div>
    </a>

    <a class="block rounded-2xl bg-gradient-to-br from-rose-600 to-pink-600 p-5 text-white shadow-sm transition hover:opacity-95" href="/pest/public/customer/my_services.php">
        <div class="text-xs/5 opacity-90">Completed Services</div>
        <div class="mt-2 text-2xl font-semibold tracking-tight"><?= e((string)$counts['services_completed']) ?></div>
        <div class="mt-1 text-xs/5 opacity-90">Tasks marked completed</div>
    </a>
</div>
<?php
$content = ob_get_clean();

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