<?php

namespace Database\Seeders;

use App\Models\Store;
use App\Models\StoreGroup;
use Illuminate\Database\Seeder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;

class StoreSeeder extends Seeder
{
    public function run(): void
    {
        $universalOnline = StoreGroup::query()->firstOrCreate(
            ['name' => 'Universal Online'],
            ['is_active' => true],
        );

        $now = Carbon::now();

        // Fuente de verdad estática (Universal / sucursales)
        // Nota: Se preserva la integridad: `tbl_stores.id` debe coincidir con `sucursales.id_sucursal`.
        $sucursales = [
            ['id' => 4, 'name' => 'Avenida Central', 'is_active' => true],
            ['id' => 5, 'name' => 'Curridabat', 'is_active' => true],
            ['id' => 6, 'name' => 'Liberia', 'is_active' => true],
            ['id' => 7, 'name' => 'Heredia', 'is_active' => true],
            ['id' => 8, 'name' => 'Desamparados', 'is_active' => true],
            ['id' => 9, 'name' => 'Lincoln', 'is_active' => true],
            ['id' => 10, 'name' => 'Cartago', 'is_active' => true],
            ['id' => 11, 'name' => 'City', 'is_active' => true],
            ['id' => 12, 'name' => 'Escazú Village', 'is_active' => true],
            ['id' => 13, 'name' => 'Multiplaza Escazú', 'is_active' => true],
            ['id' => 14, 'name' => 'Oxigeno', 'is_active' => true],
        ];

        foreach ($sucursales as $sucursal) {
            DB::table('tbl_stores')->updateOrInsert(
                ['id' => $sucursal['id']],
                [
                    'universal_sucursal_id' => $sucursal['id'],
                    'store_group_id' => $universalOnline->id,
                    'name' => $sucursal['name'],
                    'legal_id' => null,
                    'is_active' => $sucursal['is_active'],
                    'created_at' => $now,
                    'updated_at' => $now,
                ],
            );
        }
    }
}
