<?php

namespace App\Models\Task;

use Illuminate\Database\Eloquent\Model;

class TaskCommentAttachment extends Model
{
    protected $table = 'task_comment_attachments';

    protected $fillable = [
        'task_comment_id',
        'path',
        'original_name',
        'mime_type',
        'size',
        'kind',
    ];

    public function comment()
    {
        return $this->belongsTo(TaskComment::class, 'task_comment_id');
    }

    public function getUrlAttribute(): string
    {
        return asset('storage/' . $this->path);
    }

    public function getHumanSizeAttribute(): string
    {
        $bytes = (int) $this->size;
        if ($bytes >= 1048576) {
            return number_format($bytes / 1048576, 1, ',', '.') . ' MB';
        }
        if ($bytes >= 1024) {
            return number_format($bytes / 1024, 0, ',', '.') . ' KB';
        }

        return $bytes . ' B';
    }
}
