<?php

declare(strict_types=1);

namespace App\Http\Requests\Worlds;

use App\Models\World;
use App\Models\GroupChallenge;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Carbon;
use Illuminate\Validation\Rule;

class StoreWorldRequest extends FormRequest
{
    /**
     * @return array<int, string>
     */
    protected function reservedSlugs(): array
    {
        return ['create'];
    }

    public function authorize(): bool
    {
        return (bool) $this->user();
    }

    public function rules(): array
    {
        $sectionKeys = array_keys((array) config('worlds.sections', []));
        $relationTypes = array_keys((array) config('worlds.relation_types', []));

        return [
            'title' => ['required', 'string', 'max:180'],
            'slug' => ['nullable', 'string', 'max:180', 'regex:/^[a-z0-9]+(?:-[a-z0-9]+)*$/', Rule::notIn($this->reservedSlugs())],
            'tagline' => ['nullable', 'string', 'max:220'],
            'summary' => ['nullable', 'string', 'max:320'],
            'teaser_title' => ['nullable', 'string', 'max:180'],
            'teaser_summary' => ['nullable', 'string', 'max:320'],
            'description' => ['nullable', 'string', 'max:20000'],
            'cover_path' => ['nullable', 'string', 'max:2048'],
            'teaser_image_path' => ['nullable', 'string', 'max:2048'],
            'theme_key' => ['nullable', 'string', 'max:80'],
            'accent_color' => ['nullable', 'string', 'max:16'],
            'accent_color_secondary' => ['nullable', 'string', 'max:16'],
            'background_motif' => ['nullable', 'string', 'max:80'],
            'icon_name' => ['nullable', 'string', 'max:120'],
            'status' => ['required', Rule::in([World::STATUS_DRAFT, World::STATUS_PUBLISHED, World::STATUS_ARCHIVED])],
            'type' => ['required', Rule::in([World::TYPE_SEASONAL, World::TYPE_EVENT, World::TYPE_CAMPAIGN, World::TYPE_TRIBUTE])],
            'starts_at' => ['nullable', 'date'],
            'ends_at' => ['nullable', 'date', 'after_or_equal:starts_at'],
            'promotion_starts_at' => ['nullable', 'date'],
            'promotion_ends_at' => ['nullable', 'date', 'after_or_equal:promotion_starts_at'],
            'accepts_submissions' => ['nullable', 'boolean'],
            'participation_mode' => ['nullable', Rule::in([
                World::PARTICIPATION_MODE_MANUAL_APPROVAL,
                World::PARTICIPATION_MODE_AUTO_ADD,
                World::PARTICIPATION_MODE_CLOSED,
            ])],
            'submission_starts_at' => ['nullable', 'date'],
            'submission_ends_at' => ['nullable', 'date', 'after_or_equal:submission_starts_at'],
            'submission_note_enabled' => ['nullable', 'boolean'],
            'community_section_enabled' => ['nullable', 'boolean'],
            'allow_readd_after_removal' => ['nullable', 'boolean'],
            'is_featured' => ['nullable', 'boolean'],
            'is_active_campaign' => ['nullable', 'boolean'],
            'is_homepage_featured' => ['nullable', 'boolean'],
            'campaign_priority' => ['nullable', 'integer', 'min:0', 'max:9999'],
            'is_recurring' => ['nullable', 'boolean'],
            'recurrence_key' => ['nullable', 'string', 'max:120'],
            'recurrence_rule' => ['nullable', 'string', 'max:160'],
            'edition_year' => ['nullable', 'integer', 'between:2000,2100'],
            'cta_label' => ['nullable', 'string', 'max:120'],
            'cta_url' => ['nullable', 'url', 'max:2048'],
            'badge_label' => ['nullable', 'string', 'max:120'],
            'campaign_label' => ['nullable', 'string', 'max:120'],
            'badge_description' => ['nullable', 'string', 'max:2000'],
            'submission_guidelines' => ['nullable', 'string', 'max:5000'],
            'badge_url' => ['nullable', 'url', 'max:2048'],
            'seo_title' => ['nullable', 'string', 'max:255'],
            'seo_description' => ['nullable', 'string', 'max:300'],
            'og_image_path' => ['nullable', 'string', 'max:2048'],
            'recap_status' => ['nullable', Rule::in([World::RECAP_STATUS_DRAFT, World::RECAP_STATUS_PUBLISHED])],
            'recap_title' => ['nullable', 'string', 'max:180'],
            'recap_summary' => ['nullable', 'string', 'max:320'],
            'recap_intro' => ['nullable', 'string', 'max:12000'],
            'recap_editor_note' => ['nullable', 'string', 'max:4000'],
            'recap_cover_path' => ['nullable', 'string', 'max:2048'],
            'recap_article_id' => ['nullable', 'integer', 'exists:news_articles,id'],
            'recap_stats_snapshot_json' => ['nullable', 'array'],
            'recap_published_at' => ['nullable', 'date'],
            'related_tags_json' => ['nullable', 'array', 'max:12'],
            'related_tags_json.*' => ['string', 'max:40'],
            'section_order_json' => ['nullable', 'array'],
            'section_order_json.*' => ['string', Rule::in($sectionKeys)],
            'section_visibility_json' => ['nullable', 'array'],
            'section_visibility_json.*' => ['boolean'],
            'parent_world_id' => ['nullable', 'integer', 'exists:worlds,id'],
            'linked_challenge_id' => ['nullable', 'integer', 'exists:group_challenges,id'],
            'show_linked_challenge_section' => ['nullable', 'boolean'],
            'show_linked_challenge_entries' => ['nullable', 'boolean'],
            'show_linked_challenge_winners' => ['nullable', 'boolean'],
            'show_linked_challenge_finalists' => ['nullable', 'boolean'],
            'auto_grant_challenge_world_rewards' => ['nullable', 'boolean'],
            'challenge_teaser_override' => ['nullable', 'string', 'max:2000'],
            'hidden_linked_challenge_artwork_ids_json' => ['nullable', 'array'],
            'hidden_linked_challenge_artwork_ids_json.*' => ['integer', 'distinct', 'exists:artworks,id'],
            'published_at' => ['nullable', 'date'],
            'relations' => ['nullable', 'array', 'max:60'],
            'relations.*.section_key' => ['required_with:relations', 'string', Rule::in($sectionKeys)],
            'relations.*.related_type' => ['required_with:relations', 'string', Rule::in($relationTypes)],
            'relations.*.related_id' => ['required_with:relations', 'integer', 'min:1'],
            'relations.*.context_label' => ['nullable', 'string', 'max:120'],
            'relations.*.sort_order' => ['nullable', 'integer', 'min:0'],
            'relations.*.is_featured' => ['nullable', 'boolean'],
        ];
    }

