<?php

declare(strict_types=1);

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Services\Worlds\WorldService;
use App\Support\Seo\SeoFactory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

final class WorldController extends Controller
{
    public function __construct(private readonly WorldService $worlds)
    {
    }

    public function index(Request $request): Response
    {
        $payload = $this->worlds->publicIndexPayload($request->user());
        $seo = app(SeoFactory::class)->collectionListing(
            'Worlds — Skinbase',
            $payload['description'],
            route('worlds.index'),
        )->toArray();

        return Inertia::render('World/WorldIndex', array_merge($payload, [
            'seo' => $seo,
        ]))->rootView('collections');
    }

    public function show(Request $request, string $world): Response|RedirectResponse
    {
        $resolution = $this->worlds->resolvePublicWorld($world);
        $resolvedWorld = $resolution['world'] ?? null;

        abort_unless($resolvedWorld !== null, 404);

        if (! empty($resolution['redirect'])) {
            return redirect()->to((string) $resolution['redirect'], 301);
        }

        $payload = $this->worlds->publicShowPayload($resolvedWorld, $request->user());
        $seo = app(SeoFactory::class)->collectionPage(
            $resolvedWorld->seo_title ?: ($resolvedWorld->title . ' — Skinbase'),
            $resolvedWorld->seo_description ?: ($resolvedWorld->summary ?: $resolvedWorld->description ?: 'Seasonal and editorial discovery world on Skinbase.'),
            $this->worlds->canonicalPublicUrl($resolvedWorld),
            $resolvedWorld->ogImageUrl(),
        )->toArray();

        return Inertia::render('World/WorldShow', array_merge($payload, [
            'seo' => $seo,
        ]))->rootView('collections');
    }

    public function showEdition(Request $request, string $world, int $year): Response|RedirectResponse
    {
        $resolution = $this->worlds->resolvePublicEdition($world, $year);
        $resolvedWorld = $resolution['world'] ?? null;

        abort_unless($resolvedWorld !== null, 404);

        if (! empty($resolution['redirect'])) {
            return redirect()->to((string) $resolution['redirect'], 301);
        }

        $payload = $this->worlds->publicShowPayload($resolvedWorld, $request->user());
        $seo = app(SeoFactory::class)->collectionPage(
            $resolvedWorld->seo_title ?: ($resolvedWorld->title . ' — Skinbase'),
            $resolvedWorld->seo_description ?: ($resolvedWorld->summary ?: $resolvedWorld->description ?: 'Seasonal and editorial discovery world on Skinbase.'),
            $this->worlds->canonicalPublicUrl($resolvedWorld),
            $resolvedWorld->ogImageUrl(),
        )->toArray();

        return Inertia::render('World/WorldShow', array_merge($payload, [
            'seo' => $seo,
        ]))->rootView('collections');
    }
}