<?php

namespace App\Models;

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

class RequestVersion extends Model
{
    use HasFactory;

    protected $fillable = [
        'request_id',
        'version_number',
        'file_path',
        'original_filename',
        'mime_type',
        'file_size',
        'uploaded_by',
        'uploaded_at',
        'approved_at',
        'approved_by',
    ];

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

    public function request()
    {
        return $this->belongsTo(MarketingRequest::class, 'request_id');
    }

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

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

    public function revisionRequest()
    {
        return $this->hasOne(RevisionRequest::class, 'request_version_id');
    }

    public function isImage(): bool
    {
        return str_contains($this->mime_type ?? '', 'image') || in_array(strtolower(pathinfo($this->original_filename, PATHINFO_EXTENSION)), ['jpg', 'jpeg', 'png', 'gif', 'webp']);
    }

    public function isPdf(): bool
    {
        return str_contains($this->mime_type ?? '', 'pdf') || strtolower(pathinfo($this->original_filename, PATHINFO_EXTENSION)) === 'pdf';
    }
}
