<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;

class CreativeAsset extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'booking_item_id',
        'uploaded_by',
        'file_path',
        'file_name',
        'mime_type',
        'file_size',
        'type',
        'width',
        'height',
        'duration',
        'specs_validated',
        'specs_notes',
        'status',
        'reviewed_by',
        'reviewed_at',
        'review_notes',
        'version',
        'is_current',
    ];

    protected $casts = [
        'specs_validated' => 'boolean',
        'is_current'      => 'boolean',
        'reviewed_at'     => 'datetime',
    ];

    // ============================================================
    // Scopes
    // ============================================================
    public function scopeCurrent($query)
    {
        return $query->where('is_current', true);
    }

    public function scopeApproved($query)
    {
        return $query->where('status', 'approved');
    }

    public function scopePending($query)
    {
        return $query->where('status', 'pending');
    }

    public function scopeChangesRequested($query)
    {
        return $query->where('status', 'changes_requested');
    }

    // ============================================================
    // Helpers
    // ============================================================
    public function isPending(): bool
    {
        return $this->status === 'pending';
    }

    public function isApproved(): bool
    {
        return $this->status === 'approved';
    }

    public function isChangesRequested(): bool
    {
        return $this->status === 'changes_requested';
    }

    public function isRejected(): bool
    {
        return $this->status === 'rejected';
    }

    public function getUrl(): string
    {
        return Storage::url($this->file_path);
    }

    public function getFileSizeFormatted(): string
    {
        $bytes = $this->file_size;
        if (!$bytes) return 'N/A';
        if ($bytes >= 1048576) return round($bytes / 1048576, 2) . ' MB';
        if ($bytes >= 1024) return round($bytes / 1024, 2) . ' KB';
        return $bytes . ' B';
    }

    public function getDimensions(): string
    {
        if ($this->width && $this->height) {
            return "{$this->width} × {$this->height} px";
        }
        return 'N/A';
    }

    public function getStatusBadgeClass(): string
    {
        return match($this->status) {
            'approved'          => 'nx-badge-good',
            'pending'           => 'nx-badge-warn',
            'changes_requested' => 'nx-badge-warn',
            'rejected'          => 'nx-badge-danger',
            default             => 'nx-badge',
        };
    }

    public function isImage(): bool
    {
        return str_starts_with($this->mime_type ?? '', 'image/');
    }

    public function isVideo(): bool
    {
        return str_starts_with($this->mime_type ?? '', 'video/');
    }

    // ============================================================
    // Relationships
    // ============================================================
    public function bookingItem()
    {
        return $this->belongsTo(BookingItem::class);
    }

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

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