<?php

namespace Modules\ClinicCore\Entities;

use Illuminate\Database\Eloquent\Model;

/**
 * A consent form template — bilingual (EN/AR), editable, placeholder-driven.
 * business_id NULL  → built-in system template (read-only in the UI).
 * business_id set   → a clinic's own template.
 */
class ClcConsentTemplate extends Model
{
    protected $table = 'clc_consent_templates';
    protected $guarded = ['id'];

    protected $casts = [
        'requires_witness' => 'boolean',
    ];

    public function consents()
    {
        return $this->hasMany(ClcConsent::class, 'consent_template_id');
    }

    public function scopeActive($query)
    {
        return $query->where('status', 'active');
    }

    public function scopeForBusiness($query, $businessId)
    {
        return $query->where(function ($q) use ($businessId) {
            $q->whereNull('business_id')->orWhere('business_id', $businessId);
        });
    }

    /** True when this row is a built-in system template. */
    public function getIsSystemAttribute(): bool
    {
        return is_null($this->business_id);
    }

    /**
     * Consent categories — value => English label.
     */
    public static function categories(): array
    {
        return [
            'general'         => 'General',
            'surgical'        => 'Surgical',
            'hospitalization' => 'Hospitalization',
            'diagnostic'      => 'Diagnostic',
            'treatment'       => 'Treatment',
            'preventive'      => 'Preventive',
            'boarding'        => 'Boarding',
            'financial'       => 'Financial',
            'end_of_life'     => 'End of Life',
        ];
    }

    public static function categoryColor(string $category): string
    {
        return [
            'general'         => 'default',
            'surgical'        => 'danger',
            'hospitalization' => 'primary',
            'diagnostic'      => 'info',
            'treatment'       => 'success',
            'preventive'      => 'success',
            'boarding'        => 'warning',
            'financial'       => 'warning',
            'end_of_life'     => 'default',
        ][$category] ?? 'default';
    }

    /**
     * Placeholder tokens available inside template bodies, with descriptions.
     */
    public static function availableVariables(): array
    {
        return [
            '{{clinic_name}}'      => 'Clinic / business name',
            '{{pet_name}}'         => "Patient's name",
            '{{species}}'          => 'Patient profile type',
            '{{breed}}'            => 'Patient sub-type',
            '{{pet_sex}}'          => 'Patient sex',
            '{{pet_age}}'          => 'Patient age',
            '{{owner_name}}'       => 'Contact / guardian full name',
            '{{owner_phone}}'      => 'Contact phone number',
            '{{owner_email}}'      => 'Contact email address',
            '{{owner_address}}'    => 'Contact address',
            '{{doctor}}'           => 'Attending doctor',
            '{{appointment_date}}' => 'Appointment date',
            '{{appointment_ref}}'  => 'Appointment reference number',
            '{{appointment_type}}' => 'Appointment type',
            '{{today}}'            => "Today's date",
        ];
    }

    /**
     * Substitute {{tokens}} in a body with their values.
     */
    public static function render(?string $text, array $vars): string
    {
        $text = (string) $text;
        foreach ($vars as $token => $value) {
            $text = str_replace($token, (string) ($value ?? ''), $text);
        }
        return $text;
    }
}
