<?php

namespace App\Http\Controllers\Partner;

use App\Http\Controllers\Controller;
use App\Models\PartnerShow;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;
use Inertia\Response;

class DashboardController extends Controller
{
    /**
     * Display the partner dashboard.
     */
    public function __invoke(Request $request): Response
    {
        $partner = Auth::user();

        // Get statistics
        $totalShows = PartnerShow::where('partner_id', $partner->id)->count();
        $activeShows = PartnerShow::where('partner_id', $partner->id)
            ->where('is_active', true)
            ->count();
        $liveShows = PartnerShow::where('partner_id', $partner->id)
            ->where('is_live', true)
            ->count();

        // Get recent shows
        $recentShows = PartnerShow::where('partner_id', $partner->id)
            ->latest()
            ->limit(5)
            ->get()
            ->map(function ($show) {
                return [
                    'id' => $show->id,
                    'showId' => $show->showId,
                    'title' => $show->title,
                    'is_active' => $show->is_active,
                    'is_live' => $show->is_live,
                    'created_at' => $show->created_at->toISOString(),
                    'time_ago' => $show->created_at->diffForHumans(),
                ];
            });

        return Inertia::render('Partner/Dashboard', [
            'stats' => [
                'totalShows' => $totalShows,
                'activeShows' => $activeShows,
                'liveShows' => $liveShows,
            ],
            'recentShows' => $recentShows,
        ]);
    }
}

