@model FileAccessAgent.TestSite.Models.AuthDebugViewModel
@{
    ViewData["Title"] = "Redirect Login Test";
}

<div class="d-flex align-items-center justify-content-between mb-3">
    <h1 class="h3 mb-0">Redirect Login Test</h1>
    @if (!Model.IsAuthenticated)
    {
        <a class="btn btn-primary" href="/auth/login?returnUrl=/">Login via Member Center</a>
    }
    else
    {
        <form method="post" action="/auth/logout">
            @Html.AntiForgeryToken()
            <input type="hidden" name="returnUrl" value="/" />
            <button type="submit" class="btn btn-outline-danger">Logout</button>
        </form>
    }
</div>

<div class="alert @(Model.IsAuthenticated ? "alert-success" : "alert-warning")">
    <strong>Status:</strong> @(Model.IsAuthenticated ? "Authenticated" : "Anonymous")
    @if (Model.IsAuthenticated)
    {
        <span> | <strong>Name:</strong> @Model.Name</span>
        <span> | <strong>sub:</strong> @Model.Subject</span>
    }
</div>

@if (Model.IsAuthenticated)
{
    <div class="card mb-3">
        <div class="card-header">Access Token (raw)</div>
        <div class="card-body">
            <textarea class="form-control" rows="6" readonly>@Model.AccessToken</textarea>
        </div>
    </div>

    <div class="card mb-3">
        <div class="card-header">ID Token (raw)</div>
        <div class="card-body">
            <textarea class="form-control" rows="6" readonly>@Model.IdToken</textarea>
        </div>
    </div>

    <div class="card mb-3">
        <div class="card-header">Access Token Payload Claims</div>
        <div class="table-responsive">
            <table class="table table-sm table-striped mb-0">
                <thead>
                    <tr>
                        <th style="width:30%">Type</th>
                        <th>Value</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var claim in Model.AccessTokenPayload)
                    {
                        <tr>
                            <td><code>@claim.Key</code></td>
                            <td><code>@claim.Value</code></td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>
    </div>
}

<div class="card">
    <div class="card-header">User Claims</div>
    <div class="table-responsive">
        <table class="table table-sm table-striped mb-0">
            <thead>
                <tr>
                    <th style="width:30%">Type</th>
                    <th>Value</th>
                </tr>
            </thead>
            <tbody>
                @if (Model.Claims.Count == 0)
                {
                    <tr>
                        <td colspan="2" class="text-muted">No claims.</td>
                    </tr>
                }
                else
                {
                    @foreach (var claim in Model.Claims)
                    {
                        <tr>
                            <td><code>@claim.Key</code></td>
                            <td><code>@claim.Value</code></td>
                        </tr>
                    }
                }
            </tbody>
        </table>
    </div>
</div>

@if (Model.IsAuthenticated)
{
    var currentObjectKey = string.IsNullOrWhiteSpace(Model.CurrentObjectKey) ? "demo/uploads/sample.txt" : Model.CurrentObjectKey;
    <div class="card mt-3">
        <div class="card-header">Agent Functional Test</div>
        <div class="card-body">
            <form method="post" action="/test/upload" class="mb-3" enctype="multipart/form-data">
                @Html.AntiForgeryToken()
                <div class="mb-2">
                    <label class="form-label">File</label>
                    <input class="form-control" type="file" name="file" />
                </div>
                <div class="mb-2">
                    <label class="form-label">Object Key (optional)</label>
                    <input class="form-control" type="text" name="objectKey" value="@currentObjectKey" />
                </div>
                <button class="btn btn-primary" type="submit">Upload</button>
            </form>

            <div class="d-flex gap-2 mb-3">
                <form method="post" action="/test/metadata" class="d-flex gap-2">
                    @Html.AntiForgeryToken()
                    <input class="form-control" type="text" name="objectKey" value="@currentObjectKey" />
                    <button class="btn btn-outline-secondary" type="submit">Get Metadata</button>
                </form>

                <form method="post" action="/test/head" class="d-flex gap-2">
                    @Html.AntiForgeryToken()
                    <input class="form-control" type="text" name="objectKey" value="@currentObjectKey" />
                    <button class="btn btn-outline-secondary" type="submit">Head</button>
                </form>

                <form method="post" action="/test/download" class="d-flex gap-2">
                    @Html.AntiForgeryToken()
                    <input class="form-control" type="text" name="objectKey" value="@currentObjectKey" />
                    <button class="btn btn-outline-success" type="submit">Download</button>
                </form>

                <form method="post" action="/test/download-file" class="d-flex gap-2">
                    @Html.AntiForgeryToken()
                    <input class="form-control" type="text" name="objectKey" value="@currentObjectKey" />
                    <button class="btn btn-success" type="submit">Download File</button>
                </form>

                <form method="post" action="/test/delete" class="d-flex gap-2">
                    @Html.AntiForgeryToken()
                    <input class="form-control" type="text" name="objectKey" value="@currentObjectKey" />
                    <button class="btn btn-outline-danger" type="submit">Delete</button>
                </form>
            </div>

            <form method="post" action="/test/download-invalid-token" class="d-flex gap-2 mb-3">
                @Html.AntiForgeryToken()
                <input class="form-control" type="text" name="objectKey" value="@currentObjectKey" />
                <input class="form-control" type="text" name="token" value="invalid-token" />
                <button class="btn btn-outline-warning" type="submit">Download (Invalid Token)</button>
            </form>

            <form method="post" action="/test/health" class="mb-3">
                @Html.AntiForgeryToken()
                <button class="btn btn-outline-dark" type="submit">Health</button>
            </form>

            <div class="small text-muted mb-3">
                Covered APIs: <code>PUT /files/{objectKey}</code>,
                <code>GET /files/{objectKey}</code>,
                <code>HEAD /files/{objectKey}</code>,
                <code>GET /files/metadata/{objectKey}</code>,
                <code>DELETE /files/{objectKey}</code>,
                <code>GET /health</code>
            </div>

            @if (!string.IsNullOrWhiteSpace(Model.LastOperationName))
            {
                <div class="alert alert-info mb-0">
                    <div><strong>Last Operation:</strong> @Model.LastOperationName</div>
                    <div><strong>Status:</strong> @Model.LastOperationStatusCode</div>
                    <div><strong>Response:</strong></div>
                    <pre class="mb-0"><code>@Model.LastOperationResponseBody</code></pre>
                </div>
            }
        </div>
    </div>
}
