<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Facades\Storage;

/**
 * Model: RForumAttachment
 *
 * Polymorphic file attachment for forum threads and replies.
 */
class RForumAttachment extends Model
{
    use HasFactory;

    protected $table = 'r_forum_attachments';

    protected $fillable = [
        'attachable_type',
        'attachable_id',
        'user_id',
        'original_name',
        'stored_name',
        'path',
        'disk',
        'mime_type',
        'size',
        'extension',
        'type',
    ];

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

    const TYPE_IMAGE       = 'image';
    const TYPE_PDF         = 'pdf';
    const TYPE_DOCUMENT    = 'document';
    const TYPE_SPREADSHEET = 'spreadsheet';
    const TYPE_OTHER       = 'other';

    /** Allowed MIME types and their categories */
    public static array $allowedMimes = [
        'image/jpeg'                                                                 => self::TYPE_IMAGE,
        'image/png'                                                                  => self::TYPE_IMAGE,
        'image/gif'                                                                  => self::TYPE_IMAGE,
        'image/webp'                                                                 => self::TYPE_IMAGE,
        'application/pdf'                                                            => self::TYPE_PDF,
        'application/msword'                                                         => self::TYPE_DOCUMENT,
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document'   => self::TYPE_DOCUMENT,
        'application/vnd.ms-excel'                                                   => self::TYPE_SPREADSHEET,
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'         => self::TYPE_SPREADSHEET,
    ];

    public function attachable(): MorphTo
    {
        return $this->morphTo();
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /** Resolve the file type from MIME type */
    public static function resolveType(string $mime): string
    {
        return static::$allowedMimes[$mime] ?? self::TYPE_OTHER;
    }

    /** Get the public URL */
    public function getUrlAttribute(): string
    {
        return Storage::disk($this->disk)->url($this->path);
    }

    /** Human-readable file size */
    public function getHumanSizeAttribute(): string
    {
        $bytes = $this->size;
        if ($bytes < 1024) return "{$bytes} B";
        if ($bytes < 1048576) return round($bytes / 1024, 1) . ' KB';
        return round($bytes / 1048576, 1) . ' MB';
    }

    /** Icon class for the file type */
    public function getIconClassAttribute(): string
    {
        return match ($this->type) {
            self::TYPE_IMAGE       => 'fas fa-image text-blue-500',
            self::TYPE_PDF         => 'fas fa-file-pdf text-red-500',
            self::TYPE_DOCUMENT    => 'fas fa-file-word text-blue-700',
            self::TYPE_SPREADSHEET => 'fas fa-file-excel text-green-600',
            default                => 'fas fa-file text-gray-500',
        };
    }

    /** Whether the attachment is an image */
    public function isImage(): bool
    {
        return $this->type === self::TYPE_IMAGE;
    }
}
