<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Products\LicenseToken;
use App\Models\Clients\Client;
use Illuminate\Support\Str;

class LicenseTokenSeeder extends Seeder
{
    public function run(): void
    {
        /*
        |--------------------------------------------------------------------------
        | CLIENTE: 3ipunt-demo
        |--------------------------------------------------------------------------
        */
        $clientDemo = Client::where('shortname', '3ipunt-demo')->first();

        if ($clientDemo) {

            // Token FULL principal
            LicenseToken::updateOrCreate(
                ['token' => 'DEMO-' . Str::upper(Str::random(32))],
                [
                    'name'         => 'Licencia Principal Demo',
                    'client_id'    => $clientDemo->id,
                    'active'       => true,
                    'start_at'     => now()->subMonth(),
                    'end_at'       => now()->addMonths(6),
                    'mode'         => 'full',
                    'usage_limit'  => null,
                    'metadata'     => [
                        'environment' => 'development',
                        'internal'    => true,
                    ],
                    'observation'  => 'Token principal interno para los desarrollos y pruebas.',
                ]
            );

            // Token TRIAL de corta duración
            LicenseToken::updateOrCreate(
                ['token' => 'TRIAL-' . Str::upper(Str::random(20))],
                [
                    'name'         => 'Licencia de Prueba Temporal',
                    'client_id'    => $clientDemo->id,
                    'active'       => true,
                    'start_at'     => now(),
                    'end_at'       => now()->addDays(14),
                    'mode'         => 'trial',
                    'usage_limit'  => 100,
                    'metadata'     => [
                        'notes'  => 'Licencia temporal de prueba para demos.',
                        'limit'  => 100,
                    ],
                    'observation'  => 'Token temporal para demostraciones comerciales.',
                ]
            );
        }

        /*
        |--------------------------------------------------------------------------
        | CLIENTE: client-alpha
        |--------------------------------------------------------------------------
        */
        $clientAlpha = Client::where('shortname', 'client-alpha')->first();

        if ($clientAlpha) {

            LicenseToken::updateOrCreate(
                ['token' => 'ALPHA-' . Str::upper(Str::random(28))],
                [
                    'name'         => 'Licencia Producción Cliente Alpha',
                    'client_id'    => $clientAlpha->id,
                    'active'       => true,
                    'start_at'     => now()->subDays(10),
                    'end_at'       => now()->addYear(),
                    'mode'         => 'full',
                    'usage_limit'  => null,
                    'metadata'     => [
                        'country' => 'ES',
                        'tier'    => 'premium',
                    ],
                    'observation'  => 'Token asignado a Cliente Alpha para producción.',
                ]
            );
        }
    }
}
