<?php

namespace Database\Seeders;

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

class RoleHasPermissionsSeeder extends Seeder
{
    public function run(): void
    {
        $json = file_get_contents(database_path('seeders/json/role_has_permissions_20251014.json'));
        $data = json_decode($json, true);

        foreach ($data['role_has_permissions'] as $item) {
            $guardName = $item['guard_name'] ?? 'api';

            $permission = DB::table('permissions')
                ->where('name', $item['permission_name'])
                ->where('guard_name', $guardName)
                ->first();

            $role = DB::table('roles')
                ->where('name', $item['role_name'])
                ->where('guard_name', $guardName)
                ->first();

            DB::table('role_has_permissions')->insert([
                'permission_id' => $permission->id,
                'role_id' => $role->id,
            ]);
        }
    }
}
