<?php

namespace Modules\VetClinic\Services;

use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

/**
 * Out-of-scope follow-up — per-business default-role seeding.
 *
 * The 2026_05_31_000009 migration covered every business present at
 * upgrade time. NEW businesses created later (via Superadmin's "Add
 * Business" flow or the standard sign-up) need the same six veterinary
 * roles automatically. The Business::created observer
 * (VcBusinessObserver) calls seedForBusiness($newBusinessId).
 *
 * This service holds the role-permission map ONCE so the migration and
 * the observer never drift. If you add a new default role or a new
 * permission to an existing role, update PRESETS here and re-run the
 * migration on existing installs.
 */
class DefaultRolesSeeder
{
    /** Role -> permission-name array. Single source of truth. */
    public const PRESETS = [
        'Receptionist' => [
            'vet_clinic.access_module', 'vet_clinic.manage_pet_owners', 'vet_clinic.manage_pets',
            'vet_clinic.view_appointments', 'vet_clinic.manage_appointments',
            'vet_clinic.view_owner_blocking',
            'customer.view', 'customer.create',
        ],
        'Doctor' => [
            'vet_clinic.access_module', 'vet_clinic.manage_pet_owners', 'vet_clinic.manage_pets',
            'vet_clinic.view_appointments', 'vet_clinic.manage_appointments',
            'vet_clinic.change_appointment_doctor',
            'vet_clinic.manage_prescriptions', 'vet_clinic.dispense_prescriptions',
            'vet_clinic.manage_vaccinations',
            'vet_clinic.manage_reports', 'vet_clinic.view_reports',
            'vet_clinic.view_critical_report',
            'vet_clinic.record_exam_findings', 'vet_clinic.view_exam_findings',
            'vet_clinic.evaluate_pet_owner',
            'vet_clinic.manage_subject_notes', 'vet_clinic.view_confidential_notes',
            'vet_clinic.manage_controlled',
            'vet_ai.access_module', 'vet_ai.use_assistant', 'vet_ai.use_transcription',
        ],
        'Nurse' => [
            'vet_clinic.access_module', 'vet_clinic.manage_pet_owners', 'vet_clinic.manage_pets',
            'vet_clinic.view_appointments', 'vet_clinic.manage_appointments',
            'vet_clinic.manage_vaccinations',
            'vet_clinic.record_exam_findings', 'vet_clinic.view_exam_findings',
            'vet_clinic.manage_subject_notes',
            'vet_ai.use_transcription',
        ],
        'Assistant' => [
            'vet_clinic.access_module', 'vet_clinic.view_appointments',
            'vet_clinic.manage_pets', 'vet_clinic.manage_pet_owners',
            'vet_clinic.view_exam_findings',
        ],
        'Branch Manager' => [
            'vet_clinic.access_module', 'vet_clinic.manage_pet_owners', 'vet_clinic.manage_pets',
            'vet_clinic.view_appointments', 'vet_clinic.manage_appointments',
            'vet_clinic.change_appointment_doctor',
            'vet_clinic.manage_prescriptions', 'vet_clinic.dispense_prescriptions',
            'vet_clinic.manage_vaccinations', 'vet_clinic.manage_medicines',
            'vet_clinic.manage_reports', 'vet_clinic.view_reports',
            'vet_clinic.view_critical_report',
            'vet_clinic.manage_exam_templates', 'vet_clinic.record_exam_findings', 'vet_clinic.view_exam_findings',
            'vet_clinic.manage_owner_blocking', 'vet_clinic.view_owner_blocking',
            'vet_clinic.manage_evaluation_questions', 'vet_clinic.review_evaluations',
            'vet_clinic.evaluate_pet_owner', 'vet_clinic.evaluate_doctor',
            'vet_clinic.approve_edit_requests', 'vet_clinic.approve_doctor_change',
            'vet_clinic.manage_subject_notes', 'vet_clinic.view_confidential_notes',
            'vet_clinic.manage_controlled', 'vet_clinic.manage_settings',
            'vet_ai.access_module', 'vet_ai.use_assistant', 'vet_ai.manage_settings',
        ],
        'Accounting' => [
            'vet_clinic.access_module', 'vet_clinic.view_appointments',
            'vet_clinic.view_owner_blocking', 'vet_clinic.manage_owner_blocking',
            'vet_clinic.view_reports', 'vet_clinic.view_critical_report',
            'customer.view',
        ],
    ];

    /**
     * Seed all six default roles for one business. Idempotent.
     * Returns the count of roles created (existing rows skipped).
     */
    public static function seedForBusiness(int $businessId): int
    {
        $created = 0;
        foreach (self::PRESETS as $roleBase => $permList) {
            $roleName = $roleBase . '#' . $businessId;
            $role = Role::firstOrCreate(
                ['name' => $roleName, 'guard_name' => 'web'],
                ['business_id' => $businessId, 'is_default' => 0, 'is_service_staff' => 0]
            );
            if ($role->wasRecentlyCreated) $created++;
            foreach ($permList as $pName) {
                $p = Permission::where('name', $pName)->first();
                if (!$p) continue;
                try { $role->givePermissionTo($p); } catch (\Throwable $e) {}
            }
        }
        return $created;
    }
}
