<?php
namespace Database\Seeders;

use App\Models\Municipality;
use App\Models\Province;
use App\Models\Store;
use App\Models\StoreHour;
use App\Models\StoreService;
use Illuminate\Database\Seeder;

class StoreSeeder extends Seeder
{
    public function run(): void
    {
        $lua = Province::where('code', 'LUA')->value('id');
        $bgo = Province::where('code', 'BGO')->value('id');
        $belas = Municipality::where('name', 'Belas')->value('id');
        $viana = Municipality::where('name', 'Viana')->value('id');
        $cacuaco = Municipality::where('name', 'Cacuaco')->value('id');
        $dande = Municipality::where('name', 'Dande')->value('id');

        $stores = [
            [
                'name' => 'Mangolê Talatona',
                'type' => 'hiper',
                'province_id' => $lua,
                'municipality_id' => $belas,
                'neighborhood' => 'Talatona',
                'address_line' => 'Av. Avenida Principal, Zona Comercial',
                'reference_point' => 'Ao lado do Shopping Talatona',
                'latitude' => -8.9142, 'longitude' => 13.2001,
                'phone' => '+244 923 100 001', 'whatsapp' => '+244 923 100 001',
                'email' => 'talatona@mwangole.co.ao',
                'is_active' => true, 'status' => 'open', 'sort_order' => 1,
                'hours' => [
                    [0, '08:00', '22:00'], [1, '08:00', '22:00'], [2, '08:00', '22:00'],
                    [3, '08:00', '22:00'], [4, '08:00', '22:00'], [5, '08:00', '22:00'],
                    [6, '09:00', '21:00'],
                ],
                'services' => [
                    ['service' => 'Talho', 'icon' => 'heroicon-o-cube', 'sort_order' => 1],
                    ['service' => 'Padaria', 'icon' => 'heroicon-o-cube', 'sort_order' => 2],
                    ['service' => 'Peixaria', 'icon' => 'heroicon-o-cube', 'sort_order' => 3],
                    ['service' => 'Estacionamento Gratuito', 'icon' => 'heroicon-o-truck', 'sort_order' => 4],
                    ['service' => 'Aceitamos Multicaixa Express', 'icon' => 'heroicon-o-credit-card', 'sort_order' => 5],
                ],
            ],
            [
                'name' => 'Mangolê Viana',
                'type' => 'mix',
                'province_id' => $lua,
                'municipality_id' => $viana,
                'neighborhood' => 'Viana Centro',
                'address_line' => 'Rua do Comércio, 45',
                'latitude' => -8.8983, 'longitude' => 13.3683,
                'phone' => '+244 923 100 002', 'whatsapp' => '+244 923 100 002',
                'is_active' => true, 'status' => 'open', 'sort_order' => 2,
                'hours' => [
                    [0, '08:00', '21:00'], [1, '08:00', '21:00'], [2, '08:00', '21:00'],
                    [3, '08:00', '21:00'], [4, '08:00', '21:00'], [5, '08:00', '21:00'],
                    [6, '09:00', '20:00'],
                ],
                'services' => [
                    ['service' => 'Talho', 'icon' => 'heroicon-o-cube', 'sort_order' => 1],
                    ['service' => 'Padaria', 'icon' => 'heroicon-o-cube', 'sort_order' => 2],
                    ['service' => 'Aceitamos Multicaixa Express', 'icon' => 'heroicon-o-credit-card', 'sort_order' => 3],
                ],
            ],
            [
                'name' => 'Mangolê Cacuaco',
                'type' => 'mix',
                'province_id' => $lua,
                'municipality_id' => $cacuaco,
                'neighborhood' => 'Cacuaco Sul',
                'address_line' => 'Estrada Nacional, Km 12',
                'latitude' => -8.7821, 'longitude' => 13.3776,
                'phone' => '+244 923 100 003',
                'is_active' => true, 'status' => 'renovation', 'sort_order' => 3,
                'hours' => [
                    [0, '08:00', '20:00'], [1, '08:00', '20:00'], [2, '08:00', '20:00'],
                    [3, '08:00', '20:00'], [4, '08:00', '20:00'], [5, '08:00', '20:00'],
                    [6, 'closed', null],
                ],
                'services' => [
                    ['service' => 'Talho', 'icon' => 'heroicon-o-cube', 'sort_order' => 1],
                    ['service' => 'Aceitamos Multicaixa Express', 'icon' => 'heroicon-o-credit-card', 'sort_order' => 2],
                ],
            ],
            [
                'name' => 'Mangolê Dande',
                'type' => 'hiper',
                'province_id' => $bgo,
                'municipality_id' => $dande,
                'neighborhood' => 'Dande Sede',
                'address_line' => 'Av. 4 de Fevereiro, s/n',
                'latitude' => -8.4451, 'longitude' => 13.3022,
                'phone' => '+244 923 100 004', 'whatsapp' => '+244 923 100 004',
                'email' => 'dande@mwangole.co.ao',
                'is_active' => true, 'status' => 'open', 'sort_order' => 4,
                'hours' => [
                    [0, '07:30', '21:30'], [1, '07:30', '21:30'], [2, '07:30', '21:30'],
                    [3, '07:30', '21:30'], [4, '07:30', '21:30'], [5, '07:30', '21:30'],
                    [6, '08:00', '20:00'],
                ],
                'services' => [
                    ['service' => 'Talho', 'icon' => 'heroicon-o-cube', 'sort_order' => 1],
                    ['service' => 'Padaria', 'icon' => 'heroicon-o-cube', 'sort_order' => 2],
                    ['service' => 'Peixaria', 'icon' => 'heroicon-o-cube', 'sort_order' => 3],
                    ['service' => 'Zona de Refeições', 'icon' => 'heroicon-o-cube', 'sort_order' => 4],
                    ['service' => 'Estacionamento Gratuito', 'icon' => 'heroicon-o-truck', 'sort_order' => 5],
                    ['service' => 'Aceitamos Multicaixa Express', 'icon' => 'heroicon-o-credit-card', 'sort_order' => 6],
                ],
            ],
        ];

        foreach ($stores as $data) {
            $hours = $data['hours'] ?? [];
            $services = $data['services'] ?? [];
            unset($data['hours'], $data['services']);

            $store = Store::firstOrCreate(
                ['slug' => str($data['name'])->slug()],
                $data
            );

            if (!$store->wasRecentlyCreated) continue;

            foreach ($hours as [$day, $open, $close]) {
                if ($open === 'closed') {
                    StoreHour::create([
                        'store_id' => $store->id,
                        'day_of_week' => $day,
                        'is_closed' => true,
                    ]);
                } else {
                    StoreHour::create([
                        'store_id' => $store->id,
                        'day_of_week' => $day,
                        'opens_at' => $open,
                        'closes_at' => $close,
                    ]);
                }
            }

            foreach ($services as $svc) {
                StoreService::create([
                    'store_id' => $store->id,
                    'service' => $svc['service'],
                    'icon' => $svc['icon'],
                    'sort_order' => $svc['sort_order'],
                ]);
            }
        }
    }
}
