<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Products\LicenseToken;
use App\Models\Products\Product;
use Illuminate\Support\Facades\DB;

class LicenseTokenProductSeeder extends Seeder
{
    public function run(): void
    {
        // --------------------------------------------
        // CLIENTE: 3ipunt-demo
        // --------------------------------------------
        $tokenDemo = LicenseToken::where('token', 'LIKE', 'DEMO-%')->first();
        $tokenTrial = LicenseToken::where('token', 'LIKE', 'TRIAL-%')->first();

        $products = Product::all()->keyBy('slug');

        if ($tokenDemo) {
            // Token FULL → habilitar TODOS los productos
            foreach ($products as $product) {
                DB::table('license_token_product')->updateOrInsert(
                    [
                        'license_token_id' => $tokenDemo->id,
                        'product_id'       => $product->id,
                    ],
                    [
                        'mode'        => 'full',
                        'status'      => 'active',
                        'start_at'    => now()->subMonth(),
                        'end_at'      => now()->addMonths(6),
                        'observation' => 'Asignado automáticamente en seeder (token full demo).',
                        'assigned_by' => null,
                        'created_at'  => now(),
                        'updated_at'  => now(),
                    ]
                );
            }
        }

        if ($tokenTrial) {
            // Token TRIAL → habilitar solo algunos plugins
            foreach (['theme_fresk', 'block_tresipuntsepe'] as $slug) {
                if (isset($products[$slug])) {
                    DB::table('license_token_product')->updateOrInsert(
                        [
                            'license_token_id' => $tokenTrial->id,
                            'product_id'       => $products[$slug]->id,
                        ],
                        [
                            'mode'        => 'trial',
                            'status'      => 'active',
                            'start_at'    => now(),
                            'end_at'      => now()->addDays(14),
                            'observation' => 'Acceso limitado para demo comercial.',
                            'assigned_by' => null,
                            'created_at'  => now(),
                            'updated_at'  => now(),
                        ]
                    );
                }
            }
        }

        // --------------------------------------------
        // CLIENTE: client-alpha
        // --------------------------------------------
        $tokenAlpha = LicenseToken::where('token', 'LIKE', 'ALPHA-%')->first();

        if ($tokenAlpha) {
            // Alpha tiene acceso a todos excepto courseai
            foreach ($products as $slug => $product) {
                if ($slug === 'local_courseai') {
                    continue;
                }

                DB::table('license_token_product')->updateOrInsert(
                    [
                        'license_token_id' => $tokenAlpha->id,
                        'product_id'       => $product->id,
                    ],
                    [
                        'mode'        => 'full',
                        'status'      => 'active',
                        'start_at'    => now()->subDays(10),
                        'end_at'      => now()->addYear(),
                        'observation' => 'Licencia completa (excepto AI).',
                        'assigned_by' => null,
                        'created_at'  => now(),
                        'updated_at'  => now(),
                    ]
                );
            }
        }
    }
}
