<?php

namespace App\Livewire\Auth;

use App\Models\User;
use App\Models\Verification;
use App\Services\VerificationService;
use Illuminate\Support\Facades\Cache;
use Livewire\Component;
use Carbon\Carbon;

class VerifyNotice extends Component
{
    public $cooldown = 0;
    public $user;
    public $verification;
    public $blockedUntil;
    public $cooldownUntil;

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

        if ($this->user->email_verified_at) {
            return redirect()->route('dashboard.index');
        }

        // 🔥 aktuelle verification holen
        $this->verification = Verification::where('user_id', $this->user->id)
            ->where('type', 'email')
            ->whereNull('verified_at')
            ->latest()
            ->first();

        // 🔥 COOLDOWN (Redis)
        $key = "verify:cooldown:{$this->user->id}";
        $expiresAtRaw = Cache::get($key);

        if ($expiresAtRaw) {
            $expiresAt = Carbon::parse($expiresAtRaw);

            $this->cooldownUntil = $expiresAt
                ->copy()
                ->timezone($this->user->timezone);

            $this->cooldown = max(0, floor(now()->diffInSeconds($expiresAt, false)));
        }

        // 🔥 BLOCK (aus verification)
        if (
            $this->verification &&
            $this->verification->resends >= 5
        ) {
            $this->blockedUntil = $this->verification->resends_reset_at
                ? $this->verification->resends_reset_at
                    ->copy()
                    ->timezone($this->user->timezone)
                : null;
        }
    }

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

    public function resend()
    {
        if ($this->cooldown > 0) return;

        $verification = $this->verification;

        // 🔥 wenn keine existiert → neu erstellen
        if (!$verification) {
            $this->verification = VerificationService::create($this->user);
            return;
        }

        // 🔥 RESET nach 1h
        if (
            $verification->resends_reset_at &&
            now()->gt($verification->resends_reset_at)
        ) {
            $verification->update([
                'resends' => 0,
                'resends_reset_at' => now()->addHour(),
            ]);
        }

        // 🔥 INIT
        if (!$verification->resends_reset_at) {
            $verification->update([
                'resends_reset_at' => now()->addHour(),
            ]);
        }

        // 🔥 LIMIT
        if ($verification->resends >= 5) {
            $this->dispatch('notify', [
                'type' => 'error',
                'title' => t('auth.verify.limit_title'),
                'message' => t('auth.verify.limit_message'),
            ]);
            return;
        }

        // 🔥 NEUE VERIFICATION (inkl. Mail + URL)
        $this->verification = VerificationService::create($this->user);

        // 🔥 COUNTER erhöhen (auf alter verification!)
        $verification->increment('resends');

        // 🔥 COOLDOWN
        $expiresAt = now()->addSeconds(30);

        Cache::put(
            "verify:cooldown:{$this->user->id}",
            $expiresAt->toDateTimeString(),
            $expiresAt
        );

        $this->cooldown = 30;
        $this->cooldownUntil = $expiresAt
            ->copy()
            ->timezone($this->user->timezone);
    }

    public function render()
    {
        return view('livewire.auth.verify-notice')
            ->layout('layouts.blank');
    }
}
//
//namespace App\Livewire\Auth;
//
//use App\Models\User;
//use App\Services\MailService;
//use Illuminate\Support\Facades\Cache;
//use Illuminate\Support\Facades\URL;
//use Livewire\Component;
//use Carbon\Carbon;
//
//class VerifyNotice extends Component
//{
//    public $cooldown = 0;
//    public $user;
//    public $blockedUntil;
//    public $cooldownUntil;
//
//    public function mount($user)
//    {
//        $this->user = User::findOrFail($user);
//
//        if ($this->user->email_verified_at) {
//            return redirect()->route('dashboard.index');
//        }
//
//        $key = "verify:cooldown:{$this->user->id}";
//
//        $expiresAtRaw = Cache::get($key);
//
//        if ($expiresAtRaw) {
//
//            // 🔥 STRING → CARBON
//            $expiresAt = Carbon::parse($expiresAtRaw);
//
//            $this->cooldownUntil = $expiresAt
//                ->copy()
//                ->timezone($this->user->timezone);
//
//            $this->cooldown = max(0, floor(now()->diffInSeconds($expiresAt, false)));
//        }
//
//        // 🔥 BLOCKED
//        if ($this->user->verification_resends >= 5) {
//            $this->blockedUntil = $this->user->verification_resends_reset_at
//                ? $this->user->verification_resends_reset_at
//                    ->copy()
//                    ->timezone($this->user->timezone)
//                : null;
//        }
//    }
//
//    public function tick()
//    {
//        if ($this->cooldown > 0) {
//            $this->cooldown--;
//        }
//    }
//
//    public function resend()
//    {
//        if ($this->cooldown > 0) return;
//
//        $user = $this->user;
//        $key = "verify:cooldown:{$user->id}";
//
//        // 🔥 RESET (1h)
//        if (
//            $user->verification_resends_reset_at &&
//            now()->gt($user->verification_resends_reset_at)
//        ) {
//            $user->update([
//                'verification_resends' => 0,
//                'verification_resends_reset_at' => now()->addHour(),
//            ]);
//        }
//
//        // 🔥 INIT
//        if (!$user->verification_resends_reset_at) {
//            $user->update([
//                'verification_resends_reset_at' => now()->addHour()
//            ]);
//        }
//
//        // 🔥 LIMIT
//        if ($user->verification_resends >= 5) {
//            $this->dispatch('notify', [
//                'type' => 'error',
//                'title' => t('auth.verify.limit_title'),
//                'message' => t('auth.verify.limit_message'),
//            ]);
//            return;
//        }
//
//        // 🔥 SIGNED URL
//        $url = URL::temporarySignedRoute(
//            'verify.user',
//            now()->addMinutes(10),
//            ['user' => $user->id]
//        );
//
//        // 🔥 MAIL
//        MailService::queue(
//            $user->email,
//            'auth.verify',
//            [
//                'user' => $user->name,
//                'url' => $url,
//            ],
//            $user->locale,
//            null,
//            $user->id
//        );
//
//        // 🔥 COUNTER
//        $user->increment('verification_resends');
//
//        // 🔥 COOLDOWN
//        $expiresAt = now()->addSeconds(30);
//
//        // 👉 STRING speichern!
//        Cache::put($key, $expiresAt->toDateTimeString(), $expiresAt);
//
//        $this->cooldown = 30;
//
//        $this->cooldownUntil = $expiresAt
//            ->copy()
//            ->timezone($user->timezone);
//    }
//
//    public function render()
//    {
//        return view('livewire.auth.verify-notice')
//            ->layout('layouts.blank');
//    }
//}