    public function withValidator($validator): void
    {
        $validator->after(function ($validator): void {
            $sections = (array) config('worlds.sections', []);
            $recurrenceKey = trim((string) $this->input('recurrence_key', ''));
            $editionYear = $this->input('edition_year');
            $hasRecurrenceSignals = $this->boolean('is_recurring')
                || $recurrenceKey !== ''
                || is_numeric($editionYear)
                || trim((string) $this->input('recurrence_rule', '')) !== '';

            if ($hasRecurrenceSignals && ! $this->boolean('is_recurring')) {
                $validator->errors()->add('is_recurring', 'Turn on recurrence when this world belongs to a recurring campaign family.');
            }

            if ($hasRecurrenceSignals) {
                if ($recurrenceKey === '') {
                    $validator->errors()->add('recurrence_key', 'Recurring worlds need a recurrence key such as halloween or retro-month.');
                }

                if (! is_numeric($editionYear)) {
                    $validator->errors()->add('edition_year', 'Recurring worlds need an edition year.');
                }
            }

            if ($recurrenceKey !== '' && ! preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/', $recurrenceKey)) {
                $validator->errors()->add('recurrence_key', 'Use lowercase letters, numbers, and dashes only.');
            }

            if ($hasRecurrenceSignals && $recurrenceKey !== '' && is_numeric($editionYear)) {
                $worldId = $this->route('world')?->id;
                $exists = World::query()
                    ->where('recurrence_key', $recurrenceKey)
                    ->where('edition_year', (int) $editionYear)
                    ->when($worldId, fn (Builder $builder) => $builder->where('id', '!=', $worldId))
                    ->exists();

                if ($exists) {
                    $validator->errors()->add('edition_year', 'That recurrence key already has an edition for this year.');
                }

                $startsAt = $this->filled('starts_at') ? Carbon::parse((string) $this->input('starts_at')) : null;
                $endsAt = $this->filled('ends_at') ? Carbon::parse((string) $this->input('ends_at')) : null;
                $wouldBeCurrentNow = (string) $this->input('status') === World::STATUS_PUBLISHED
                    && ($startsAt === null || ! $startsAt->isFuture())
                    && ($endsAt === null || ! $endsAt->isPast());

                if ($wouldBeCurrentNow) {
                    $hasCurrentConflict = World::query()
                        ->published()
                        ->where('recurrence_key', $recurrenceKey)
                        ->when($worldId, fn (Builder $builder) => $builder->where('id', '!=', $worldId))
                        ->get()
                        ->contains(fn (World $world): bool => $world->isCurrent());

                    if ($hasCurrentConflict) {
                        $validator->errors()->add('status', 'This recurrence family already has a current published edition. Archive it or move its dates before publishing another current edition.');
                    }
                }
            }

            foreach ((array) $this->input('relations', []) as $index => $relation) {
                $sectionKey = (string) ($relation['section_key'] ?? '');
                $relatedType = (string) ($relation['related_type'] ?? '');
                $allowed = (array) ($sections[$sectionKey]['relation_types'] ?? []);

                if ($sectionKey !== '' && $relatedType !== '' && $allowed !== [] && ! in_array($relatedType, $allowed, true)) {
                    $validator->errors()->add("relations.{$index}.related_type", 'That entity type cannot be attached to the selected section.');
                }
            }

            if ((string) $this->input('participation_mode') === World::PARTICIPATION_MODE_CLOSED && $this->boolean('accepts_submissions')) {
                $validator->errors()->add('accepts_submissions', 'Closed worlds cannot accept creator submissions.');
            }

            if ($this->boolean('is_homepage_featured') && ! $this->boolean('is_active_campaign')) {
                $validator->errors()->add('is_active_campaign', 'Homepage featured worlds must also be marked as active campaigns.');
            }

            $linkedChallengeId = (int) $this->input('linked_challenge_id', 0);

            if ($linkedChallengeId > 0) {
                $challenge = GroupChallenge::query()->with('group')->find($linkedChallengeId);

                if (! $challenge) {
                    $validator->errors()->add('linked_challenge_id', 'Select a valid linked challenge.');
                } elseif ((string) $challenge->status === GroupChallenge::STATUS_DRAFT && (string) $this->input('status') !== World::STATUS_DRAFT) {
                    $validator->errors()->add('linked_challenge_id', 'Draft challenges can only be linked while the world is still a draft.');
                } elseif ((string) $this->input('status') === World::STATUS_PUBLISHED && (string) $challenge->status === GroupChallenge::STATUS_ARCHIVED) {
                    $validator->errors()->add('linked_challenge_id', 'Archived challenges cannot be linked to a live world.');
                } elseif ((string) $this->input('status') === World::STATUS_PUBLISHED && ! $this->linkedChallengeMatchesWorldWindow($challenge)) {
                    $validator->errors()->add('linked_challenge_id', 'The linked challenge should overlap this world\'s intended campaign window.');
                }

                if ($challenge) {
                    $challengeArtworkIds = $challenge->artworkLinks()
                        ->pluck('artwork_id')
                        ->map(fn ($id): int => (int) $id)
                        ->all();

                    $invalidHiddenArtworkIds = collect((array) $this->input('hidden_linked_challenge_artwork_ids_json', []))
                        ->map(fn ($id): int => (int) $id)
                        ->filter(fn (int $id): bool => $id > 0)
                        ->diff($challengeArtworkIds)
                        ->values();

                    if ($invalidHiddenArtworkIds->isNotEmpty()) {
                        $validator->errors()->add('hidden_linked_challenge_artwork_ids_json', 'Hidden challenge entries must belong to the linked challenge.');
                    }
                }
            } elseif (collect((array) $this->input('hidden_linked_challenge_artwork_ids_json', []))->filter()->isNotEmpty()) {
                $validator->errors()->add('hidden_linked_challenge_artwork_ids_json', 'Hide specific challenge entries only after linking a primary challenge.');
            }
        });
    }

