<?php

declare(strict_types=1);

namespace App\Models;

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

final class EnhanceJob extends Model
{
    use HasFactory;
    use SoftDeletes;

    public const STATUS_PENDING = 'pending';
    public const STATUS_QUEUED = 'queued';
    public const STATUS_PROCESSING = 'processing';
    public const STATUS_COMPLETED = 'completed';
    public const STATUS_FAILED = 'failed';
    public const STATUS_CANCELLED = 'cancelled';
    public const STATUS_EXPIRED = 'expired';

    public const ENGINE_STUB = 'stub';
    public const ENGINE_EXTERNAL_WORKER = 'external_worker';

    protected $fillable = [
        'user_id',
        'artwork_id',
        'status',
        'engine',
        'mode',
        'scale',
        'source_disk',
        'source_path',
        'source_hash',
        'input_width',
        'input_height',
        'input_filesize',
        'input_mime',
        'output_disk',
        'output_path',
        'output_hash',
        'output_width',
        'output_height',
        'output_filesize',
        'output_mime',
        'preview_disk',
        'preview_path',
        'processing_seconds',
        'error_message',
        'metadata',
        'queued_at',
        'started_at',
        'finished_at',
        'expires_at',
    ];

    protected $casts = [
        'metadata' => 'array',
        'queued_at' => 'datetime',
        'started_at' => 'datetime',
        'finished_at' => 'datetime',
        'expires_at' => 'datetime',
        'deleted_at' => 'datetime',
    ];

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

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

    public function isPending(): bool
    {
        return $this->status === self::STATUS_PENDING;
    }

    public function isQueued(): bool
    {
        return $this->status === self::STATUS_QUEUED;
    }

    public function isProcessing(): bool
    {
        return $this->status === self::STATUS_PROCESSING;
    }

    public function isCompleted(): bool
    {
        return $this->status === self::STATUS_COMPLETED;
    }

    public function isFailed(): bool
    {
        return $this->status === self::STATUS_FAILED;
    }

    public function canBeDeletedBy(User $user): bool
    {
        if ($user->isAdmin() || $user->isModerator()) {
            return true;
        }

        return (int) $this->user_id === (int) $user->id
            && in_array($this->status, [self::STATUS_PENDING, self::STATUS_FAILED, self::STATUS_COMPLETED, self::STATUS_CANCELLED, self::STATUS_EXPIRED], true);
    }

    public function sourceUrl(): ?string
    {
        return $this->resolveDiskUrl($this->source_disk, $this->source_path);
    }

    public function outputUrl(): ?string
    {
        return $this->resolveDiskUrl($this->output_disk, $this->output_path);
    }

    public function previewUrl(): ?string
    {
        return $this->resolveDiskUrl($this->preview_disk, $this->preview_path);
    }

    private function resolveDiskUrl(?string $disk, ?string $path): ?string
    {
        $trimmedPath = ltrim(trim((string) $path), '/');

        if ($trimmedPath === '') {
            return null;
        }

        $configuredDisk = trim((string) config('enhance.disk', 'public'));
        $targetDisk = trim((string) $disk) ?: $configuredDisk ?: 'public';

        // For non-local disks (e.g. S3-backed), construct the CDN URL directly.
        // For local disks ('public', 'local') fall through to Storage::disk()->url()
        // so that the correct APP_URL-based path is returned in non-CDN environments.
        $base = rtrim((string) config('cdn.files_url', ''), '/');
        if ($base !== '' && $targetDisk === $configuredDisk && ! in_array($targetDisk, ['public', 'local'], true)) {
            return $base . '/' . $trimmedPath;
        }

        $url = Storage::disk($targetDisk)->url($trimmedPath);

        if (str_starts_with($url, 'http://') || str_starts_with($url, 'https://')) {
            return $url;
        }

        return url($url);
    }
}