<?php

namespace Database\Seeders;

use App\Models\TopologyConnectionRule;
use App\Models\TopologyDeviceType;
use Illuminate\Database\Seeder;

class TopologySeeder extends Seeder
{
    public function run(): void
    {
        $definitions = [
            ['code' => 'POP', 'name' => 'Point of Presence', 'category' => 'site', 'color' => '#dc3545'],
            ['code' => 'OTB', 'name' => 'Optical Termination Box', 'category' => 'termination', 'color' => '#fd7e14'],
            ['code' => 'JB', 'name' => 'Joint Box', 'category' => 'closure', 'color' => '#6f42c1'],
            ['code' => 'ODC', 'name' => 'Optical Distribution Cabinet', 'category' => 'distribution', 'color' => '#0d6efd'],
            ['code' => 'ODP', 'name' => 'Optical Distribution Point', 'category' => 'distribution', 'color' => '#198754'],
            ['code' => 'SPLITTER', 'name' => 'Splitter / Ratio', 'category' => 'splitter', 'color' => '#20c997', 'can_have_splitters' => true],
        ];

        $types = collect($definitions)->mapWithKeys(function ($definition) {
            $type = TopologyDeviceType::updateOrCreate(
                ['tenant_id' => null, 'code' => $definition['code']],
                [
                    ...$definition,
                    'tenant_id' => null,
                    'can_have_ports' => true,
                    'can_have_splitters' => $definition['can_have_splitters'] ?? false,
                    'is_system' => true,
                    'icon' => 'cil-location-pin',
                    'status' => 'active',
                ],
            );

            return [$type->code => $type];
        });

        foreach ([
            ['POP', 'POP'], ['POP', 'OTB'], ['OTB', 'JB'], ['OTB', 'ODC'],
            ['OTB', 'SPLITTER'], ['JB', 'ODC'], ['JB', 'ODP'], ['ODC', 'ODP'],
            ['ODC', 'SPLITTER'], ['SPLITTER', 'ODP'],
        ] as [$source, $target]) {
            TopologyConnectionRule::updateOrCreate(
                [
                    'tenant_id' => null,
                    'source_device_type_id' => $types[$source]->id,
                    'target_device_type_id' => $types[$target]->id,
                ],
                ['allowed' => true],
            );
        }
    }
}