    private function linkedChallengeMatchesWorldWindow(GroupChallenge $challenge): bool
    {
        $worldStartsAt = $this->dateInput('promotion_starts_at') ?? $this->dateInput('starts_at');
        $worldEndsAt = $this->dateInput('promotion_ends_at') ?? $this->dateInput('ends_at');
        $challengeStartsAt = $challenge->start_at;
        $challengeEndsAt = $challenge->end_at;

        if (! $worldStartsAt && ! $worldEndsAt) {
            return true;
        }

        if (! $challengeStartsAt && ! $challengeEndsAt) {
            return true;
        }

        $effectiveWorldStart = $worldStartsAt ?? $worldEndsAt;
        $effectiveWorldEnd = $worldEndsAt ?? $worldStartsAt;
        $effectiveChallengeStart = $challengeStartsAt ?? $challengeEndsAt;
        $effectiveChallengeEnd = $challengeEndsAt ?? $challengeStartsAt;

        if (! $effectiveWorldStart || ! $effectiveWorldEnd || ! $effectiveChallengeStart || ! $effectiveChallengeEnd) {
            return true;
        }

        return $effectiveChallengeStart->lte($effectiveWorldEnd) && $effectiveChallengeEnd->gte($effectiveWorldStart);
    }

    private function dateInput(string $key): ?Carbon
    {
        $value = $this->input($key);

        if (! is_string($value) || trim($value) === '') {
            return null;
        }

        try {
            return Carbon::parse($value);
        } catch (\Throwable) {
            return null;
        }
    }
}