@model ProjectRestorePointListViewModel
@{
    ViewData["Title"] = "Restore Points";
    var defaultRestorePointName = $"Manual restore point - {DateTime.Now:yyyy-MM-dd HH:mm}";
    var restorePointLimitReached = Model.RestorePoints.Count >= 10;
    const string restorePointLimitMessage = "You have reached the limit of 10 restore points for this project. Delete an old restore point before creating a new one. You can download a restore point first if you want to keep a local backup copy.";
}

@functions {
    private static string FormatSize(long bytes)
    {
        if (bytes >= 1024 * 1024)
        {
            return $"{bytes / 1024d / 1024d:0.0} MB";
        }

        if (bytes >= 1024)
        {
            return $"{bytes / 1024d:0.0} KB";
        }

        return $"{bytes} bytes";
    }
}

<nav class="breadcrumb-trail">
    <a asp-controller="Projects" asp-action="Details" asp-route-id="@Model.Project.ProjectID">@Model.Project.ProjectName</a>
    <span>Restore Points</span>
</nav>

<div class="page-heading compact">
    <div>
        <p class="eyebrow">Project backup</p>
        <h1>Restore Points <help-icon key="restorePoints.overview" /></h1>
        <p class="lead-text">Create manual database-saved snapshots of the project export JSON.</p>
    </div>
    <div class="button-row">
        <a class="btn btn-outline-secondary" asp-controller="Projects" asp-action="Details" asp-route-id="@Model.Project.ProjectID">Back to project</a>
        <a class="btn btn-outline-secondary" asp-controller="Exports" asp-action="ProjectBackup" asp-route-projectId="@Model.Project.ProjectID">Download Project Backup</a>
    </div>
</div>

@if (TempData["ProjectRestorePointMessage"] is string message)
{
    <div class="alert alert-info">@message</div>
}

<section class="story-bible-section">
    <div class="story-bible-section-heading">
        <div>
            <p class="eyebrow">Manual snapshot</p>
            <h2>Create restore point</h2>
        </div>
    </div>
    @if (restorePointLimitReached)
    {
        <div class="alert alert-warning">@restorePointLimitMessage</div>
    }
    <form asp-action="Create" method="post" class="row g-3" data-confirm-message="Create restore point?&#10;&#10;This will save the current state of this project as a restore point. You can return to it later if you need to undo major changes." data-confirm-button-text="Create Restore Point" data-confirm-button-class="btn-primary">
        <input type="hidden" name="projectId" value="@Model.Project.ProjectID" />
        <div class="col-md-5">
            <label class="form-label" for="restorePointName">Name</label>
            <input class="form-control" id="restorePointName" name="restorePointName" maxlength="255" placeholder="@defaultRestorePointName" disabled="@restorePointLimitReached" />
        </div>
        <div class="col-md-5">
            <label class="form-label" for="notes">Notes</label>
            <input class="form-control" id="notes" name="notes" maxlength="1000" placeholder="Optional reason or reminder" disabled="@restorePointLimitReached" />
        </div>
        <div class="col-md-2 d-flex align-items-end">
            <button class="btn btn-primary w-100" type="submit" disabled="@restorePointLimitReached">Create Restore Point</button>
        </div>
    </form>
</section>

<section class="list-section">
    <h2>Saved restore points</h2>
    @if (!Model.RestorePoints.Any())
    {
        <p class="muted">No restore points yet.</p>
    }
    else
    {
        <div class="table-responsive">
            <table class="table align-middle">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Type</th>
                        <th>Created</th>
                        <th>Size</th>
                        <th>Notes</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var restorePoint in Model.RestorePoints)
                    {
                        <tr>
                            <td>@restorePoint.RestorePointName</td>
                            <td><span class="status-pill">@restorePoint.RestorePointType</span></td>
                            <td>@restorePoint.CreatedDateUtc.ToLocalTime().ToString("dd MMM yyyy HH:mm")</td>
                            <td>@FormatSize(restorePoint.JsonSizeBytes)</td>
                            <td>@restorePoint.Notes</td>
                            <td class="text-end">
                                <a class="btn btn-outline-secondary btn-sm" asp-action="Download" asp-route-projectId="@Model.Project.ProjectID" asp-route-id="@restorePoint.ProjectRestorePointID">Download</a>
                                <button class="btn btn-outline-danger btn-sm" type="button" data-bs-toggle="modal" data-bs-target="#restorePointModal-@restorePoint.ProjectRestorePointID">Restore</button>
                                <button class="btn btn-outline-danger btn-sm" type="button" data-bs-toggle="modal" data-bs-target="#deleteRestorePointModal-@restorePoint.ProjectRestorePointID">Delete</button>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>

        @foreach (var restorePoint in Model.RestorePoints)
        {
            <div class="modal fade" id="restorePointModal-@restorePoint.ProjectRestorePointID" tabindex="-1" aria-labelledby="restorePointModalLabel-@restorePoint.ProjectRestorePointID" aria-hidden="true">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h2 class="modal-title fs-5" id="restorePointModalLabel-@restorePoint.ProjectRestorePointID">Restore Project</h2>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <p>This will replace the current contents of this project with the selected restore point.</p>
                            <p>A safety backup of the current project will be created automatically before the restore begins.</p>
                            <p class="mb-0">This operation may overwrite recent changes.</p>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                            <form asp-action="Restore" method="post" data-confirm-skip="true">
                                <input type="hidden" name="projectId" value="@Model.Project.ProjectID" />
                                <input type="hidden" name="id" value="@restorePoint.ProjectRestorePointID" />
                                <button class="btn btn-danger" type="submit">Restore Project</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

            <div class="modal fade" id="deleteRestorePointModal-@restorePoint.ProjectRestorePointID" tabindex="-1" aria-labelledby="deleteRestorePointModalLabel-@restorePoint.ProjectRestorePointID" aria-hidden="true">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h2 class="modal-title fs-5" id="deleteRestorePointModalLabel-@restorePoint.ProjectRestorePointID">Delete Restore Point</h2>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <p>This will permanently delete this restore point. You will not be able to restore from it after deletion.</p>
                            <p class="mb-0">You can download the restore point first if you want to keep a local backup copy.</p>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                            <form asp-action="Delete" method="post" data-confirm-skip="true">
                                <input type="hidden" name="projectId" value="@Model.Project.ProjectID" />
                                <input type="hidden" name="id" value="@restorePoint.ProjectRestorePointID" />
                                <button class="btn btn-danger" type="submit">Delete Restore Point</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        }
    }
</section>
