<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class UploadedChatFile extends Model
{
    use HasFactory;

    protected $table = 'uploaded_chat_files';
    protected $primaryKey = 'file_id';
    public $incrementing = true;

    protected $fillable = [
        'user_id',
        'chat_id',
        'file_name',
        'file_type',
        'file_size',
        'file_url',
    ];

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

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

    public function chat(): BelongsTo
    {
        return $this->belongsTo(ChatRoom::class, 'chat_id');
    }

    public function scopeRecent($query)
    {
        return $query->orderBy('uploaded_at', 'desc');
    }

    public static function generateUrl($originalName)
    {
        return Storage::url('chat/files/' . Str::uuid() . '_' . $originalName);
    }
}

