<?php

namespace Daylight\Core\Livewire\Store\Users;

use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Password;
use Illuminate\View\View;
use Livewire\Component;

class UserActions extends Component
{
    public User $user;

    public function impersonate()
    {
        Auth::user()->impersonate($this->user);

        return redirect()->route(app()->getLocale() . '_page', ['any' => null]);
    }

    public function sendVerificationEmail()
    {
        $this->user->sendEmailVerificationNotification();

        return redirect(
            route('daylight.store.users.show', $this->user)
        )->with('success', __('daylight::general.verification_email_sent'));
    }

    public function disableMfa()
    {
        $this->user->disableTwoFactor();

        return redirect(
            route('daylight.store.users.show', $this->user)
        )->with('success', __('daylight::general.mfa_disabled'));
    }

    public function sendPasswordReset()
    {
        $status = Password::broker()->sendResetLink(['email' => $this->user->email]);

        if ($status === Password::RESET_LINK_SENT) {
            return redirect(
                route('daylight.store.users.show', $this->user)
            )->with('success', __('daylight::general.password_reset_email_sent'));
        }

        return redirect(route('daylight.store.users.show', $this->user));
    }

    public function render(): View
    {
        return view('daylight::livewire.store.users.user-actions');
    }
}
