<?php

namespace App\Livewire;

use App\Models\JobPackage;
use App\Models\Contract;
use Livewire\Component;
use Illuminate\Support\Facades\Auth;

class AgentDashboard extends Component
{
    public $totalPackages;
    public $matchingPackages;
    public $selectedPackages;
    public $completedPackages;
    public $recentMatching = [];
    public $queueCount;

    public function mount()
    {
        // Total Paket
        $this->totalPackages = JobPackage::count();
        
        // Paket Matching
        $this->matchingPackages = JobPackage::where('status', 'matching')->count();
        
        // Paket Selected
        $this->selectedPackages = JobPackage::where('status', 'selected')->count();
        
        // Paket Completed
        $this->completedPackages = JobPackage::where('status', 'completed')->count();
        
        // Queue Count (paket yang menunggu matching)
        $this->queueCount = JobPackage::where('status', 'submitted')->count();

        // Recent Matching Activity
        $this->recentMatching = JobPackage::with(['ppk', 'selectedContractor'])
            ->whereIn('status', ['matching', 'selected', 'contracting'])
            ->orderBy('updated_at', 'desc')
            ->limit(5)
            ->get()
            ->map(function($package) {
                return [
                    'id' => $package->id,
                    'name' => $package->name,
                    'status' => $package->status,
                    'updated_at' => $package->updated_at,
                    'contractor' => $package->selectedContractor ? $package->selectedContractor->business_name : null,
                ];
            })
            ->toArray();
    }

    public function render()
    {
        return view('livewire.agent-dashboard');
    }
}