<?php

namespace Database\Seeders;

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

class RoleSeeder extends Seeder
{
    public function run(): void
    {
        app(PermissionRegistrar::class)->forgetCachedPermissions();

        $permissions = [
            // patients & clinical
            'patients.view', 'patients.manage',
            'chart.view', 'chart.manage',
            'treatment-plans.view', 'treatment-plans.manage',
            'files.view', 'files.manage',
            // scheduling
            'appointments.view', 'appointments.manage',
            'recalls.view', 'recalls.manage',
            // money
            'invoices.view', 'invoices.manage',
            'installments.view', 'installments.manage',
            'reports.view',
            // clinic administration
            'staff.manage', 'settings.manage', 'billing.manage',
        ];

        foreach ($permissions as $permission) {
            Permission::findOrCreate($permission, 'web');
        }

        Role::findOrCreate('super_admin', 'web'); // gate-bypassed in AppServiceProvider

        Role::findOrCreate('clinic_owner', 'web')
            ->syncPermissions($permissions);

        Role::findOrCreate('dentist', 'web')->syncPermissions([
            'patients.view', 'patients.manage',
            'chart.view', 'chart.manage',
            'treatment-plans.view', 'treatment-plans.manage',
            'files.view', 'files.manage',
            'appointments.view', 'appointments.manage',
            'recalls.view', 'recalls.manage',
            'invoices.view', 'reports.view',
        ]);

        Role::findOrCreate('receptionist', 'web')->syncPermissions([
            'patients.view', 'patients.manage',
            'appointments.view', 'appointments.manage',
            'recalls.view', 'recalls.manage',
            'invoices.view', 'invoices.manage',
            'installments.view', 'installments.manage',
            'files.view',
        ]);
    }
}
