@model WritingPlansManagementViewModel
@{
    ViewData["Title"] = "Writing Plans";
    var message = TempData["WritingPlansMessage"] as string;
    var error = TempData["WritingPlansError"] as string;
}

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

<div class="page-heading compact">
    <div>
        <p class="eyebrow">Schedule admin</p>
        <h1>Writing Plans</h1>
        <p class="lead-text">Manage writing plans and their generated schedules.</p>
    </div>
    <div class="button-row">
        <a class="btn btn-primary" asp-action="ScheduleSetup">Create Writing Schedule</a>
        <a class="btn btn-outline-secondary" asp-action="SchedulePreview">Schedule Preview</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>
}

<section class="story-bible-section">
    <div class="story-bible-section-heading">
        <div>
            <p class="eyebrow">Plans</p>
            <h2>Writing Plans</h2>
        </div>
        <span class="soft-count">@Model.Plans.Count plans</span>
    </div>

    @if (!Model.Plans.Any())
    {
        <p class="muted">No writing plans found.</p>
    }
    else
    {
        <div class="table-responsive">
            <table class="table align-middle">
                <thead>
                    <tr>
                        <th>Plan Name</th>
                        <th>Project</th>
                        <th>Book</th>
                        <th>Goal Type</th>
                        <th>Start Date</th>
                        <th>Deadline Date</th>
                        <th>Availability</th>
                        <th>Active</th>
                        <th>Planned Tasks</th>
                        <th>Projected Finish Date</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.GoalType</td>
                            <td>@plan.StartDate.ToString("dd MMM yyyy")</td>
                            <td>@plan.DeadlineDate.ToString("dd MMM yyyy")</td>
                            <td>@plan.AvailabilitySummary</td>
                            <td>@(plan.IsActive ? "Yes" : "No")</td>
                            <td>@plan.PlannedTaskCount</td>
                            <td>@(plan.ProjectedFinishDate?.ToString("dd MMM yyyy") ?? "-")</td>
                            <td>
                                <div class="button-row">
                                    <a class="btn btn-sm btn-outline-secondary" asp-action="SchedulePreview" asp-route-writingPlanId="@plan.WritingPlanID">View Schedule</a>
                                    <a class="btn btn-sm btn-outline-primary" asp-action="EditWritingPlan" asp-route-writingPlanId="@plan.WritingPlanID">Edit Plan</a>
                                    @if (plan.IsActive)
                                    {
                                        <form asp-action="DeactivateWritingPlan" method="post">
                                            <input type="hidden" name="writingPlanId" value="@plan.WritingPlanID" />
                                            <button class="btn btn-sm btn-outline-secondary" type="submit">Deactivate Plan</button>
                                        </form>
                                    }
                                    else
                                    {
                                        <form asp-action="ActivateWritingPlan" method="post">
                                            <input type="hidden" name="writingPlanId" value="@plan.WritingPlanID" />
                                            <button class="btn btn-sm btn-outline-secondary" type="submit">Activate Plan</button>
                                        </form>
                                    }
                                    <form asp-action="RegenerateWritingPlanSchedule" method="post">
                                        <input type="hidden" name="writingPlanId" value="@plan.WritingPlanID" />
                                        <button class="btn btn-sm btn-outline-primary" type="submit">Regenerate Schedule</button>
                                    </form>
                                    <button class="btn btn-sm btn-outline-danger" type="button" data-bs-toggle="modal" data-bs-target="#deleteWritingPlanModal-@plan.WritingPlanID">Delete Plan</button>
                                </div>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>

        @foreach (var plan in Model.Plans)
        {
            <div class="modal fade" id="deleteWritingPlanModal-@plan.WritingPlanID" tabindex="-1" aria-labelledby="deleteWritingPlanModalTitle-@plan.WritingPlanID" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h2 class="modal-title fs-5" id="deleteWritingPlanModalTitle-@plan.WritingPlanID">Delete Writing Plan</h2>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <p>Delete <strong>@plan.PlanName</strong>?</p>
                            <p class="muted mb-0">The writing plan and its associated generated schedule items will be deleted. Scenes and chapters themselves will not be deleted.</p>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                            <form asp-action="DeleteWritingPlan" method="post" class="m-0" data-no-delete-confirm="true">
                                <input type="hidden" name="writingPlanId" value="@plan.WritingPlanID" />
                                <button class="btn btn-danger" type="submit">Delete</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        }
    }
</section>
