@model WritingSchedulePreviewViewModel
@{
    ViewData["Title"] = "Schedule Preview";
    var message = TempData["SchedulePreviewMessage"] as string;
    var error = TempData["SchedulePreviewError"] as string;
}

<nav class="breadcrumb-trail" aria-label="Breadcrumb">
    <a asp-controller="Writer" asp-action="Index">Writer Workspace</a>
    <a asp-controller="Writer" asp-action="WritingPlans">Writing Plans</a>
    <span>Schedule Preview</span>
</nav>

<div class="page-heading compact">
    <div>
        <p class="eyebrow">Schedule</p>
        <h1>Schedule Preview</h1>
        <p class="lead-text">Generated writing schedule items.</p>
    </div>
    <div class="button-row">
        <a class="btn btn-outline-secondary" asp-action="WritingPlans">Back to Writing Plans</a>
        @if (Model.SelectedPlan is not null)
        {
            <a class="btn btn-outline-primary" asp-action="EditWritingPlan" asp-route-writingPlanId="@Model.SelectedPlan.WritingPlanID">Edit Plan</a>
        }
    </div>
</div>

@if (!string.IsNullOrWhiteSpace(message))
{
    <div class="alert alert-info">@message</div>
}

@if (!string.IsNullOrWhiteSpace(error))
{
    <div class="alert alert-warning">@error</div>
}

@if (!Model.Plans.Any())
{
    <section class="story-bible-section">
        <p class="muted">No writing plans found.</p>
        <a class="btn btn-primary" asp-action="ScheduleSetup">Create Writing Schedule</a>
    </section>
}
else
{
    <section class="story-bible-section">
        <div class="story-bible-section-heading">
            <div>
                <p class="eyebrow">Plan</p>
                <h2>Writing Plan Selection</h2>
            </div>
        </div>

        <form asp-action="SchedulePreview" method="get" class="story-bible-search mb-3">
            <select class="form-select" name="writingPlanId">
                @foreach (var plan in Model.Plans)
                {
                    <option value="@plan.WritingPlanID" selected="@(plan.WritingPlanID == Model.SelectedWritingPlanID)">
                        @plan.PlanName - @plan.ProjectName / @(string.IsNullOrWhiteSpace(plan.BookDisplayTitle) ? "All Books" : plan.BookDisplayTitle) - @plan.DeadlineDate.ToString("dd MMM yyyy") - @(plan.IsActive ? "Active" : "Inactive")
                    </option>
                }
            </select>
            <button class="btn btn-primary" type="submit">Select</button>
        </form>

        <div class="table-responsive">
            <table class="table align-middle">
                <thead>
                    <tr>
                        <th>Plan Name</th>
                        <th>Project</th>
                        <th>Book</th>
                        <th>Deadline Date</th>
                        <th>Active</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var plan in Model.Plans)
                    {
                        <tr>
                            <td>@plan.PlanName</td>
                            <td>@plan.ProjectName</td>
                            <td>@(string.IsNullOrWhiteSpace(plan.BookDisplayTitle) ? "All Books" : plan.BookDisplayTitle)</td>
                            <td>@plan.DeadlineDate.ToString("dd MMM yyyy")</td>
                            <td>@(plan.IsActive ? "Yes" : "No")</td>
                            <td>
                                @if (plan.WritingPlanID == Model.SelectedWritingPlanID)
                                {
                                    <span class="status-pill">Selected</span>
                                }
                                else
                                {
                                    <a asp-action="SchedulePreview" asp-route-writingPlanId="@plan.WritingPlanID">Select</a>
                                }
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>
    </section>

    @if (Model.SelectedPlan is not null)
    {
        @if (Model.SelectedPlanRequiresRebalance)
        {
            <div class="alert alert-warning">Schedule requires attention.</div>
        }

        <section class="story-bible-section">
            <div class="story-bible-section-heading">
                <div>
                    <p class="eyebrow">Summary</p>
                    <h2>Schedule Summary</h2>
                </div>
            </div>
            <div class="story-bible-grid story-bible-grid-compact">
                <article class="story-bible-card"><h3>Total Tasks</h3><strong class="writer-kpi">@Model.TotalTasks</strong></article>
                <article class="story-bible-card"><h3>Draft Tasks</h3><strong class="writer-kpi">@Model.DraftTasks</strong></article>
                <article class="story-bible-card"><h3>Revision Tasks</h3><strong class="writer-kpi">@Model.RevisionTasks</strong></article>
                <article class="story-bible-card"><h3>Polish Tasks</h3><strong class="writer-kpi">@Model.PolishTasks</strong></article>
                <article class="story-bible-card"><h3>Total Estimated Minutes</h3><strong class="writer-kpi">@Model.TotalEstimatedMinutes</strong></article>
                <article class="story-bible-card"><h3>Projected Finish Date</h3><strong class="writer-kpi">@(Model.ProjectedFinishDate?.ToString("dd MMM yyyy") ?? "-")</strong></article>
            </div>
        </section>

        <section class="story-bible-section">
            <div class="story-bible-section-heading">
                <div>
                    <p class="eyebrow">Items</p>
                    <h2>Schedule Preview Table</h2>
                </div>
                <span class="soft-count">@Model.Items.Count items</span>
            </div>

            @if (!Model.Items.Any())
            {
                <p class="muted">No schedule generated.</p>
            }
            else
            {
                <div class="table-responsive">
                    <table class="table align-middle">
                        <thead>
                            <tr>
                                <th>Scheduled Date</th>
                                <th>Task Type</th>
                                <th>Book</th>
                                <th>Chapter</th>
                                <th>Scene</th>
                                <th>Priority</th>
                                <th>Estimated Minutes</th>
                                <th>Target Words</th>
                                <th>Schedule Status</th>
                                <th>Notes</th>
                                <th>Blocked</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.Items)
                            {
                                <tr>
                                    <td>@item.ScheduledDate.ToString("dd MMM yyyy")</td>
                                    <td>@item.TaskType</td>
                                    <td>@item.BookDisplayTitle</td>
                                    <td>Chapter @item.ChapterNumber: @item.ChapterTitle</td>
                                    <td>Scene @item.SceneNumber: @item.SceneTitle</td>
                                    <td>@(item.Priority?.ToString() ?? "-")</td>
                                    <td>@(item.EstimatedMinutes?.ToString() ?? "-")</td>
                                    <td>@(item.TargetWords?.ToString() ?? "-")</td>
                                    <td>@item.ScheduleStatus</td>
                                    <td>@(item.Notes ?? "-")</td>
                                    <td>@(item.IsBlocked ? "Yes" : "No")</td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            }
        </section>
    }
}
