<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Models\User;
use App\Services\ImpersonationService;
use Filament\Notifications\Notification;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

final class ImpersonationController extends Controller
{
    public function __construct(
        private readonly ImpersonationService $impersonationService
    ) {}

    /**
     * Start impersonating a user.
     */
    public function start(Request $request, User $user)
    {
        $admin = Auth::user();

        if (! $admin || ! $admin->isSuperAdmin()) {
            Notification::make()
                ->title('Access Denied')
                ->body('Only SuperAdmin users can impersonate other users.')
                ->danger()
                ->send();

            return redirect()->back();
        }

        if (! $this->impersonationService->startImpersonation($admin, $user, $request)) {
            Notification::make()
                ->title('Impersonation Failed')
                ->body('Unable to start impersonation. Please check permissions and try again.')
                ->danger()
                ->send();

            return redirect()->back();
        }

        Notification::make()
            ->title('Impersonation Started')
            ->body("You are now logged in as {$user->name}.")
            ->success()
            ->send();

        // Redirect to user dashboard (assuming you have a dashboard route)
        return redirect()->route('dashboard');
    }

    /**
     * Stop impersonating the current user.
     */
    public function stop(Request $request)
    {
        if (! $this->impersonationService->isImpersonating()) {
            return redirect()->to(\Filament\Pages\Dashboard::getUrl());
        }

        $impersonator = $this->impersonationService->getCurrentImpersonator();

        if (! $this->impersonationService->stopImpersonation($request)) {
            Notification::make()
                ->title('Failed to Stop Impersonation')
                ->body('Unable to stop impersonation session.')
                ->danger()
                ->send();

            return redirect()->back();
        }

        Notification::make()
            ->title('Impersonation Ended')
            ->body('You have been returned to your admin account.')
            ->success()
            ->send();

        return redirect()->to(\Filament\Pages\Dashboard::getUrl());
    }

    /**
     * Show impersonation status (AJAX endpoint).
     */
    public function status(Request $request)
    {
        if (! $this->impersonationService->isImpersonating()) {
            return response()->json([
                'is_impersonating' => false,
            ]);
        }

        $impersonator = $this->impersonationService->getCurrentImpersonator();
        $target = Auth::user();
        $remainingMinutes = $this->impersonationService->getRemainingMinutes();

        return response()->json([
            'is_impersonating' => true,
            'impersonator' => [
                'name' => $impersonator?->name,
                'email' => $impersonator?->email,
            ],
            'target' => [
                'name' => $target?->name,
                'email' => $target?->email,
            ],
            'remaining_minutes' => $remainingMinutes,
            'expires_soon' => $remainingMinutes <= 5,
        ]);
    }
}
