<?php

namespace Database\Seeders;

use App\Addons\Services\AddonLoader;
use App\Core\Services\PermissionService;
use App\Core\Services\PluginService;
use App\Enums\RoleEnum;
use App\Models\Role;
use Illuminate\Database\Seeder;

class RolePermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $permissionService = new PermissionService(new PluginService(new AddonLoader));

        // Sync all permissions to database
        $permissionService->syncPermissions();

        // Create system roles
        $this->createSystemRoles();

        // Assign default permissions
        $permissionService->assignDefaultPermissions();
    }

    /**
     * Create system roles
     */
    private function createSystemRoles(): void
    {
        $roles = [
            [
                'name' => RoleEnum::CUSTOMER->displayName(),
                'slug' => RoleEnum::CUSTOMER->value,
                'description' => RoleEnum::CUSTOMER->description(),
                'is_active' => true,
            ],
            [
                'name' => RoleEnum::SUPER_ADMIN->displayName(),
                'slug' => RoleEnum::SUPER_ADMIN->value,
                'description' => RoleEnum::SUPER_ADMIN->description(),
                'is_active' => true,
            ],
            [
                'name' => RoleEnum::ADMINISTRATION->displayName(),
                'slug' => RoleEnum::ADMINISTRATION->value,
                'description' => RoleEnum::ADMINISTRATION->description(),
                'is_active' => true,
            ],
            [
                'name' => RoleEnum::SALES_MANAGER->displayName(),
                'slug' => RoleEnum::SALES_MANAGER->value,
                'description' => RoleEnum::SALES_MANAGER->description(),
                'is_active' => true,
            ],
        ];

        foreach ($roles as $roleData) {
            Role::firstOrCreate(
                ['slug' => $roleData['slug']],
                $roleData
            );
        }
    }
}
