<?php

namespace App\Http\Controllers\Meet;

use App\Http\Controllers\Controller;
use App\Models\Participant;
use App\Models\Room;
use App\Models\Session;
use App\Models\SessionFeedback;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class LeaveController extends Controller
{
    public function show(Request $request, Session $session): View|RedirectResponse
    {
        $session->loadMissing('room.organization');
        abort_unless($session->room, 404);

        $feedback = (array) $request->session()->get('meet.left.feedback', []);

        if (($feedback['session_uuid'] ?? null) !== $session->uuid) {
            return redirect()->route('meet.join', $session->room);
        }

        return view('meet.left.show', [
            'session' => $session,
            'room' => $session->room,
            'organization' => $session->room->organization,
            'feedback' => $feedback,
        ]);
    }

    public function store(Request $request, Session $session): RedirectResponse
    {
        $feedback = (array) $request->session()->get('meet.left.feedback', []);

        abort_unless(($feedback['session_uuid'] ?? null) === $session->uuid, 403);

        $validated = $request->validate([
            'rating' => ['required', 'integer', 'min:1', 'max:5'],
            'audio_rating' => ['nullable', 'integer', 'min:1', 'max:5'],
            'video_rating' => ['nullable', 'integer', 'min:1', 'max:5'],
            'comment' => ['nullable', 'string', 'max:1000'],
        ]);

        SessionFeedback::updateOrCreate(
            [
                'session_id' => $session->id,
                'participant_uuid' => $feedback['participant_uuid'] ?? null,
            ],
            [
                'display_name' => $feedback['display_name'] ?? null,
                'user_ref' => $feedback['user_ref'] ?? null,
                'rating' => $validated['rating'],
                'audio_rating' => $validated['audio_rating'] ?? null,
                'video_rating' => $validated['video_rating'] ?? null,
                'comment' => $validated['comment'] ?? null,
            ],
        );

        $request->session()->forget('meet.left.feedback');

        return redirect()
            ->route('meet.left.thanks', $session)
            ->with('rated', true);
    }

    public function thanks(Request $request, Session $session): View
    {
        $session->loadMissing('room.organization');

        return view('meet.left.thanks', [
            'session' => $session,
            'room' => $session->room,
            'organization' => $session->room->organization,
        ]);
    }

    public function ended(Request $request, Session $session): View|RedirectResponse
    {
        $session->loadMissing('room.organization');
        abort_unless($session->room, 404);

        if ($session->isLive()) {
            return redirect()->route('meet.room', $session);
        }

        $canGiveFeedback = $this->prepareFeedbackSession($request, $session);

        return view('meet.ended.show', [
            'session' => $session,
            'room' => $session->room,
            'organization' => $session->room->organization,
            'canGiveFeedback' => $canGiveFeedback,
        ]);
    }

    public function prepareFeedbackSession(Request $request, Session $session): bool
    {
        $participantUuid = $request->session()->get("meet.participant.{$session->uuid}");

        if ($participantUuid) {
            $participant = Participant::where('uuid', $participantUuid)
                ->where('session_id', $session->id)
                ->first();

            if ($participant) {
                $request->session()->put('meet.left.feedback', [
                    'session_uuid' => $session->uuid,
                    'participant_uuid' => $participant->uuid,
                    'display_name' => $participant->display_name,
                    'user_ref' => $participant->user_ref,
                ]);
                $request->session()->forget("meet.participant.{$session->uuid}");

                return true;
            }
        }

        $feedback = (array) $request->session()->get('meet.left.feedback', []);

        return ($feedback['session_uuid'] ?? null) === $session->uuid;
    }
}
