<?php

declare(strict_types=1);

namespace App\Http\Controllers\Studio;

use App\Enums\WorldRewardType;
use App\Http\Controllers\Controller;
use App\Http\Requests\Worlds\StoreWorldRequest;
use App\Http\Requests\Worlds\UpdateWorldRequest;
use App\Models\World;
use App\Models\WorldSubmission;
use App\Services\Worlds\WorldAnalyticsService;
use App\Services\Worlds\WorldEditorialSuggestionService;
use App\Services\Worlds\WorldRewardService;
use App\Services\Worlds\WorldService;
use App\Services\Worlds\WorldSubmissionService;
use App\Support\Seo\SeoFactory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

final class StudioWorldController extends Controller
{
    public function __construct(
        private readonly WorldService $worlds,
        private readonly WorldEditorialSuggestionService $editorialSuggestions,
        private readonly WorldSubmissionService $submissions,
        private readonly WorldRewardService $rewards,
        private readonly WorldAnalyticsService $analytics,
    )
    {
    }

    public function index(Request $request): Response
    {
        $this->authorize('manage', World::class);

        return Inertia::render('Studio/StudioWorldsIndex', [
            'title' => 'Worlds',
            'description' => 'Create and manage seasonal, event, and campaign destinations across Skinbase.',
            'listing' => $this->worlds->studioListing($request->only(['q', 'status', 'type', 'per_page'])),
            'analytics' => $this->analytics->portfolioReport(),
            'statusOptions' => [
                ['value' => World::STATUS_DRAFT, 'label' => 'Draft'],
                ['value' => World::STATUS_PUBLISHED, 'label' => 'Published'],
                ['value' => World::STATUS_ARCHIVED, 'label' => 'Archived'],
            ],
            'typeOptions' => [
                ['value' => World::TYPE_SEASONAL, 'label' => 'Seasonal'],
                ['value' => World::TYPE_EVENT, 'label' => 'Event'],
                ['value' => World::TYPE_CAMPAIGN, 'label' => 'Campaign'],
                ['value' => World::TYPE_TRIBUTE, 'label' => 'Tribute'],
            ],
            'createUrl' => route('studio.worlds.create'),
        ]);
    }

    public function create(Request $request): Response|RedirectResponse
    {
        if (! $request->user()?->can('create', World::class)) {
            return redirect()->route('worlds.index');
        }

        return Inertia::render('Studio/StudioWorldEditor', [
            'title' => 'Create world',
            'description' => 'Build a curated campaign destination with themed visuals, ordered sections, and explicit content attachments.',
            'world' => null,
            'themeOptions' => $this->worlds->themeOptions(),
            'sectionOptions' => $this->worlds->sectionOptions(),
            'relationTypeOptions' => $this->worlds->relationTypeOptions(),
            'typeOptions' => [
                ['value' => World::TYPE_SEASONAL, 'label' => 'Seasonal'],
                ['value' => World::TYPE_EVENT, 'label' => 'Event'],
                ['value' => World::TYPE_CAMPAIGN, 'label' => 'Campaign'],
                ['value' => World::TYPE_TRIBUTE, 'label' => 'Tribute'],
            ],
            'statusOptions' => [
                ['value' => World::STATUS_DRAFT, 'label' => 'Draft'],
                ['value' => World::STATUS_PUBLISHED, 'label' => 'Published'],
                ['value' => World::STATUS_ARCHIVED, 'label' => 'Archived'],
            ],
            'storeUrl' => route('studio.worlds.store'),
            'entitySearchUrl' => route('studio.worlds.entity-search'),
            'suggestions' => null,
            'suggestionActions' => null,
            'duplicateActions' => null,
            'mediaSupport' => [
                'picker_available' => false,
                'helper_text' => 'Drop a cover or OG image here and Skinbase will optimize it and store it on the CDN automatically.',
                'upload_url' => route('api.studio.worlds.media.upload'),
                'delete_url' => route('api.studio.worlds.media.destroy'),
                'files_base_url' => rtrim((string) config('cdn.files_url', 'https://files.skinbase.org'), '/'),
                'accepted_mime_types' => ['image/jpeg', 'image/png', 'image/webp'],
                'max_file_size_mb' => 6,
            ],
        ]);
    }

    public function store(StoreWorldRequest $request): RedirectResponse
    {
        $this->authorize('create', World::class);

        $world = $this->worlds->store($request->user(), $request->validated());

        return redirect()->route('studio.worlds.edit', ['world' => $world])->with('success', 'World draft created.');
    }

