<?php

declare(strict_types=1);

namespace App\Models;

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

class WorldWebStoryPage extends Model
{
    use HasFactory;
    use SoftDeletes;

    public const LAYOUT_COVER = 'cover';
    public const LAYOUT_ARTWORK = 'artwork';
    public const LAYOUT_CREATOR = 'creator';
    public const LAYOUT_MOOD = 'mood';
    public const LAYOUT_COLLECTION = 'collection';
    public const LAYOUT_CTA = 'cta';

    public const BACKGROUND_IMAGE = 'image';
    public const BACKGROUND_VIDEO = 'video';
    public const BACKGROUND_GRADIENT = 'gradient';

    protected $fillable = [
        'story_id',
        'artwork_id',
        'position',
        'layout',
        'background_type',
        'background_path',
        'background_mobile_path',
        'headline',
        'body',
        'cta_label',
        'cta_url',
        'alt_text',
        'caption',
        'credit_text',
        'text_position',
        'overlay_strength',
        'animation',
        'active',
    ];

    protected $casts = [
        'position' => 'integer',
        'overlay_strength' => 'integer',
        'active' => 'boolean',
    ];

    protected static function booted(): void
    {
        $flushCache = static function (self $page): void {
            $story = $page->relationLoaded('story') ? $page->story : $page->story()->with('world')->first();

            if (! ($story instanceof WorldWebStory)) {
                return;
            }

            Cache::forget('web_story:' . $story->slug);
            Cache::forget('web_story_index');

            if ($story->world?->slug) {
                Cache::forget('world:' . $story->world->slug . ':web_story');
            }
        };

        static::saved($flushCache);
        static::deleted($flushCache);
        static::restored($flushCache);
    }

    public function story(): BelongsTo
    {
        return $this->belongsTo(WorldWebStory::class, 'story_id');
    }

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

    public function scopeOrderedPages(Builder $query): Builder
    {
        return $query->orderBy('position')->orderBy('id');
    }

    public function backgroundUrl(): ?string
    {
        return $this->assetUrl($this->background_mobile_path ?: $this->background_path);
    }

    public function desktopBackgroundUrl(): ?string
    {
        return $this->assetUrl($this->background_path ?: $this->background_mobile_path);
    }

    private function assetUrl(?string $path): ?string
    {
        $resolved = trim((string) $path);

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

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

        return rtrim((string) config('cdn.files_url', 'https://files.skinbase.org'), '/') . '/' . ltrim($resolved, '/');
    }
}