<?php

namespace App\Livewire\Auth;

use Livewire\Component;
use Illuminate\Support\Facades\Auth;

class Login extends Component
{
    public $email;
    public $password;
    public $email_verified_at;

    public function login()
    {
        $this->validate(
            [
                'email' => ['required', 'email'],
                'password' => ['required'],
            ],
            [
                'email.required' => t('auth.validation.email_required'),
                'email.email' => t('auth.validation.email_invalid'),
                'password.required' => t('auth.validation.password_required'),
            ]
        );

        if (!Auth::attempt([
            'email' => $this->email,
            'password' => $this->password
        ])) {
            $this->addError('email', t('auth.login.invalid_credentials'));
            return;
        }

        $user = Auth::user();

        if (!$user->email_verified_at) {
            Auth::logout(); // 🔥 wichtig!
            return redirect()->route('verify.notice', [
                'user' => $user->id
            ]);
        }

        request()->session()->regenerate();

        return redirect()->route('dashboard.index');
    }

    public function render()
    {
        return view('livewire.auth.login')
            ->layout('layouts.blank');
    }
}
