<?php

namespace App\Livewire\LicenseTokens\Components;

use App\Models\Clients\Client;
use Livewire\Component;

class LicenseForm extends Component
{
    public bool $autoGenerateToken = true;
    public ?string $token = null;
    public ?int $clientId = null;
    public ?string $startAt = null;
    public ?string $endAt = null;
    public ?string $observation = null;

    public function mount(
        bool $autoGenerateToken = true,
        ?string $token = null,
        ?int $clientId = null,
        ?string $startAt = null,
        ?string $endAt = null,
        ?string $observation = null
    ) {
        $this->autoGenerateToken = $autoGenerateToken;
        $this->clientId = $clientId;
        $this->startAt = $startAt;
        $this->endAt = $endAt;
        $this->observation = $observation;
        
        // Si auto-generar está activo y no hay token, generar uno
        if ($autoGenerateToken && empty($token)) {
            $this->generateToken();
        } else {
            $this->token = $token;
        }
    }

    public function updatedAutoGenerateToken()
    {
        if ($this->autoGenerateToken) {
            $this->generateToken();
        } else {
            $this->token = null;
        }
        $this->dispatch('updatedAutoGenerateToken', $this->autoGenerateToken);
    }

    public function updatedToken()
    {
        $this->dispatch('updatedToken', $this->token);
    }

    public function updatedClientId()
    {
        $this->dispatch('updatedClientId', $this->clientId);
    }

    public function updatedStartAt()
    {
        // Auto-completar endAt = startAt + 1 año
        if ($this->startAt) {
            $startDate = \Carbon\Carbon::parse($this->startAt);
            $this->endAt = $startDate->addYear()->format('Y-m-d');
        }
        $this->dispatch('updatedStartAt', $this->startAt);
        $this->dispatch('updatedEndAt', $this->endAt);
    }

    public function updatedEndAt()
    {
        $this->dispatch('updatedEndAt', $this->endAt);
    }

    public function updatedObservation()
    {
        $this->dispatch('updatedObservation', $this->observation);
    }

    public function generateToken()
    {
        $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        
        do {
            // Generar token con patrón: 3IP-XXXX-XXXX-XXXX-XXXX
            $blocks = [];
            for ($i = 0; $i < 4; $i++) {
                $block = '';
                for ($j = 0; $j < 4; $j++) {
                    $block .= $characters[random_int(0, strlen($characters) - 1)];
                }
                $blocks[] = $block;
            }
            $generatedToken = '3IP-' . implode('-', $blocks);
        } while (\App\Models\Products\LicenseToken::where('token', $generatedToken)->exists());

        $this->token = $generatedToken;
        $this->dispatch('updatedToken', $this->token);
    }

    public function render()
    {
        return view('livewire.license-tokens.components.license-form', [
            'clients' => Client::orderBy('name')->get(),
        ]);
    }
}

