<?php

namespace Modules\VetClinic\Entities;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;

class VcImagingStudy extends Model
{
    protected $table = 'vc_imaging_studies';
    protected $guarded = ['id'];

    protected $casts = [
        'uploaded_at' => 'datetime',
    ];

    public function request()
    {
        return $this->belongsTo(VcImagingRequest::class, 'imaging_request_id');
    }

    public function getFileUrlAttribute(): ?string
    {
        return $this->file_path ? Storage::disk('public')->url($this->file_path) : null;
    }

    public function getIsImageAttribute(): bool
    {
        return str_starts_with((string) $this->file_mime, 'image/');
    }

    public function getIsDicomAttribute(): bool
    {
        $mime = (string) $this->file_mime;
        return $mime === 'application/dicom'
            || str_ends_with(strtolower((string) $this->original_name), '.dcm');
    }
}
