<?php

namespace Database\Seeders;

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

class RoleSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 创建或获取角色
        $admin = Role::firstOrCreate(['name' => 'admin']);
        $staff = Role::firstOrCreate(['name' => 'staff']);
        $shareholder = Role::firstOrCreate(['name' => 'shareholder']);

        // 创建或获取权限
        $permissions = [
            'view_users',
            'create_users',
            'edit_users',
            'delete_users',
            'view_credits',
            'create_credits',
            'edit_credits',
            'delete_credits',
            'approve_credits',
        ];

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

        // 为管理员分配所有权限
        $admin->syncPermissions(Permission::all());
        
        // 为员工分配特定权限
        $staff->syncPermissions([
            'view_users',
            'view_credits',
            'create_credits',
        ]);
        
        // 为股东分配查看权限和审批权限
        $shareholder->syncPermissions([
            'view_users',
            'view_credits',
            'approve_credits',
        ]);
    }
} 