<?php

use App\Models\Module;
use App\Models\Permission;
use App\Models\Role;
use App\Models\RoleModules;
use App\Models\RolePermission;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // 1. Ensure Phone Book parent module exists
        $phoneBookModule = Module::where('name', 'phone_book')->orWhere('link', 'LIKE', '%phone_book%')->first();
        $phoneBookId = $phoneBookModule ? $phoneBookModule->id : 30;

        // 2. Add Numbers module under Phone Book if not already present
        $numbersModule = Module::where('name', 'number_list')->orWhere('link', '/admin/numbers')->first();
        if (!$numbersModule) {
            $numbersModule = Module::create([
                'display_name' => 'Number List',
                'name' => 'number_list',
                'link' => '/admin/numbers',
                'icon' => 'fa-list-ol',
                'parent_id' => $phoneBookId,
                'status' => 1,
            ]);
        }

        // Add permissions for Number List
        $numberPermissions = ['view', 'add', 'update', 'delete'];
        foreach ($numberPermissions as $perm) {
            $permName = 'number_list_' . $perm;
            $existingPerm = Permission::where('name', $permName)->first();
            if (!$existingPerm) {
                $existingPerm = Permission::create([
                    'module_id' => $numbersModule->id,
                    'name' => $permName,
                    'display_name' => 'Number List ' . ucfirst($perm),
                    'status' => 1,
                ]);
            }

            // Assign to all roles
            $roles = Role::all();
            foreach ($roles as $role) {
                if (!RolePermission::where('role_id', $role->id)->where('permission_id', $existingPerm->id)->exists()) {
                    RolePermission::create([
                        'role_id' => $role->id,
                        'permission_id' => $existingPerm->id,
                    ]);
                }
            }
        }

        // Assign Number module to all roles
        $roles = Role::all();
        foreach ($roles as $role) {
            if (!RoleModules::where('role_id', $role->id)->where('module_id', $numbersModule->id)->exists()) {
                RoleModules::create([
                    'role_id' => $role->id,
                    'module_id' => $numbersModule->id,
                ]);
            }
        }

        // 3. Ensure SMS Queue module is registered under Messaging (parent_id: 16)
        $messagingModule = Module::where('name', 'messaging')->first();
        $messagingId = $messagingModule ? $messagingModule->id : 16;

        $smsQueueModule = Module::where('name', 'sms_messages')->orWhere('link', '/admin/sms_messages')->first();
        if (!$smsQueueModule) {
            $smsQueueModule = Module::create([
                'display_name' => 'SMS Queue',
                'name' => 'sms_messages',
                'link' => '/admin/sms_messages',
                'icon' => 'fa-clock',
                'parent_id' => $messagingId,
                'status' => 1,
            ]);
        }

        $smsQueuePermissions = ['view', 'add', 'update', 'delete'];
        foreach ($smsQueuePermissions as $perm) {
            $permName = 'sms_messages_' . $perm;
            $existingPerm = Permission::where('name', $permName)->first();
            if (!$existingPerm) {
                $existingPerm = Permission::create([
                    'module_id' => $smsQueueModule->id,
                    'name' => $permName,
                    'display_name' => 'SMS Queue ' . ucfirst($perm),
                    'status' => 1,
                ]);
            }

            foreach ($roles as $role) {
                if (!RolePermission::where('role_id', $role->id)->where('permission_id', $existingPerm->id)->exists()) {
                    RolePermission::create([
                        'role_id' => $role->id,
                        'permission_id' => $existingPerm->id,
                    ]);
                }
            }
        }

        foreach ($roles as $role) {
            if (!RoleModules::where('role_id', $role->id)->where('module_id', $smsQueueModule->id)->exists()) {
                RoleModules::create([
                    'role_id' => $role->id,
                    'module_id' => $smsQueueModule->id,
                ]);
            }
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        $numbersModule = Module::where('name', 'number_list')->first();
        if ($numbersModule) {
            Permission::where('module_id', $numbersModule->id)->delete();
            RoleModules::where('module_id', $numbersModule->id)->delete();
            $numbersModule->delete();
        }

        $smsQueueModule = Module::where('name', 'sms_messages')->first();
        if ($smsQueueModule) {
            Permission::where('module_id', $smsQueueModule->id)->delete();
            RoleModules::where('module_id', $smsQueueModule->id)->delete();
            $smsQueueModule->delete();
        }
    }
};
