<?php

namespace Modules\Auth\Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Modules\Auth\Models\Role;
use Modules\Auth\Models\Permission;
use Modules\Core\Enums\RolesEnum;
use Modules\Core\Traits\HasModulePermissions;

class PermissionAndRoleSeeder extends Seeder
{
    use HasModulePermissions;

    public function run(): void
    {
        DB::transaction(function () {

            $roles = [];
            $order = 1;

            foreach (RolesEnum::cases() as $enum) {
                $roles[$enum->value] = Role::updateOrCreate(
                    [
                        'name' => $enum->value,
                        'guard_name' => 'api',
                    ],
                    [
                        'title' => $enum->label(),
                        'sorting_index' => $order++,
                    ]
                );
            }

            // ------------------
            // Auth Permissions
            // ------------------
            $this->seedPermissions('auth', [
                'dashboard' => ['view'],

                'users' => [
                    'view',
                    'create',
                    'update',
                    'delete',
                    'status',
                    'update-roles',
                ],

                'roles' => [
                    'view',
                    'assign',
                ],

                'permissions' => [
                    'view',
                    'manage',
                ],

                'profile' => [
                    'view',
                    'update',
                ],
            ]);

            // ------------------
            // Role → Permission mapping
            // ------------------

            // Admin
            $roles[RolesEnum::ADMIN->value]->syncPermissions([
                'auth.dashboard.view',
                'auth.users.view',
                'auth.users.create',
                'auth.users.update',
                'auth.users.update-roles',
                'auth.users.status',
                'auth.roles.view',
                'auth.roles.assign',
                'auth.permissions.view',
                'auth.profile.view',
                'auth.profile.update',
            ]);

            // HR
            $roles[RolesEnum::HR->value]->syncPermissions([
                'auth.dashboard.view',
                'auth.users.view',
                'auth.users.create',
                'auth.users.update',
                'auth.users.status',
                'auth.roles.view',
                'auth.profile.view',
                'auth.profile.update',
            ]);

            foreach ([
                RolesEnum::ED,
                RolesEnum::MANAGER,
                RolesEnum::ACCOUNTS,
                RolesEnum::INVENTORY,
                RolesEnum::BILLINGS,
                RolesEnum::EMPLOYEES,
            ] as $role) {
                $roles[$role->value]->syncPermissions([
                    'auth.dashboard.view',
                    'auth.profile.view',
                    'auth.profile.update',
                ]);
            }
        });
    }
}