    public function edit(Request $request, World $world): Response
    {
        $this->authorize('update', $world);

        return Inertia::render('Studio/StudioWorldEditor', [
            'title' => 'Edit world',
            'description' => 'Tune the world identity, adjust section order, and refine the curated attachments.',
            'world' => $this->worlds->mapStudioWorld($world, $request->user()),
            'themeOptions' => $this->worlds->themeOptions(),
            'sectionOptions' => $this->worlds->sectionOptions(),
            'relationTypeOptions' => $this->worlds->relationTypeOptions(),
            'typeOptions' => [
                ['value' => World::TYPE_SEASONAL, 'label' => 'Seasonal'],
                ['value' => World::TYPE_EVENT, 'label' => 'Event'],
                ['value' => World::TYPE_CAMPAIGN, 'label' => 'Campaign'],
                ['value' => World::TYPE_TRIBUTE, 'label' => 'Tribute'],
            ],
            'statusOptions' => [
                ['value' => World::STATUS_DRAFT, 'label' => 'Draft'],
                ['value' => World::STATUS_PUBLISHED, 'label' => 'Published'],
                ['value' => World::STATUS_ARCHIVED, 'label' => 'Archived'],
            ],
            'updateUrl' => route('studio.worlds.update', ['world' => $world]),
            'previewUrl' => route('studio.worlds.preview', ['world' => $world]),
            'publishUrl' => route('studio.worlds.publish', ['world' => $world]),
            'publishRecapUrl' => route('studio.worlds.recap.publish', ['world' => $world]),
            'archiveUrl' => route('studio.worlds.archive', ['world' => $world]),
            'entitySearchUrl' => route('studio.worlds.entity-search'),
            'suggestions' => $this->editorialSuggestions->editorPayload($world, $request->user()),
            'suggestionActions' => [
                'add' => route('studio.worlds.suggestions.add', ['world' => $world]),
                'pin' => route('studio.worlds.suggestions.pin', ['world' => $world]),
                'dismiss' => route('studio.worlds.suggestions.dismiss', ['world' => $world]),
                'notRelevant' => route('studio.worlds.suggestions.not-relevant', ['world' => $world]),
                'restore' => route('studio.worlds.suggestions.restore', ['world' => $world]),
            ],
            'duplicateActions' => [
                'duplicateUrl' => route('studio.worlds.duplicate', ['world' => $world]),
                'newEditionUrl' => route('studio.worlds.new-edition', ['world' => $world]),
                'canCreateEdition' => $this->worlds->canCreateNewEdition($world),
                'duplicateModeOptions' => $this->worlds->duplicateModeOptions(false),
                'newEditionModeOptions' => $this->worlds->duplicateModeOptions(true),
            ],
            'mediaSupport' => [
                'picker_available' => false,
                'helper_text' => 'Drop a cover or OG image here and Skinbase will optimize it and store it on the CDN automatically.',
                'upload_url' => route('api.studio.worlds.media.upload'),
                'delete_url' => route('api.studio.worlds.media.destroy'),
                'files_base_url' => rtrim((string) config('cdn.files_url', 'https://files.skinbase.org'), '/'),
                'accepted_mime_types' => ['image/jpeg', 'image/png', 'image/webp'],
                'max_file_size_mb' => 6,
            ],
        ]);
    }

    public function update(UpdateWorldRequest $request, World $world): RedirectResponse
    {
        $this->authorize('update', $world);

        $this->worlds->update($world, $request->user(), $request->validated());

        return back()->with('success', 'World updated.');
    }

    public function publish(Request $request, World $world): RedirectResponse
    {
        $this->authorize('update', $world);

        $this->worlds->publish($world);

        return back()->with('success', 'World published.');
    }

    public function archive(Request $request, World $world): RedirectResponse
    {
        $this->authorize('update', $world);

        $this->worlds->archive($world);

        return back()->with('success', 'World archived.');
    }

    public function publishRecap(Request $request, World $world): RedirectResponse
    {
        $this->authorize('update', $world);

        $this->worlds->publishRecap($world);

        return back()->with('success', 'World recap published.');
    }

