<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Facades\Auth;

class PatientPrescription extends Model
{
    protected $table = 'patient_prescription';

    protected $fillable = [
        'patient_id',
        'prescription_id',
        'direction_one',
        'direction_two',
        'dont_substitute',
        'comments',
        'appointment_id',
        "brand",
        "from",
        "quantity",
        "direction_quantity",
        "refill_quantity",
        "dosage",
        "status",
        'created_by_id',
        'created_by_type'
    ];

    protected $appends = ['created_by'];

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            if (Auth::guard('admin')->check()) {
                $model->created_by_id = Auth::guard('admin')->id();
                $model->created_by_type = 'App\Models\Admin';
            } elseif (Auth::check()) {
                $model->created_by_id = Auth::id();
                $model->created_by_type = 'App\Models\Telemedpro';
            }
        });
    }
    public function createdBy(): MorphTo
    {
        return $this->morphTo();
    }
    public function getCreatedByAttribute()
    {
        if (!$this->created_by_type || !$this->created_by_id) {
            return null;
        }

        $model = $this->created_by_type::find($this->created_by_id);
        if (!$model) {
            return null;
        }

        $type = $this->created_by_type === 'App\Models\Admin' ? 'Admin' : 'Agent';

        return $model->name . ' (' . $type . ')';
    }
    /* public function getCreatedByAttribute()
    {
        // Check if we're using a custom select
        if (!array_key_exists('created_by_id', $this->attributes) || !array_key_exists('created_by_type', $this->attributes)) {
            return $this->provide_name ? $this->provide_name . ' (Provider)' : null;
        }

        if (!$this->created_by_type || !$this->created_by_id) {
            return $this->provide_name ? $this->provide_name . ' (Provider)' : null;
        }

        $model = $this->created_by_type::find($this->created_by_id);
        if (!$model) {
            return $this->provide_name ? $this->provide_name . ' (Provider)' : null;
        }

        $type = match ($this->created_by_type) {
            'App\Models\Admin' => 'Admin',
            'App\Models\Telemedpro' => 'TelemedPro',
            //default => 'User',
        };

        return $model->name . ' (' . $type . ')';
    } */

    protected $hidden = ['created_by_id', 'created_by_type'];



    public function patient()
    {
        return $this->belongsTo(Patient::class, 'patient_id');
    }

    public function prescription()
    {
        return $this->belongsTo(Prescription::class, 'prescription_id');
    }

    public function appointment()
    {
        return $this->belongsTo(Appointment::class, 'appointment_id', 'id');
    }
}
