@model SceneEditViewModel
@{
    var povCharacterId = Model.POVCharacterID
        ?? Model.SceneCharacters.FirstOrDefault(x => x.RoleInSceneTypeName?.Contains("POV", StringComparison.OrdinalIgnoreCase) == true)?.CharacterID;
    var suggestionCount = Model.CharacterSuggestions.Count + Model.AssetSuggestions.Count + Model.LocationSuggestions.Count;
    static string PreviewText(string? value, string fallback) => string.IsNullOrWhiteSpace(value) ? fallback : value.Trim();
}

<article class="scene-inspector-v2-card" data-scene-summary-part="people">
    <div class="scene-inspector-v2-card-header">
        <div>
            <p class="eyebrow">People</p>
            <div class="scene-inspector-v2-heading">
                <h3>Characters and relationships</h3>
                <help-icon key="sceneInspector.characters" mode="inline" />
            </div>
        </div>
        <div class="scene-inspector-v2-actions">
            <div class="btn-group scene-inspector-v2-action-menu">
                <button type="button" class="btn btn-sm btn-outline-primary" data-scene-editor="characters" data-scene-id="@Model.SceneID" data-scene-editor-url="@Url.Action("SceneCharactersEditor", "Scenes", new { sceneId = Model.SceneID })">Edit People</button>
                <button type="button" class="btn btn-sm btn-outline-primary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
                    <span class="visually-hidden">Show people editor actions</span>
                </button>
                <div class="dropdown-menu dropdown-menu-end">
                    <button type="button" class="dropdown-item" data-scene-editor="characters" data-scene-id="@Model.SceneID" data-scene-editor-url="@Url.Action("SceneCharactersEditor", "Scenes", new { sceneId = Model.SceneID })">Characters</button>
                    <button type="button" class="dropdown-item" data-scene-editor="attributes" data-scene-id="@Model.SceneID" data-scene-editor-url="@Url.Action("SceneCharacterAttributesEditor", "Scenes", new { sceneId = Model.SceneID })">Attributes</button>
                    <button type="button" class="dropdown-item" data-scene-editor="suggestions" data-scene-id="@Model.SceneID" data-scene-editor-url="@Url.Action("SceneSuggestionsEditor", "Scenes", new { sceneId = Model.SceneID })">Suggestions</button>
                    <button type="button" class="dropdown-item" data-scene-editor="knowledge" data-scene-id="@Model.SceneID" data-scene-editor-url="@Url.Action("SceneKnowledgeEditor", "Scenes", new { sceneId = Model.SceneID })">Knowledge</button>
                    <button type="button" class="dropdown-item" data-scene-editor="relationships" data-scene-id="@Model.SceneID" data-scene-editor-url="@Url.Action("SceneRelationshipsEditor", "Scenes", new { sceneId = Model.SceneID })">Relationships</button>
                </div>
            </div>
        </div>
    </div>

    <div class="scene-inspector-v2-section">
        <span>Characters in scene</span>
        @if (!Model.SceneCharacters.Any())
        {
            <p class="muted">No characters have been added to this scene.</p>
        }
        else
        {
            <ul class="scene-inspector-v2-character-list">
                @foreach (var character in Model.SceneCharacters.Take(6))
                {
                    var isPov = character.CharacterID == povCharacterId;
                    <li>
                        <partial name="~/Views/Shared/_Avatar.cshtml" model="@(new AvatarViewModel { DisplayName = character.CharacterName, ImagePath = character.CharacterImagePath, ThumbnailPath = character.CharacterThumbnailPath, Size = "sm", CssClass = isPov ? "scene-inspector-v2-avatar-pov" : "" })" />
                        <div>
                            <strong>@character.CharacterName</strong>
                            <span>@(isPov ? "Point of view" : character.RoleInSceneTypeName ?? character.PresenceTypeName ?? "In scene")</span>
                        </div>
                        @if (isPov)
                        {
                            <span class="scene-inspector-v2-mini-badge">POV</span>
                        }
                    </li>
                }
            </ul>
            @if (Model.SceneCharacters.Count > 6)
            {
                <p class="muted">+ @(Model.SceneCharacters.Count - 6) more</p>
            }
        }
    </div>

    <div class="scene-inspector-v2-preview-stack">
        @if (Model.CharacterKnowledge.Any())
        {
            <details class="scene-inspector-v2-preview-group" open>
                <summary>Knowledge changes <span>@Model.CharacterKnowledge.Count</span></summary>
                <ul class="scene-inspector-v2-compact-list">
                    @foreach (var item in Model.CharacterKnowledge.Take(3))
                    {
                        <li><strong>@item.CharacterName</strong> @PreviewText(item.Description, $"is marked as {item.KnowledgeStateName}.")</li>
                    }
                </ul>
            </details>
        }
        @if (Model.RelationshipEvents.Any())
        {
            <details class="scene-inspector-v2-preview-group">
                <summary>Relationship changes <span>@Model.RelationshipEvents.Count</span></summary>
                <ul class="scene-inspector-v2-compact-list">
                    @foreach (var item in Model.RelationshipEvents.Take(3))
                    {
                        <li><strong>@item.CharacterAName &harr; @item.CharacterBName</strong> @PreviewText(item.Description, $"{item.RelationshipStateName} / {item.RelationshipTypeName}.")</li>
                    }
                </ul>
            </details>
        }
        @if (Model.CharacterAttributeEvents.Any())
        {
            <details class="scene-inspector-v2-preview-group">
                <summary>Attribute changes <span>@Model.CharacterAttributeEvents.Count</span></summary>
                <ul class="scene-inspector-v2-compact-list">
                    @foreach (var item in Model.CharacterAttributeEvents.Take(3))
                    {
                        <li><strong>@item.CharacterName</strong> @item.AttributeName: @item.AttributeValue</li>
                    }
                </ul>
            </details>
        }
        @if (suggestionCount > 0)
        {
            <div class="scene-inspector-v2-suggestion-strip">
                <strong>@suggestionCount</strong>
                <span>Word Companion suggestion@(suggestionCount == 1 ? "" : "s") pending</span>
            </div>
        }
    </div>
</article>
