<?php

namespace Modules\ClinicCore\Observers;

use App\Business;
use Modules\ClinicCore\Services\DefaultRolesSeeder;

/**
 * Out-of-scope follow-up #OOS-a — when a NEW business is created (via
 * Superadmin's "Add Business" flow, or any other path), automatically
 * seed the six default dermatology-clinic roles for it.
 *
 * Existing businesses at migration time were handled by
 * 2026_05_31_000009_seed_default_vet_roles. This observer covers every
 * business created afterwards.
 *
 * Fails soft: if the seeder errors (e.g. permission table not yet
 * created on a partial install), the business creation itself still
 * succeeds.
 */
class ClcBusinessObserver
{
    public function created(Business $business): void
    {
        try {
            DefaultRolesSeeder::seedForBusiness((int) $business->id);
        } catch (\Throwable $e) {
            \Log::warning(
                'ClinicCore default-role seed failed for new business #'
                . $business->id . ': ' . $e->getMessage()
            );
        }
    }
}
