<?php
require_once __DIR__ . '/../config/init.php';

header('Content-Type: application/json');

Auth::requireAuth();

$stats = [];

// Weapons stats
$stats['weapons'] = [
    'total' => Database::fetchOne("SELECT COUNT(*) as count FROM weapons_inventory")['count'] ?? 0,
    'issued' => Database::fetchOne("SELECT COUNT(*) as count FROM weapons_inventory WHERE current_location = 'Issued'")['count'] ?? 0,
    'serviceable' => Database::fetchOne("SELECT COUNT(*) as count FROM weapons_inventory WHERE `condition` = 'Serviceable'")['count'] ?? 0,
    'unserviceable' => Database::fetchOne("SELECT COUNT(*) as count FROM weapons_inventory WHERE `condition` = 'Unserviceable'")['count'] ?? 0
];

// Ammunition stats
$stats['ammunition'] = [
    'total_types' => Database::fetchOne("SELECT COUNT(*) as count FROM ammunition_inventory")['count'] ?? 0,
    'total_rounds' => Database::fetchOne("SELECT SUM(balance) as total FROM ammunition_inventory")['total'] ?? 0,
    'expiring_soon' => Database::fetchOne(
        "SELECT COUNT(*) as count FROM ammunition_inventory 
         WHERE expiry_date IS NOT NULL
         AND expiry_date <= DATE_ADD(CURDATE(), INTERVAL 30 DAY)
         AND expiry_date >= CURDATE()"
    )['count'] ?? 0,
    'low_stock' => Database::fetchOne(
        "SELECT COUNT(*) as count FROM ammunition_inventory WHERE balance < 100"
    )['count'] ?? 0
];

// Asset stats
$stats['assets'] = [
    'land' => Database::fetchOne("SELECT COUNT(*) as count FROM land_assets")['count'] ?? 0,
    'buildings' => Database::fetchOne("SELECT COUNT(*) as count FROM building_assets")['count'] ?? 0,
    'rented' => Database::fetchOne("SELECT COUNT(*) as count FROM rented_properties")['count'] ?? 0,
    'projects' => Database::fetchOne("SELECT COUNT(*) as count FROM ongoing_projects")['count'] ?? 0,
    'movable' => Database::fetchOne("SELECT COUNT(*) as count FROM movable_assets")['count'] ?? 0,
    'ict' => Database::fetchOne("SELECT COUNT(*) as count FROM ict_assets")['count'] ?? 0
];

// Fleet stats
$stats['fleet'] = [
    'vehicles' => Database::fetchOne("SELECT COUNT(*) as count FROM vehicle_assets")['count'] ?? 0,
    'aircraft' => Database::fetchOne("SELECT COUNT(*) as count FROM aircraft_assets")['count'] ?? 0,
    'marine' => Database::fetchOne("SELECT COUNT(*) as count FROM marine_assets")['count'] ?? 0,
    'motorcycles' => Database::fetchOne("SELECT COUNT(*) as count FROM motorcycle_assets")['count'] ?? 0
];

// Requisition stats
$stats['requisitions'] = [
    'pending' => Database::fetchOne("SELECT COUNT(*) as count FROM requisitions WHERE status = 'Pending'")['count'] ?? 0,
    'approved' => Database::fetchOne("SELECT COUNT(*) as count FROM requisitions WHERE status = 'Approved'")['count'] ?? 0,
    'rejected' => Database::fetchOne("SELECT COUNT(*) as count FROM requisitions WHERE status = 'Rejected'")['count'] ?? 0
];

// User stats
$stats['users'] = [
    'total' => Database::fetchOne("SELECT COUNT(*) as count FROM users WHERE is_active = 1")['count'] ?? 0
];

// Recent activity
$stats['recent_activity'] = Database::fetchAll(
    "SELECT al.*, u.full_name 
     FROM audit_logs al
     LEFT JOIN users u ON al.user_id = u.id
     ORDER BY al.created_at DESC
     LIMIT 10"
);

echo json_encode(['success' => true, 'stats' => $stats]);