<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Domains\Menu\Models\Menu;
use App\Domains\Tenant\Models\Tenant;

class MenuSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $menus = [
            ['code' => 'index', 'route' => '/'],
            ['code' => 'product.detail', 'route' => '/product/:id'],
            ['code' => 'checkout', 'route' => '/checkout'],
            ['code' => 'profile', 'route' => '/profile'],
            ['code' => 'purchases', 'route' => '/purchases'],
            ['code' => 'tickets', 'route' => '/tickets'],
        ];

        foreach ($menus as $menuData) {
            Menu::firstOrCreate(['code' => $menuData['code']], $menuData);
        }

        $tenants = Tenant::all();

        $allMenus = Menu::pluck('code')->toArray();

        foreach ($tenants as $tenant) {
            $menuCodes = $allMenus;

            if ($tenant->codigo === 'sonder') {
                // Sonder NO tiene tickets
                $menuCodes = array_diff($menuCodes, ['tickets']);
            } else {
                // Los demás NO tienen product.detail
                $menuCodes = array_diff($menuCodes, ['product.detail']);
            }

            // Usar sync para asociar los menues al tenant
            $tenant->menues()->sync($menuCodes);
        }
    }
}
