<?php

namespace Database\Seeders;

use App\Enums\MenuLocationEnum;
use App\Models\Menu;
use App\Models\MenuItem;
use Illuminate\Database\Seeder;

class MenuSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $menus = [
            [
                'name' => 'Quick Links',
                'slug' => 'quick-links',
                'location' => MenuLocationEnum::QUICK_LINKS,
                'pages' => ['home', 'about-us', 'contact-us', 'login', 'register', 'privacy-policy', 'terms-and-conditions'], // page slugs for footer menu
            ],
            [
                'name' => 'Resources Menu',
                'slug' => 'resources-menu',
                'location' => MenuLocationEnum::RESOURCES_MENU,
                'pages' => ['confidentiality-and-privacy', 'return-and-refund-policy', 'legal-information'], // page slugs for footer bottom menu
            ],
        ];
        $pages = \App\Models\Page::get();

        foreach ($menus as $menuItem) {
            $menu = Menu::create([
                'name' => $menuItem['name'],
                'slug' => $menuItem['slug'],
                'location' => $menuItem['location'],
            ]);
            foreach ($pages as $key => $page) {
                if (! in_array($page->slug, $menuItem['pages'])) {
                    continue;
                }
                MenuItem::create([
                    'title' => $page->title,
                    'menu_id' => $menu->id,
                    'page_id' => $page->id,
                    'parent_id' => null,
                    'order' => $key,
                    'is_primary' => 0,
                    'target' => '_self',
                    'url' => null,
                ]);
            }
        }

        // Create or update the header menu
        $headerMenu = Menu::updateOrCreate(
            ['slug' => 'header-menu'],
            [
                'name' => 'Main Menu',
                'location' => MenuLocationEnum::HEADER,
                'status' => 'active',
            ]
        );

        // Clear existing menu items if any
        MenuItem::where('menu_id', $headerMenu->id)->delete();
        // Define the menu structure
        $menuItems = [

            [
                'title' => 'Quiz',
                'url' => '/quizzes',
                'order' => 1,

            ],
            [
                'title' => 'Contest',
                'url' => '/contests',
                'order' => 2,

            ],
            [
                'title' => 'About Us',
                'url' => '/about-us',
                'order' => 3,
            ],
            [
                'title' => 'Contact Us',
                'url' => '/contact-us',
                'order' => 4,
            ],
            [
                'title' => 'Games',
                'order' => 5,
                'children' => [
                    [
                        'title' => 'Wordling',
                        'url' => '/games/wordling',
                        'order' => 1,
                    ],
                    [
                        'title' => 'Wordling Min',
                        'url' => '/games/wordling-min',
                        'order' => 2,
                    ],
                    [
                        'title' => 'Wordling Max',
                        'url' => '/games/wordling-max',
                        'order' => 3,
                    ],
                    [
                        'title' => 'Hexling',
                        'url' => '/games/hexling',
                        'order' => 4,
                    ],
                ],
            ],

        ];
        // Create menu items
        foreach ($menuItems as $item) {
            $this->createMenuItem($headerMenu->id, $item);
        }

    }

    private function createMenuItem(int $menuId, array $item, ?int $parentId = null): void
    {
        $page = null;
        if (isset($item['url'])) {
            // Try to find a page with a matching slug from the URL
            $slug = trim(parse_url($item['url'], PHP_URL_PATH), '/');
            $page = \App\Models\Page::where('slug', $slug)->first();
        }
        $menuItem = MenuItem::create([
            'menu_id' => $menuId,
            'parent_id' => $parentId,
            'page_id' => $page ? $page->id : null,
            'title' => $item['title'],
            'url' => $item['url'] ?? null,
            'order' => $item['order'],
            'target' => '_self',
        ]);
        if (! empty($item['children'])) {
            foreach ($item['children'] as $child) {
                $this->createMenuItem($menuId, $child, $menuItem->id);
            }
        }
    }
}
