<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

class FinancePermissionSeeder extends Seeder
{
    public function run(): void
    {
        // 建立新權限
        $permissions = [
            'utility_fees.view',
            'utility_fees.create',
            'utility_fees.edit',
            'utility_fees.delete',
            'accounting.view',
        ];

        foreach ($permissions as $permission) {
            Permission::firstOrCreate(['name' => $permission]);
        }

        // 分配權限給現有角色
        
        // Super Admin 獲得所有
        $superAdmin = Role::where('name', 'super-admin')->first();
        if ($superAdmin) {
            $superAdmin->givePermissionTo($permissions);
        }

        // Admin 獲得所有
        $admin = Role::where('name', 'admin')->first();
        if ($admin) {
            $admin->givePermissionTo($permissions);
        }

        // Viewer 獲得檢視權限
        $viewer = Role::where('name', 'viewer')->first();
        if ($viewer) {
            $viewer->givePermissionTo([
                'utility_fees.view',
                'accounting.view',
            ]);
        }
    }
}
