<?php

namespace Database\Seeders;

use App\Models\Module;
use App\Models\Role;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class RoleModuleSeeder extends Seeder
{
    public function run(): void
    {
        $superadmin  = Role::where('name', 'Superadmin')->first();
        $aofsMgmt    = Role::where('name', 'AOFS Management')->first();

        $portal      = Module::where('slug', 'portal')->first();
        $account     = Module::where('slug', 'accountlayer')->first();
        $casefile    = Module::where('slug', 'casefile')->first();

        if ($aofsMgmt && $account) {
            DB::table('role_module')->updateOrInsert(
                ['role_id' => $aofsMgmt->id, 'module_id' => $account->id],
                []
            );
        }

        if ($aofsMgmt && $casefile) {
            DB::table('role_module')->updateOrInsert(
                ['role_id' => $aofsMgmt->id, 'module_id' => $casefile->id],
                []
            );
        }

        if ($superadmin && $portal) {
            DB::table('role_module')->updateOrInsert(
                ['role_id' => $superadmin->id, 'module_id' => $portal->id],
                []
            );
        }
    }
}