    public function duplicate(Request $request, World $world): RedirectResponse
    {
        $this->authorize('create', World::class);
        $this->authorize('update', $world);

        $validated = $request->validate([
            'copy_mode' => ['nullable', 'string', 'in:' . implode(',', [
                WorldService::COPY_MODE_STRUCTURE_ONLY,
                WorldService::COPY_MODE_WITH_RELATIONS,
            ])],
        ]);

        $duplicate = $this->worlds->duplicateWithMode(
            $world,
            $request->user(),
            false,
            (string) ($validated['copy_mode'] ?? WorldService::COPY_MODE_WITH_RELATIONS),
        );

        return redirect()->route('studio.worlds.edit', ['world' => $duplicate])->with('success', 'World duplicated into a new draft.');
    }

    public function newEdition(Request $request, World $world): RedirectResponse
    {
        $this->authorize('create', World::class);
        $this->authorize('update', $world);

        $validated = $request->validate([
            'copy_mode' => ['nullable', 'string', 'in:' . implode(',', [
                WorldService::COPY_MODE_STRUCTURE_ONLY,
                WorldService::COPY_MODE_WITH_RELATIONS,
            ])],
        ]);

        $edition = $this->worlds->duplicateWithMode(
            $world,
            $request->user(),
            true,
            (string) ($validated['copy_mode'] ?? WorldService::COPY_MODE_WITH_RELATIONS),
        );

        return redirect()->route('studio.worlds.edit', ['world' => $edition])->with('success', 'Next edition draft created.');
    }

    public function entitySearch(Request $request): JsonResponse
    {
        $this->authorize('manage', World::class);

        $validated = $request->validate([
            'type' => ['required', 'string'],
            'q' => ['nullable', 'string', 'max:120'],
        ]);

        return response()->json([
            'items' => $this->worlds->searchEntities((string) $validated['type'], (string) ($validated['q'] ?? ''), $request->user()),
        ]);
    }

    public function addSuggestion(Request $request, World $world): JsonResponse
    {
        $this->authorize('update', $world);

        $validated = $request->validate([
            'related_type' => ['required', 'string'],
            'related_id' => ['required', 'integer', 'min:1'],
            'section_key' => ['required', 'string'],
            'is_featured' => ['nullable', 'boolean'],
        ]);

        return response()->json(
            $this->editorialSuggestions->addSuggestionToSection(
                $world,
                $request->user(),
                (string) $validated['related_type'],
                (int) $validated['related_id'],
                (string) $validated['section_key'],
                (bool) ($validated['is_featured'] ?? false),
            )
        );
    }

    public function pinSuggestion(Request $request, World $world): JsonResponse
    {
        $this->authorize('update', $world);

        $validated = $request->validate([
            'related_type' => ['required', 'string'],
            'related_id' => ['required', 'integer', 'min:1'],
            'section_key' => ['nullable', 'string'],
        ]);

        return response()->json(
            $this->editorialSuggestions->pinSuggestion(
                $world,
                $request->user(),
                (string) $validated['related_type'],
                (int) $validated['related_id'],
                (string) ($validated['section_key'] ?? ''),
            )
        );
    }

    public function dismissSuggestion(Request $request, World $world): JsonResponse
    {
        $this->authorize('update', $world);

        $validated = $request->validate([
            'related_type' => ['required', 'string'],
            'related_id' => ['required', 'integer', 'min:1'],
        ]);

        return response()->json(
            $this->editorialSuggestions->dismissSuggestion(
                $world,
                $request->user(),
                (string) $validated['related_type'],
                (int) $validated['related_id'],
            )
        );
    }

    public function markSuggestionNotRelevant(Request $request, World $world): JsonResponse
    {
        $this->authorize('update', $world);

        $validated = $request->validate([
            'related_type' => ['required', 'string'],
            'related_id' => ['required', 'integer', 'min:1'],
        ]);

        return response()->json(
            $this->editorialSuggestions->markSuggestionNotRelevant(
                $world,
                $request->user(),
                (string) $validated['related_type'],
                (int) $validated['related_id'],
            )
        );
    }

    public function restoreSuggestion(Request $request, World $world): JsonResponse
    {
        $this->authorize('update', $world);

        $validated = $request->validate([
            'related_type' => ['required', 'string'],
            'related_id' => ['required', 'integer', 'min:1'],
        ]);

        return response()->json(
            $this->editorialSuggestions->restoreSuggestion(
                $world,
                (string) $validated['related_type'],
                (int) $validated['related_id'],
            )
        );
    }

    public function approveSubmission(Request $request, World $world, WorldSubmission $submission): RedirectResponse
    {
        return $this->transitionSubmission($request, $world, $submission, WorldSubmission::STATUS_LIVE, 'Submission approved and is now live.');
    }

