@model ManuscriptScanReviewViewModel
@{
    ViewData["Title"] = "Review manuscript scan";
    var probableCharacters = Model.CharacterCandidates
        .Where(candidate => string.Equals(candidate.Category, "ProbableCharacter", StringComparison.OrdinalIgnoreCase))
        .ToList();
    var possibleCharacters = Model.CharacterCandidates
        .Where(candidate => string.Equals(candidate.Category, "PossibleCharacter", StringComparison.OrdinalIgnoreCase))
        .ToList();
    var relationshipTitles = Model.CharacterCandidates
        .Where(candidate => string.Equals(candidate.Category, "RelationshipTitle", StringComparison.OrdinalIgnoreCase))
        .ToList();
    var excludedCharacters = Model.CharacterCandidates
        .Where(candidate => string.Equals(candidate.Category, "Excluded", StringComparison.OrdinalIgnoreCase))
        .ToList();
}

<section class="onboarding-shell" aria-labelledby="scan-review-title">
    <form class="onboarding-panel onboarding-review-panel" asp-controller="Onboarding" asp-action="SaveScanReview" method="post">
        @Html.AntiForgeryToken()
        <input type="hidden" asp-for="PreviewID" name="PreviewID" />
        <partial name="_OnboardingJourneyHeader" model="@(new OnboardingJourneyHeaderViewModel { CurrentStep = 7 })" />

        <div class="onboarding-copy">
            <p class="eyebrow">Step 7 of 9</p>
            <h1 id="scan-review-title">Review Chapters</h1>
            <p>Review the chapters PlotDirector found before Story Intelligence begins.</p>
            <p>Scene and character scan details are shown for context only in this phase.</p>
        </div>

        @if (TempData["OnboardingReviewMessage"] is string reviewMessage)
        {
            <div class="alert alert-success">@reviewMessage</div>
        }
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>

        <div class="onboarding-scan-counts onboarding-review-counts">
            <div>
                <span>Chapters</span>
                <strong>@Model.SelectedChapterCount / @Model.ChapterCount</strong>
            </div>
            <div>
                <span>Scenes</span>
                <strong>@Model.SelectedSceneCount / @Model.SceneCount</strong>
            </div>
            <div>
                <span>Characters</span>
                <strong>@Model.SelectedCharacterCount / @Model.CharacterCandidateCount</strong>
            </div>
            <div>
                <span>Words</span>
                <strong>@Model.SelectedWordCount.ToString("N0")</strong>
            </div>
        </div>

        <div class="onboarding-review-tools" data-review-tools>
            <button class="btn btn-sm btn-outline-secondary" type="button" data-bulk-target="chapter" data-bulk-state="true">Include all chapters</button>
            <button class="btn btn-sm btn-outline-secondary" type="button" data-bulk-target="chapter" data-bulk-state="false">Exclude all chapters</button>
            <button class="btn btn-sm btn-outline-secondary" type="button" data-bulk-target="scene" data-bulk-state="true">Include all scenes</button>
            <button class="btn btn-sm btn-outline-secondary" type="button" data-bulk-target="scene" data-bulk-state="false">Exclude all scenes</button>
            <button class="btn btn-sm btn-outline-secondary" type="reset">Restore scan defaults</button>
        </div>

        <section class="onboarding-review-grid" aria-label="Scan preview">
            <div class="onboarding-review-section">
                <h2>Chapters and scenes</h2>
                @if (!Model.Chapters.Any())
                {
                    <p>No chapters were detected. Make sure your chapter headings use Heading 1, or add clear chapter headings.</p>
                }
                else
                {
                    <div class="onboarding-review-chapters">
                        @{
                            var chapterIndex = 0;
                            var sceneIndex = 0;
                        }
                        @foreach (var chapter in Model.Chapters)
                        {
                            <details class="onboarding-review-chapter" open>
                                <summary>
                                    <span>Chapter @chapter.ChapterNumber</span>
                                    <strong>@chapter.ReviewTitle</strong>
                                    <small>@chapter.WordCount.ToString("N0") words / @chapter.Scenes.Count scene@(chapter.Scenes.Count == 1 ? string.Empty : "s")</small>
                                </summary>
                                <input type="hidden" name="Chapters[@chapterIndex].TemporaryChapterKey" value="@chapter.TemporaryChapterKey" />
                                <input type="hidden" name="Chapters[@chapterIndex].ChapterNumber" value="@chapter.ChapterNumber" />
                                <label class="onboarding-review-toggle">
                                    <input type="checkbox" name="Chapters[@chapterIndex].Include" value="true" checked="@chapter.Include" data-review-kind="chapter" />
                                    <input type="hidden" name="Chapters[@chapterIndex].Include" value="false" />
                                    Include chapter
                                </label>
                                <label class="onboarding-review-field">
                                    Chapter title
                                    <input class="form-control" name="Chapters[@chapterIndex].Title" value="@chapter.ReviewTitle" />
                                </label>

                                @if (chapter.Scenes.Any())
                                {
                                    <ol>
                                        @foreach (var scene in chapter.Scenes)
                                        {
                                            <li>
                                                <input type="hidden" name="Scenes[@sceneIndex].TemporarySceneKey" value="@scene.TemporarySceneKey" />
                                                <input type="hidden" name="Scenes[@sceneIndex].TemporaryChapterKey" value="@scene.TemporaryChapterKey" />
                                                <input type="hidden" name="Scenes[@sceneIndex].SceneNumberWithinChapter" value="@scene.SceneNumberWithinChapter" />
                                                <label class="onboarding-review-toggle">
                                                    <input type="checkbox" name="Scenes[@sceneIndex].Include" value="true" checked="@scene.Include" data-review-kind="scene" />
                                                    <input type="hidden" name="Scenes[@sceneIndex].Include" value="false" />
                                                    Include scene
                                                </label>
                                                <label class="onboarding-review-field">
                                                    Scene title
                                                    <input class="form-control" name="Scenes[@sceneIndex].Title" value="@scene.ReviewTitle" placeholder="Scene @scene.SceneNumberWithinChapter" />
                                                </label>
                                                <span>@scene.WordCount.ToString("N0") words</span>
                                                @if (scene.WordCount == 0)
                                                {
                                                    <label class="onboarding-review-toggle">
                                                        <input type="checkbox" name="Scenes[@sceneIndex].AllowZeroWords" value="true" />
                                                        <input type="hidden" name="Scenes[@sceneIndex].AllowZeroWords" value="false" />
                                                        Allow empty scene
                                                    </label>
                                                }
                                                @if (!string.IsNullOrWhiteSpace(scene.OpeningTextPreview))
                                                {
                                                    <p>@scene.OpeningTextPreview</p>
                                                }
                                            </li>
                                            sceneIndex++;
                                        }
                                    </ol>
                                }
                                else
                                {
                                    <p>No scene breaks were detected. PlotDirector will treat this chapter as one scene for now.</p>
                                }
                            </details>
                            chapterIndex++;
                        }
                    </div>
                }
            </div>

            <aside class="onboarding-review-section">
                <h2>Character candidates</h2>
                @if (!probableCharacters.Any() && !possibleCharacters.Any() && !relationshipTitles.Any())
                {
                    <p>No repeated character names were found yet. You can still prepare chapters and add characters later.</p>
                }
                else
                {
                    var characterIndex = 0;
                    <label class="onboarding-character-filter">
                        Find a candidate
                        <input class="form-control" type="search" placeholder="Search names or notes" data-character-filter />
                    </label>
                    <p class="onboarding-empty-state" data-character-empty hidden>No matching candidates.</p>
                    <div class="onboarding-character-groups">
                        @foreach (var group in new[]
                        {
                            new { Title = "Probable characters", Candidates = probableCharacters },
                            new { Title = "Possible characters", Candidates = possibleCharacters },
                            new { Title = "Relationship titles", Candidates = relationshipTitles }
                        })
                        {
                            if (!group.Candidates.Any())
                            {
                                continue;
                            }

                            <section>
                                <h3>@group.Title</h3>
                                <ul class="onboarding-character-candidates">
                                    @foreach (var candidate in group.Candidates)
                                    {
                                        <li data-character-name="@candidate.ReviewName" data-character-reason="@candidate.Reason">
                                            <input type="hidden" name="Characters[@characterIndex].TemporaryCharacterKey" value="@candidate.TemporaryCharacterKey" />
                                            <input type="hidden" name="Characters[@characterIndex].Category" value="@candidate.Category" />
                                            <label class="onboarding-review-toggle">
                                                <input type="checkbox" name="Characters[@characterIndex].Include" value="true" checked="@candidate.Include" />
                                                <input type="hidden" name="Characters[@characterIndex].Include" value="false" />
                                                Include
                                            </label>
                                            <label class="onboarding-review-field">
                                                Name
                                                <input class="form-control" name="Characters[@characterIndex].Name" value="@candidate.ReviewName" />
                                            </label>
                                            <label class="onboarding-review-field">
                                                Existing character
                                                <select class="form-select" name="Characters[@characterIndex].ExistingCharacterID">
                                                    <option value="">Create later if imported</option>
                                                    @foreach (var option in Model.ExistingCharacterOptions)
                                                    {
                                                        <option value="@option.CharacterID" selected="@(candidate.ExistingCharacterID == option.CharacterID)">@option.CharacterName</option>
                                                    }
                                                </select>
                                            </label>
                                            <span>@candidate.MentionCount mention@(candidate.MentionCount == 1 ? string.Empty : "s") / score @candidate.QualityScore</span>
                                            @if (!string.IsNullOrWhiteSpace(candidate.ExistingCharacterName))
                                            {
                                                <small>Matches existing character: @candidate.ExistingCharacterName</small>
                                            }
                                            @if (!string.IsNullOrWhiteSpace(candidate.Reason))
                                            {
                                                <small>@candidate.Reason</small>
                                            }
                                        </li>
                                        characterIndex++;
                                    }
                                </ul>
                            </section>
                        }
                        @if (excludedCharacters.Any())
                        {
                            <details class="onboarding-excluded-candidates">
                                <summary>Show excluded candidates</summary>
                                <section>
                                    <h3>Excluded candidates</h3>
                                    <ul class="onboarding-character-candidates">
                                        @foreach (var candidate in excludedCharacters)
                                        {
                                            <li data-character-name="@candidate.ReviewName" data-character-reason="@candidate.Reason">
                                                <input type="hidden" name="Characters[@characterIndex].TemporaryCharacterKey" value="@candidate.TemporaryCharacterKey" />
                                                <input type="hidden" name="Characters[@characterIndex].Category" value="@candidate.Category" />
                                                <label class="onboarding-review-toggle">
                                                    <input type="checkbox" name="Characters[@characterIndex].Include" value="true" checked="@candidate.Include" />
                                                    <input type="hidden" name="Characters[@characterIndex].Include" value="false" />
                                                    Include
                                                </label>
                                                <label class="onboarding-review-field">
                                                    Name
                                                    <input class="form-control" name="Characters[@characterIndex].Name" value="@candidate.ReviewName" />
                                                </label>
                                                <label class="onboarding-review-field">
                                                    Existing character
                                                    <select class="form-select" name="Characters[@characterIndex].ExistingCharacterID">
                                                        <option value="">Create later if imported</option>
                                                        @foreach (var option in Model.ExistingCharacterOptions)
                                                        {
                                                            <option value="@option.CharacterID" selected="@(candidate.ExistingCharacterID == option.CharacterID)">@option.CharacterName</option>
                                                        }
                                                    </select>
                                                </label>
                                                <span>@candidate.MentionCount mention@(candidate.MentionCount == 1 ? string.Empty : "s") / score @candidate.QualityScore</span>
                                                @if (!string.IsNullOrWhiteSpace(candidate.Reason))
                                                {
                                                    <small>@candidate.Reason</small>
                                                }
                                            </li>
                                            characterIndex++;
                                        }
                                    </ul>
                                </section>
                            </details>
                        }
                    </div>
                }
            </aside>
        </section>

        <div class="onboarding-actions">
            <a class="btn btn-outline-secondary" asp-controller="Onboarding" asp-action="Index">Back to setup</a>
            <button class="btn btn-primary" type="submit" name="intent" value="continue">Save and continue</button>
        </div>
    </form>
