<?php

namespace Database\Seeders;

use App\Models\Environments\Environment;
use App\Models\Support\Support;
use Illuminate\Database\Seeder;

class EnvironmentSeeder extends Seeder
{
    public function run(): void
    {
        // Obtenemos soportes existentes (ID dinámico, no hardcodeado)
        $basic = Support::where('shortname', 'basic')->first();
        $premium = Support::where('shortname', 'premium')->first();

        // Creamos primer entorno
        $environment1 = Environment::create([
            'name' => 'Prueba',
            'domain' => 'http://prueba.test/',
            'description' => 'Entorno de prueba',
            'env' => 'local',
            'version' => '3.12',
            'type_id' => 1,
            'client_id' => 1,
            'has_support' => true,
            'lastversion' => '3.12.3',
            'lastminor' => '3.12.3',
            'active' => true,
            'key' => 'TIPMANAGER',
        ]);

        // Asignamos soporte actual
        $environment1->supports()->attach($basic->id, [
            'comment' => 'Soporte activo',
            'start_at' => '2022-01-10',
            'end_at' => '2023-03-10',
        ]);

        // Actualizamos FK del soporte actual
        $environment1->update(['environment_support_id' => $environment1->supports()->latest()->first()->pivot->id]);

        // Creamos un soporte antiguo histórico
        $environment1->supports()->attach($premium->id, [
            'comment' => 'Soporte caducado',
            'start_at' => '2020-01-10',
            'end_at' => '2021-07-10',
        ]);

        // Segundo entorno
        $environment2 = Environment::create([
            'name' => 'Prueba2',
            'domain' => 'http://prueba2.test/',
            'description' => 'Entorno de prueba 2',
            'env' => 'pre',
            'version' => '3.11',
            'type_id' => 1,
            'client_id' => 2,
            'has_support' => true,
            'lastversion' => '3.12',
            'lastminor' => '3.11.3',
            'active' => false,
            'key' => 'TEST-12343243',
        ]);

        // soporte para el environment2
        $environment2->supports()->attach($premium->id, [
            'comment' => 'Soporte caducado',
            'start_at' => '2021-03-10',
            'end_at' => '2022-02-10',
        ]);

        $environment2->update(['environment_support_id' => $environment2->supports()->latest()->first()->pivot->id]);
    }
}