    public function removeSubmission(Request $request, World $world, WorldSubmission $submission): RedirectResponse
    {
        return $this->transitionSubmission($request, $world, $submission, WorldSubmission::STATUS_REMOVED, 'Submission removed from the world.');
    }

    public function blockSubmission(Request $request, World $world, WorldSubmission $submission): RedirectResponse
    {
        return $this->transitionSubmission($request, $world, $submission, WorldSubmission::STATUS_BLOCKED, 'Submission blocked from this world.');
    }

    public function unblockSubmission(Request $request, World $world, WorldSubmission $submission): RedirectResponse
    {
        return $this->transitionSubmission($request, $world, $submission, WorldSubmission::STATUS_REMOVED, 'Submission unblocked. It can now be restored or re-added later.');
    }

    public function restoreSubmission(Request $request, World $world, WorldSubmission $submission): RedirectResponse
    {
        return $this->transitionSubmission($request, $world, $submission, WorldSubmission::STATUS_LIVE, 'Submission restored to live.');
    }

    public function featureSubmission(Request $request, World $world, WorldSubmission $submission): RedirectResponse
    {
        return $this->toggleFeaturedSubmission($request, $world, $submission, true, 'Submission featured in the public community section.');
    }

    public function unfeatureSubmission(Request $request, World $world, WorldSubmission $submission): RedirectResponse
    {
        return $this->toggleFeaturedSubmission($request, $world, $submission, false, 'Submission removed from featured community placement.');
    }

    public function pendingSubmission(Request $request, World $world, WorldSubmission $submission): RedirectResponse
    {
        return $this->transitionSubmission($request, $world, $submission, WorldSubmission::STATUS_PENDING, 'Submission returned to pending.');
    }

    public function grantSubmissionReward(Request $request, World $world, WorldSubmission $submission, string $rewardType): RedirectResponse
    {
        $this->authorize('update', $world);

        abort_unless((int) $submission->world_id === (int) $world->id, 404);

        $reward = WorldRewardType::tryFrom($rewardType);
        abort_if($reward === null || $reward->isAutomatic(), 404);

        $validated = $request->validate([
            'review_note' => ['nullable', 'string', 'max:1000'],
        ]);

        $this->rewards->grantManualReward($submission, $request->user(), $reward, (string) ($validated['review_note'] ?? ''));

        return back()->with('success', $reward->label() . ' reward granted.');
    }

    public function revokeSubmissionReward(Request $request, World $world, WorldSubmission $submission, string $rewardType): RedirectResponse
    {
        $this->authorize('update', $world);

        abort_unless((int) $submission->world_id === (int) $world->id, 404);

        $reward = WorldRewardType::tryFrom($rewardType);
        abort_if($reward === null || $reward->isAutomatic(), 404);

        $this->rewards->revokeManualReward($submission, $reward);

        return back()->with('success', $reward->label() . ' reward revoked.');
    }

    public function preview(Request $request, World $world): \Inertia\Response
    {
        $this->authorize('update', $world);

        $payload = $this->worlds->publicShowPayload($world, $request->user(), true);
        $seo = app(SeoFactory::class)->collectionPage(
            $world->seo_title ?: ($world->title . ' — Skinbase Preview'),
            $world->seo_description ?: ($world->summary ?: $world->description ?: 'Preview world page'),
            route('studio.worlds.preview', ['world' => $world]),
            $world->ogImageUrl(),
            false,
        )->toArray();

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

    private function transitionSubmission(Request $request, World $world, WorldSubmission $submission, string $status, string $flashMessage): RedirectResponse
    {
        $this->authorize('update', $world);

        abort_unless((int) $submission->world_id === (int) $world->id, 404);

        $validated = $request->validate([
            'review_note' => ['nullable', 'string', 'max:1000'],
        ]);

        $this->submissions->transition($submission, $request->user(), $status, $validated['review_note'] ?? null);

        return back()->with('success', $flashMessage);
    }

    private function toggleFeaturedSubmission(Request $request, World $world, WorldSubmission $submission, bool $featured, string $flashMessage): RedirectResponse
    {
        $this->authorize('update', $world);

        abort_unless((int) $submission->world_id === (int) $world->id, 404);

        $validated = $request->validate([
            'review_note' => ['nullable', 'string', 'max:1000'],
        ]);

        $this->submissions->setFeatured($submission, $request->user(), $featured, $validated['review_note'] ?? null);

        return back()->with('success', $flashMessage);
    }
}