</section>

<script>
    const reviewTools = document.querySelector("[data-review-tools]");
    reviewTools?.addEventListener("click", (event) => {
        const button = event.target.closest("[data-bulk-target]");
        if (!button) {
            return;
        }

        const target = button.dataset.bulkTarget;
        const checked = button.dataset.bulkState === "true";
        document.querySelectorAll(`[data-review-kind="${target}"]`).forEach((checkbox) => {
            checkbox.checked = checked;
        });
    });

    const characterFilter = document.querySelector("[data-character-filter]");
    characterFilter?.addEventListener("input", () => {
        const query = characterFilter.value.trim().toLowerCase();
        let visibleCount = 0;
        document.querySelectorAll("[data-character-name]").forEach((item) => {
            const text = `${item.dataset.characterName || ""} ${item.dataset.characterReason || ""}`.toLowerCase();
            const isVisible = query.length === 0 || text.includes(query);
            item.hidden = !isVisible;
            if (isVisible) {
                visibleCount++;
            }
        });

        document.querySelectorAll(".onboarding-character-groups section").forEach((section) => {
            const hasVisibleItems = [...section.querySelectorAll("[data-character-name]")].some((item) => !item.hidden);
            section.hidden = !hasVisibleItems;
        });

        const emptyState = document.querySelector("[data-character-empty]");
        if (emptyState) {
            emptyState.hidden = visibleCount > 0;
        }
    });
</script>
