<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\Setting;
use App\Models\Show;
use App\Models\User;
use App\Services\ActivityLogService;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;
use Inertia\Response;

class ReplayController extends Controller
{
    public function __construct(
        protected ActivityLogService $activityLogService
    ) {}

    /**
     * Display a listing of replay shows that user has purchased.
     */
    public function index(): Response
    {
        $user = Auth::user();
        $hasActiveMembership = $user->hasActiveMembership();

        // Get user's purchased shows that have is_replay = true and is_active = false
        if ($hasActiveMembership) {
            // If user has active membership, all shows with is_replay = true and is_active = false are accessible
            $replayShows = $user->shows()
                ->where('is_replay', true)
                ->where('is_active', false)
                ->orderBy('start_time', 'desc')
                ->get();
        } else {
            // Only shows that user has purchased and have is_replay = true and is_active = false
            $replayShows = $user->shows()
                ->where('is_replay', true)
                ->where('is_active', false)
                ->orderBy('start_time', 'desc')
                ->get();
        }

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

    /**
     * Display a full page replay player for a specific show.
     */
    public function show(Show $show): Response
    {
        $user = Auth::user();
        $hasActiveMembership = $user->hasActiveMembership();

        // Check if user has access to this show
        $hasAccess = $hasActiveMembership || $show->hasUser($user);

        if (! $hasAccess) {
            abort(403, 'Anda tidak memiliki akses ke replay show ini.');
        }

        // Check if show has replay enabled and is inactive
        if (! $show->is_replay || $show->is_active) {
            abort(404, 'Replay show tidak tersedia.');
        }

        if (! $show->url_replay) {
            abort(404, 'URL replay tidak tersedia untuk show ini.');
        }

        // Log watch activity
        $this->activityLogService->logWatch(
            $show,
            "User {$user->name} watched replay show '{$show->title}'",
            [
                'show_id' => $show->id,
                'show_title' => $show->title,
                'has_active_membership' => $hasActiveMembership,
            ]
        );

        return Inertia::render('Dashboard/Replay/Show', [
            'show' => $show,
            'has_active_membership' => $hasActiveMembership,
            'membership_expires_at' => $user->membership_expires_at?->toIso8601String(),
        ]);
    }
}
