<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Partner;
use App\Models\Event;
use App\Models\Transaction;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class OversightController extends Controller
{
    public function index()
    {
        // --- 1. Statistik Partner ---
        $totalActive = Partner::where('status', 'active')->count();
        $totalPending = Partner::where('status', 'pending')->count();
        $totalSuspended = Partner::where('status', 'suspended')->count();
        $totalPartners = Partner::count();

        // --- 2. Grafik Pendapatan Global Bulanan (12 bulan) ---
        $globalRevenue = Transaction::select(
            DB::raw('YEAR(created_at) as year'),
            DB::raw('MONTH(created_at) as month'),
            DB::raw('SUM(total_price) as total')
        )
            ->whereIn('status', ['settlement', 'success'])
            ->where('created_at', '>=', now()->subMonths(12))
            ->groupBy('year', 'month')
            ->orderBy('year', 'asc')
            ->orderBy('month', 'asc')
            ->get();

        $globalRevenueChart = [];
        $globalRevenueLabels = [];
        for ($i = 11; $i >= 0; $i--) {
            $date = now()->subMonths($i);
            $month = $date->month;
            $year = $date->year;
            $label = $date->format('M Y');

            $found = $globalRevenue->firstWhere(function ($item) use ($month, $year) {
                return $item->year == $year && $item->month == $month;
            });

            $globalRevenueLabels[] = $label;
            $globalRevenueChart[] = $found ? (int)$found->total : 0;
        }

        // --- 3. Top 10 Partners by Revenue ---
        $topPartners = Partner::withCount(['events'])
            ->get()
            ->map(function ($partner) {
                $partner->total_revenue = Transaction::whereHas('event', function ($q) use ($partner) {
                    $q->where('partner_id', $partner->id);
                })
                ->whereIn('status', ['settlement', 'success'])
                ->sum('total_price');
                return $partner;
            })
            ->sortByDesc('total_revenue')
            ->take(10);

        // --- 4. Partner Pending (butuh approval) ---
        $pendingPartners = Partner::where('status', 'pending')
            ->latest()
            ->take(10)
            ->get();

        // --- 5. Statistik Global ---
        $totalRevenue = Transaction::whereIn('status', ['settlement', 'success'])->sum('total_price');
        $totalTicketsSold = Transaction::whereIn('status', ['settlement', 'success'])->count();
        $totalEvents = Event::count();

        return view('admin.oversight', compact(
            'totalActive',
            'totalPending',
            'totalSuspended',
            'totalPartners',
            'globalRevenueLabels',
            'globalRevenueChart',
            'topPartners',
            'pendingPartners',
            'totalRevenue',
            'totalTicketsSold',
            'totalEvents'
        ));
    }
}

