<?php

namespace App\Livewire\Auth;

use App\Models\User;
use App\Services\MailService;
use Livewire\Component;

class Verify extends Component
{
    public $code = ['', '', '', '', '', ''];
    public $user;
    public $cooldown = 0;

    public function mount($user)
    {
        $this->user = User::findOrFail($user);
    }

    public function updatedCode()
    {
        if (strlen(implode('', $this->code)) === 6) {
            $this->verify();
        }
    }

    public function tick()
    {
        if ($this->cooldown > 0) {
            $this->cooldown--;
        }
    }

    public function resend()
    {
        $user = $this->user;

        // 🔥 Reset nach 1 Stunde
        if (
            $user->verification_resends_reset_at &&
            now()->gt($user->verification_resends_reset_at)
        ) {
            $user->update([
                'verification_resends' => 0,
                'verification_resends_reset_at' => now()->addHour(),
            ]);
        }

        // 🔥 Initial setzen (beim ersten Mal)
        if (!$user->verification_resends_reset_at) {
            $user->verification_resends_reset_at = now()->addHour();
        }

        // 🔥 Limit prüfen
        if ($user->verification_resends >= 5) {

            $this->dispatch('notify', [
                'type' => 'error',
                'title' => t('auth.verify.limit_title'),
                'message' => t('auth.verify.limit_message'),
            ]);

            return;
        }

        // 🔥 Code neu generieren
        $code = rand(100000, 999999);

        $user->update([
            'email_verification_code' => $code,
            'email_verification_expires_at' => now()->addMinutes(10),

            'verification_resends' => $user->verification_resends + 1,
            'verification_resends_reset_at' => $user->verification_resends_reset_at ?? now()->addHour(),
        ]);

        MailService::queue(
            $user->email,
            'auth.verify',
            [
                'user' => $user->name,
                'code' => $code,
            ],
            $user->locale,
            null,
            $user->id,
        );

        $this->cooldown = 30;
    }


    public function verify()
    {
        $code = implode('', $this->code);

        if ($code !== $this->user->email_verification_code) {
            $this->addError('code', t('auth.verify.invalid'));
            return;
        }

        if (now()->gt($this->user->email_verification_expires_at)) {
            $this->addError('code', t('auth.verify.expired'));
            return;
        }

        $this->user->update([
            'email_verified_at' => now(),
            'email_verification_code' => null,
            'email_verification_expires_at' => null,
            'verification_resends' => 0,
            'verification_resends_reset_at' => null,
        ]);

        session()->flash('success', t('auth.verify.success'));

        return redirect()->route('login');
    }

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