<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\System\Role;
use Spatie\Permission\Models\Permission;
use Illuminate\Support\Facades\Schema;

class RoleSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 重設快取
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        // 建立權限
        $permissions = [
            'menu.members',
            'menu.machines',
            'menu.machines.list',
            'menu.machines.permissions',
            'menu.machines.utilization',
            'menu.machines.maintenance',
            'menu.app',
            'menu.warehouses',
            'menu.sales',
            'menu.analysis',
            'menu.audit',
            'menu.data-config',
            'menu.data-config.products',
            'menu.data-config.advertisements',
            'menu.data-config.sub-accounts',
            'menu.data-config.points',
            'menu.data-config.badges',
            'menu.remote',
            'menu.line',
            'menu.reservation',
            'menu.special-permission',
            'menu.basic-settings',
            'menu.basic.machines',
            'menu.basic.payment-configs',
            'menu.permissions',
            'menu.permissions.companies',
            'menu.permissions.accounts',
            'menu.permissions.roles',
        ];

        foreach ($permissions as $permission) {
            Permission::updateOrCreate(['name' => $permission, 'guard_name' => 'web']);
        }

        // 建立角色
        $superAdmin = Role::updateOrCreate(
            ['name' => 'super-admin', 'company_id' => null],
            ['is_system' => true, 'guard_name' => 'web']
        );
        $superAdmin->syncPermissions(Permission::all());

        $tenantAdmin = Role::updateOrCreate(
            ['name' => '客戶管理員角色模板', 'company_id' => null],
            ['is_system' => true, 'guard_name' => 'web']
        );
        $tenantAdmin->syncPermissions([
            'menu.members',
            'menu.machines',
            'menu.machines.list',
            'menu.machines.permissions',
            'menu.machines.utilization',
            'menu.machines.maintenance',
            'menu.app',
            'menu.warehouses',
            'menu.sales',
            'menu.analysis',
            'menu.audit',
            'menu.data-config',
            'menu.data-config.products',
            'menu.data-config.advertisements',
            'menu.data-config.sub-accounts',
            'menu.data-config.points',
            'menu.data-config.badges',
            'menu.remote',
            'menu.line',
            'menu.reservation',
            'menu.special-permission',
        ]);
    }
}