<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Upload extends Model
{
    protected $fillable = [
        'original_name', 'path', 'size', 'mime_type', 'uploader_id',
    ];

    protected $appends = ['size_human', 'icon'];

    public function uploadable()
    {
        return $this->morphTo();
    }

    public function uploader()
    {
        return $this->belongsTo(User::class, 'uploader_id');
    }

    public function getSizeHumanAttribute(): string
    {
        $b = $this->size;
        if ($b < 1024)       return $b . ' B';
        if ($b < 1_048_576)  return round($b / 1024, 1) . ' KB';
        return round($b / 1_048_576, 1) . ' MB';
    }

    public function getIconAttribute(): string
    {
        $mime = $this->mime_type ?? '';
        if (str_contains($mime, 'pdf'))                                          return '📄';
        if (str_contains($mime, 'image'))                                        return '🖼️';
        if (str_contains($mime, 'spreadsheet') || str_contains($mime, 'excel')) return '📊';
        if (str_contains($mime, 'word') || str_contains($mime, 'document'))     return '📝';
        if (str_contains($mime, 'zip') || str_contains($mime, 'archive'))       return '🗜️';
        return '📎';
    }
}
