<?php

namespace Database\Seeders;

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

class SynapsePermissionSeeder extends Seeder
{
    public function run(): void
    {
        $permission = Permission::firstOrCreate(
            ['name' => 'synapse.use', 'guard_name' => 'web'],
            [
                'group'       => 'Synapse',
                'label'       => 'Use Synapse AI agents',
                'description' => 'Access Synapse AI features including DSAR analysis and document review',
            ]
        );

        // Grant to any role that already has 'documents.upload'
        $roles = Role::whereHas('permissions', function ($q) {
            $q->where('name', 'documents.upload');
        })->get();

        foreach ($roles as $role) {
            $role->givePermissionTo($permission);
        }

        $this->command->info('Synapse permission created and assigned to ' . $roles->count() . ' role(s).');
    }
}
