<?php

namespace App\Http\Controllers;

use App\Models\Setting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;
use Inertia\Response;

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

        // If user has active membership, show all active shows
        // Otherwise, show only purchased shows
        if ($hasActiveMembership) {
            $shows = \App\Models\Show::where('is_active', true)
                ->orderBy('start_time', 'desc')
                ->get();
        } else {
            // Load shows yang dimiliki user dengan eager loading
            $shows = $user->shows()
                ->orderBy('show_user.purchased_at', 'desc')
                ->get();
        }

        return Inertia::render('Dashboard', [
            'shows' => $shows,
            'has_active_membership' => $hasActiveMembership,
            'membership_expires_at' => $user->membership_expires_at?->toIso8601String(),
            'membership_started_at' => $user->membership_started_at?->toIso8601String(),
            'streaming_maintenance' => Setting::isEnabled('streaming_maintenance', false),
        ]);
    }
}
