<?php

declare(strict_types=1);

namespace ImovelZapAi\SuperAdmin\Application\Actions;

use Illuminate\Support\Facades\Auth;
use ImovelZapAi\SuperAdmin\Application\Services\PlatformAuditLogger;
use ImovelZapAi\SuperAdmin\Infrastructure\Persistence\Eloquent\PlatformOperatorModel;

final class PlatformLogoutAction
{
    public function __construct(
        private readonly PlatformAuditLogger $audit,
    ) {}

    public function execute(): void
    {
        /** @var PlatformOperatorModel|null $operator */
        $operator = Auth::guard('platform')->user();
        if ($operator !== null) {
            $this->audit->log('platform.logout', PlatformOperatorModel::class, (string) $operator->id);
        }

        Auth::guard('platform')->logout();
        request()->session()->forget('platform_mfa_passed');
        request()->session()->invalidate();
        request()->session()->regenerateToken();
    }
}
