<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

/**
 * Item #6 - seed six standard veterinary-clinic roles per business so
 * new installs have a workable RBAC baseline out-of-the-box.
 *
 *   Receptionist   - book/edit appointments, manage owners/pets, no clinical
 *   Doctor         - full consultation + prescriptions + dispense + vaccines
 *   Nurse          - vital intake, vaccinations, prep
 *   Assistant      - read-mostly, basic appointment support
 *   Branch Manager - all clinical + admin + reports + blocking + edit-requests
 *   Accounting     - financial view + critical-check report + owner financials
 *
 * UltimatePOS scopes roles by business — names use the convention
 * "<Role>#<business_id>". For each existing business, this migration:
 *   1. Creates the six roles if missing (idempotent)
 *   2. Grants the appropriate permissions per role
 *
 * On first install of a NEW business, the existing install flow doesn't
 * call us automatically; the install controller would need to invoke
 * this seeder per-business. Until then, this migration handles every
 * business present at upgrade time, and the InstallController can call
 * the same logic for fresh installs.
 */
class SeedDefaultVetRoles extends Migration
{
    public function up()
    {
        // ── Permission presets per role -----------------------------
        $perms = [
            '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',
            ],
        ];

        // ── For each business, ensure the six roles exist + are granted.
        // Delegates to the shared DefaultRolesSeeder so the migration and
        // the Business::created observer never drift on the permission map.
        // (The $perms array above is kept for documentation; the seeder
        // is the active source of truth.)
        $businessIds = DB::table('business')->pluck('id');
        foreach ($businessIds as $bizId) {
            try {
                \Modules\VetClinic\Services\DefaultRolesSeeder::seedForBusiness((int) $bizId);
            } catch (\Throwable $e) {
                \Log::warning('Default-role seed skipped for biz ' . $bizId . ': ' . $e->getMessage());
            }
        }
    }

    public function down()
    {
        // Safety: don't auto-delete roles - someone may have assigned users
        // to them. Manual cleanup if a clinic explicitly wants the defaults
        // removed.
    }
}
