<?php

namespace Modules\ClinicCore\Entities;

use Illuminate\Database\Eloquent\Model;

class ClcMedicine extends Model
{
    protected $table = 'clc_medicines';
    protected $guarded = ['id'];

    public function category()
    {
        return $this->belongsTo(ClcMedicineCategory::class, 'category_id');
    }

    public function product()
    {
        return $this->belongsTo(\App\Product::class, 'product_id');
    }

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

    public function scopeControlled($query)
    {
        return $query->whereNotNull('controlled_schedule');
    }

    public function getIsControlledAttribute(): bool
    {
        return !empty($this->controlled_schedule);
    }

    /** Controlled-substance schedules and the label / colour used for badges. */
    public static function schedules(): array
    {
        return [
            'narcotic'          => ['label' => 'Narcotic (مخدرات)',           'color' => 'danger'],
            'schedule_1'        => ['label' => 'Schedule I (جدول I)',         'color' => 'danger'],
            'schedule_2'        => ['label' => 'Schedule II (جدول II)',       'color' => 'warning'],
            'psychotropic'      => ['label' => 'Psychotropic (مؤثرات عقلية)', 'color' => 'warning'],
            'controlled_other'  => ['label' => 'Other controlled',             'color' => 'default'],
        ];
    }

    public function getScheduleLabelAttribute(): ?string
    {
        if (!$this->controlled_schedule) return null;
        return static::schedules()[$this->controlled_schedule]['label'] ?? $this->controlled_schedule;
    }
}
