<?php
declare(strict_types=1);

namespace App\Modules\Procurement\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Core\App;

class ProcurementDashboardController extends Controller
{
    public function index(Request $request): Response
    {
        $db = App::getInstance()->db();

        // Summary counts
        $prCounts = $db->selectOne(
            "SELECT
                COUNT(CASE WHEN `status` = 'submitted' THEN 1 END) as pending_approval,
                COUNT(CASE WHEN `status` = 'approved' THEN 1 END) as approved,
                COUNT(CASE WHEN `status` IN ('draft','submitted','approved') THEN 1 END) as open_total
             FROM `purchase_requisitions` WHERE `is_archived` = 0"
        );

        $poCounts = $db->selectOne(
            "SELECT
                COUNT(CASE WHEN `status` IN ('draft','submitted','approved') THEN 1 END) as open_pos,
                COUNT(CASE WHEN `status` = 'approved' THEN 1 END) as awaiting_receipt,
                COALESCE(SUM(CASE WHEN `status` IN ('approved') THEN `total_amount` ELSE 0 END), 0) as open_value
             FROM `purchase_orders` WHERE `is_archived` = 0"
        );

        $grnCounts = $db->selectOne(
            "SELECT COUNT(*) as pending FROM `goods_received_notes` WHERE `status` = 'inspecting' AND `is_archived` = 0"
        );

        $invoiceCounts = $db->selectOne(
            "SELECT
                COUNT(CASE WHEN `status` IN ('draft','verified') THEN 1 END) as pending,
                COUNT(CASE WHEN `match_status` = 'discrepancy' THEN 1 END) as discrepancies,
                COALESCE(SUM(CASE WHEN `status` = 'approved' THEN `total_amount` ELSE 0 END), 0) as unpaid_value
             FROM `vendor_invoices` WHERE `is_archived` = 0"
        );

        $overdueAP = $db->selectOne(
            "SELECT COUNT(*) as cnt, COALESCE(SUM(`balance`), 0) as total
             FROM `accounts_payable`
             WHERE `status` = 'overdue' AND `is_archived` = 0"
        );

        // Recent activity (last 10 items across all procurement)
        $recentActivity = $db->select(
            "(SELECT 'pr' as type, `pr_number` as doc_number, `status`, `created_at`, `estimated_total` as amount FROM `purchase_requisitions` WHERE `is_archived` = 0 ORDER BY `created_at` DESC LIMIT 5)
             UNION ALL
             (SELECT 'grn' as type, `grn_number` as doc_number, `status`, `created_at`, `total_received_value` as amount FROM `goods_received_notes` WHERE `is_archived` = 0 ORDER BY `created_at` DESC LIMIT 5)
             UNION ALL
             (SELECT 'invoice' as type, `internal_number` as doc_number, `status`, `created_at`, `total_amount` as amount FROM `vendor_invoices` WHERE `is_archived` = 0 ORDER BY `created_at` DESC LIMIT 5)
             ORDER BY `created_at` DESC LIMIT 10"
        );

        return $this->view('Procurement.Views.dashboard.index', [
            'prCounts'       => $prCounts,
            'poCounts'       => $poCounts,
            'grnCounts'      => $grnCounts,
            'invoiceCounts'  => $invoiceCounts,
            'overdueAP'      => $overdueAP,
            'recentActivity' => $recentActivity,
        ]);
    }
}
