<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class PatientNotification extends Model
{
    protected $table = 'patient_notifications';

    protected $fillable = [
        'patient_id', 'doctor_id', 'type',
        'title', 'body', 'data', 'is_read', 'read_at',
    ];

    protected $casts = [
        'data'     => 'array',
        'is_read'  => 'boolean',
        'read_at'  => 'datetime',
    ];

    public function patient() { return $this->belongsTo(Patient::class); }
    public function doctor()  { return $this->belongsTo(Doctor::class); }

    public function markRead(): void
    {
        $this->update(['is_read' => true, 'read_at' => now()]);
    }

    // أيقونة حسب النوع
    public function getIconAttribute(): string
    {
        return match($this->type) {
            'appointment_confirmed'  => '✅',
            'appointment_cancelled'  => '❌',
            'queue_called'           => '📣',
            'queue_position'         => '🕐',
            'prescription_ready'     => '💊',
            default                  => '🔔',
        };
    }

    public function getColorAttribute(): string
    {
        return match($this->type) {
            'appointment_confirmed'  => 'success',
            'appointment_cancelled'  => 'danger',
            'queue_called'           => 'warning',
            'queue_position'         => 'info',
            'prescription_ready'     => 'primary',
            default                  => 'secondary',
        };
    }

    // إرسال إشعار لمريض
    public static function send(int $patientId, string $type, string $title, string $body, array $data = [], ?int $doctorId = null): self
    {
        return self::create([
            'patient_id' => $patientId,
            'doctor_id'  => $doctorId,
            'type'       => $type,
            'title'      => $title,
            'body'       => $body,
            'data'       => $data,
        ]);
    }
}
