<?php

namespace App\Models;

use App\Models\BaseModel;

class TransactionAttachment extends BaseModel
{
    protected $table = 'transaction_attachments';

    protected $fillable = [
        'company_id','attachable_type','attachable_id',
        'file_name','file_path','file_type','file_size',
        'description','uploaded_by',
    ];

    protected $casts = [
        'file_size' => 'integer',
    ];

    public function attachable()  { return $this->morphTo(); }
    public function company()     { return $this->belongsTo(Company::class); }
    public function uploadedBy()  { return $this->belongsTo(User::class, 'uploaded_by'); }

    public function getFileSizeHumanAttribute(): string
    {
        $bytes = $this->file_size;
        if ($bytes < 1024) return $bytes . ' B';
        if ($bytes < 1048576) return round($bytes / 1024, 1) . ' KB';
        return round($bytes / 1048576, 1) . ' MB';
    }

    public function isImage(): bool
    {
        return str_starts_with($this->file_type ?? '', 'image/');
    }

    public function isPdf(): bool
    {
        return $this->file_type === 'application/pdf';
    }
}