//
//namespace App\Livewire\Auth;
//
//use App\Models\User;
//use App\Services\MailService;
//use Illuminate\Support\Facades\Cache;
//use Illuminate\Support\Facades\URL;
//use Livewire\Component;
//
//class VerifyNotice extends Component
//{
//    public $cooldown = 0;
//    public $user;
//    public $blockedUntil;
//    public $cooldownUntil;
//
//    public function mount($user)
//    {
//        $this->user = User::findOrFail($user);
//
//        $key = "verify:cooldown:{$this->user->id}";
//
//        if (Cache::has($key)) {
//            $expiresAt = \Carbon\Carbon::parse(Cache::get($key));
//
//            $this->cooldownUntil = $expiresAt
//                ->copy()
//                ->timezone($this->user->timezone);
//
//            $this->cooldown = now()->diffInSeconds($expiresAt, false);
//
//            if ($this->cooldown < 0) {
//                $this->cooldown = 0;
//            }
//        }
//
//        if ($this->user->verification_resends >= 5) {
//            $this->blockedUntil = $this->user->verification_resends_reset_at
//                ? \Carbon\Carbon::parse($this->user->verification_resends_reset_at)
//                    ->timezone($this->user->timezone)
//                : null;
//        }
//    }
//
//    public function tick()
//    {
//        if ($this->cooldown > 0) {
//            $this->cooldown--;
//        }
//    }
//
//    public function resend()
//    {
//        if ($this->cooldown > 0) return;
//
//        $user = $this->user;
//        $key = "verify:cooldown:{$user->id}";
//
//        // 🔥 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
//        if (!$user->verification_resends_reset_at) {
//            $user->update([
//                'verification_resends_reset_at' => now()->addHour()
//            ]);
//        }
//
//        // 🔥 LIMIT
//        if ($user->verification_resends >= 5) {
//            $this->dispatch('notify', [
//                'type' => 'error',
//                'title' => t('auth.verify.limit_title'),
//                'message' => t('auth.verify.limit_message'),
//            ]);
//            return;
//        }
//
//        // 🔥 URL generieren
//        $url = URL::temporarySignedRoute(
//            'verify.user',
//            now()->addMinutes(10),
//            ['user' => $user->id]
//        );
//
//        // 🔥 Queue
//        MailService::queue(
//            $user->email,
//            'auth.verify',
//            [
//                'user' => $user->name,
//                'url' => $url,
//            ],
//            $user->locale,
//            null,
//            $user->id
//        );
//
//        // 🔥 Counter erhöhen
//        $user->increment('verification_resends');
//
//        $expiresAt = now()->addSeconds(30);
//
//        Cache::put($key, now()->addSeconds(30), now()->addSeconds(30));
//
//        $this->cooldown = 30;
//        $this->cooldownUntil = $expiresAt;
//    }
//
//
////    public function resend()
////    {
////        if ($this->cooldown > 0) return;
////
////        $user = $this->user;
////
////        $url = URL::temporarySignedRoute(
////            'verify.user',
////            now()->addMinutes(10),
////            ['user' => $user->id]
////        );
////
////        MailService::queue(
////            $user->email,
////            'auth.verify',
////            [
////                'user' => $user->name,
////                'url' => $url,
////            ],
////            $user->locale,
////            null,
////            $user->id
////        );
////
////        $this->cooldown = 30;
////    }
//
//    public function render()
//    {
//        return view('livewire.auth.verify-notice')
//            ->layout('layouts.blank');
//    }
//}
