<?php

namespace Modules\Dummy\Database\Seeders\Modules\AiSocialSuites\versions\v6_9_0;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class MenuItemsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::transaction(function () {
            // 1. Create/Find AI Social Suites Menu as top-level menu (separate, not under AI Characters)
            $existingParent = DB::table('menu_items')
                ->where('label', 'AI Social Suites')
                ->where('menu', 1)
                ->where('parent', 0)
                ->first();

            if ($existingParent) {
                $aiSocialSuitesId = $existingParent->id;
            } else {
                $aiSocialSuitesId = DB::table('menu_items')->insertGetId([
                    'label' => 'AI Social Suites',
                    'link' => NULL,
                    'params' => '{"permission":"Modules\\\\AiSocialSuites\\\\Http\\\\Controllers\\\\Admin\\\\AiSocialSuitesController@accounts","route_name":["admin.ai-social-suites.accounts"]}',
                    'is_default' => 0,
                    'icon' => 'fas fa-share-alt',
                    'parent' => 0, // Top-level menu, not under AI Characters
                    'sort' => 20, 
                    'class' => NULL,
                    'menu' => 1,
                    'depth' => 0,
                    'is_custom_menu' => 0,
                    'created_at' => now(),
                    'updated_at' => now(),
                ]);
            }

            // 2. Create Social Accounts Submenu
            if (!DB::table('menu_items')->where('label', 'Social Accounts')->where('parent', $aiSocialSuitesId)->exists()) {
                DB::table('menu_items')->insert([
                    'label' => 'Social Accounts',
                    'link' => 'ai-social-suites/accounts',
                    'params' => '{"permission":"Modules\\\\AiSocialSuites\\\\Http\\\\Controllers\\\\Admin\\\\AiSocialSuitesController@accounts","route_name":["admin.ai-social-suites.accounts"]}',
                    'is_default' => 1,
                    'icon' => '',
                    'parent' => $aiSocialSuitesId,
                    'sort' => 1,
                    'class' => NULL,
                    'menu' => 1,
                    'depth' => 1,
                    'is_custom_menu' => 0,
                    'created_at' => now(),
                    'updated_at' => now(),
                ]);
            }

            // 3. Create Social Posts Submenu
            if (!DB::table('menu_items')->where('label', 'Social Posts')->where('parent', $aiSocialSuitesId)->exists()) {
                DB::table('menu_items')->insert([
                    'label' => 'Social Posts',
                    'link' => 'ai-social-suites/posts',
                    'params' => '{"permission":"Modules\\\\AiSocialSuites\\\\Http\\\\Controllers\\\\Admin\\\\AiSocialSuitesController@posts","route_name":["admin.ai-social-suites.posts"]}',
                    'is_default' => 1,
                    'icon' => NULL,
                    'parent' => $aiSocialSuitesId,
                    'sort' => 2,
                    'class' => NULL,
                    'menu' => 1,
                    'depth' => 1,
                    'is_custom_menu' => 0,
                    'created_at' => now(),
                    'updated_at' => now(),
                ]);
            }
        });
    }
}
