@model SceneEditViewModel
@{
    var completed = Model.ChecklistItems.Count(x => x.IsCompleted);
    var total = Model.ChecklistItems.Count;
    var checklistLabel = total == 0 ? "No checklist yet" : $"{completed} of {total} complete";
    var incompleteChecklist = Model.ChecklistItems.Where(x => !x.IsCompleted).Take(3).ToList();

    static string? FileHref(string? filePath)
    {
        if (string.IsNullOrWhiteSpace(filePath))
        {
            return null;
        }

        return filePath.StartsWith("http", StringComparison.OrdinalIgnoreCase)
            ? filePath
            : filePath.StartsWith("/")
                ? filePath
                : !System.IO.Path.IsPathRooted(filePath)
                    ? "/" + filePath.TrimStart('/', '\\')
                    : null;
    }
}

<article class="scene-inspector-v2-card" data-scene-summary-part="writer">
    <div class="scene-inspector-v2-card-header">
        <div>
            <p class="eyebrow">Writer</p>
            <div class="scene-inspector-v2-heading">
                <h3>Drafting support</h3>
                <help-icon key="sceneInspector.writer" 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="notes" data-scene-id="@Model.SceneID" data-scene-editor-url="@Url.Action("SceneNotesEditor", "Scenes", new { sceneId = Model.SceneID })">Edit Writer</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 writer editor actions</span>
                </button>
                <div class="dropdown-menu dropdown-menu-end">
                    <button type="button" class="dropdown-item" data-scene-editor="notes" data-scene-id="@Model.SceneID" data-scene-editor-url="@Url.Action("SceneNotesEditor", "Scenes", new { sceneId = Model.SceneID })">Notes</button>
                    <button type="button" class="dropdown-item" data-scene-editor="checklist" data-scene-id="@Model.SceneID" data-scene-editor-url="@Url.Action("SceneChecklistEditor", "Scenes", new { sceneId = Model.SceneID })">Checklist</button>
                    <button type="button" class="dropdown-item" data-scene-editor="workflow" data-scene-id="@Model.SceneID" data-scene-editor-url="@Url.Action("SceneWorkflowEditor", "Scenes", new { sceneId = Model.SceneID })">Workflow</button>
                    <button type="button" class="dropdown-item" data-scene-editor="attachments" data-scene-id="@Model.SceneID" data-scene-editor-url="@Url.Action("SceneAttachmentsEditor", "Scenes", new { sceneId = Model.SceneID })">Attachments</button>
                </div>
            </div>
        </div>
    </div>

    @if (Model.Workflow.IsBlocked)
    {
        <div class="scene-inspector-v2-callout">
            <strong>Blocked</strong>
            <p>@(string.IsNullOrWhiteSpace(Model.Workflow.BlockedReason) ? "No blocked reason has been recorded." : Model.Workflow.BlockedReason)</p>
        </div>
    }

    <div class="scene-inspector-v2-preview-stack">
        <div class="scene-inspector-v2-section">
            <span>Notes</span>
            @if (!Model.Notes.Any())
            {
                <p class="muted">No drafting notes yet.</p>
            }
            else
            {
                <ul class="scene-inspector-v2-compact-list">
                    @foreach (var note in Model.Notes.OrderByDescending(x => x.IsPinned).ThenByDescending(x => x.UpdatedDate).Take(3))
                    {
                        <li>@(string.IsNullOrWhiteSpace(note.NoteTitle) ? note.NoteText : note.NoteTitle)</li>
                    }
                </ul>
            }
        </div>

        <div class="scene-inspector-v2-section">
            <span>Checklist</span>
            @if (!Model.ChecklistItems.Any())
            {
                <p class="muted">No checklist items yet.</p>
            }
            else if (!incompleteChecklist.Any())
            {
                <p class="scene-inspector-v2-good-state">All @total checklist item@(total == 1 ? "" : "s") complete.</p>
            }
            else
            {
                <p class="muted">@checklistLabel</p>
                <ul class="scene-inspector-v2-compact-list">
                    @foreach (var item in incompleteChecklist)
                    {
                        <li>@item.ItemText</li>
                    }
                </ul>
            }
        </div>

        <div class="scene-inspector-v2-section">
            <span>Attachments</span>
            @if (!Model.Attachments.Any())
            {
                <p class="muted">No scene references attached yet.</p>
            }
            else
            {
                <ul class="scene-inspector-v2-compact-list">
                    @foreach (var attachment in Model.Attachments.Take(3))
                    {
                        var href = FileHref(attachment.FilePath);
                        <li>
                            @if (!string.IsNullOrWhiteSpace(href))
                            {
                                <a href="@href" target="_blank" rel="noopener">@attachment.Title</a>
                            }
                            else if (!string.IsNullOrWhiteSpace(attachment.ExternalUrl))
                            {
                                <a href="@attachment.ExternalUrl" target="_blank" rel="noopener">@attachment.Title</a>
                            }
                            else
                            {
                                <span>@attachment.Title</span>
                            }
                        </li>
                    }
                </ul>
            }
        </div>
    </div>
</article>
