@page
@model Status.Pages.Admin.AlertRulesModel
@{
    ViewData["Title"] = "Alert Rules";
    ViewData["Page"] = "admin-alerts";
}

@if (!string.IsNullOrEmpty(Model.Message))
{
    <div class="alert alert-success">✓ @Model.Message</div>
}

<div class="section-header">
    <span class="section-title">Alert Rules</span>
    <button class="btn btn-primary" onclick="openModal('add-rule-modal')">+ Add Rule</button>
</div>

<div class="card">
    <table class="data-table">
        <thead><tr>
            <th>Monitor</th>
            <th>Condition</th>
            <th>Threshold</th>
            <th>Webhook</th>
            <th>Cooldown</th>
            <th>Last Fired</th>
            <th>Enabled</th>
            <th>Actions</th>
        </tr></thead>
        <tbody>
        @foreach (var r in Model.Rules)
        {
            <tr>
                <td style="color:var(--text-primary)">@(r.Monitor?.Name ?? "All Monitors")</td>
                <td class="mono" style="font-size:11px">@r.Condition</td>
                <td class="mono" style="font-size:11px">@(r.ThresholdValue?.ToString() ?? "-")</td>
                <td style="font-size:11px;color:var(--text-muted)">@(string.IsNullOrEmpty(r.WebhookUrl) ? "-" : "✓ configured")</td>
                <td class="mono" style="font-size:11px">@r.CooldownMinutes min</td>
                <td class="mono" style="font-size:11px">@(r.LastFiredAt?.ToString("yyyy-MM-dd HH:mm") ?? "never")</td>
                <td>
                    <form method="post" asp-page-handler="Toggle" style="display:inline">
                        <input type="hidden" name="id" value="@r.Id" />
                        <button type="submit" class="btn btn-outline btn-sm">
                            @(r.IsEnabled ? "🟢 On" : "⚫ Off")
                        </button>
                    </form>
                </td>
                <td>
                    <form method="post" asp-page-handler="Delete" style="display:inline" onsubmit="return confirm('Delete rule?')">
                        <input type="hidden" name="id" value="@r.Id" />
                        <button type="submit" class="btn btn-danger btn-sm">Delete</button>
                    </form>
                </td>
            </tr>
        }
        @if (!Model.Rules.Any())
        {
            <tr><td colspan="8" style="text-align:center;padding:32px;color:var(--text-muted)">No alert rules configured.</td></tr>
        }
        </tbody>
    </table>
</div>

<!-- Add Rule Modal -->
<div class="modal-overlay" id="add-rule-modal">
    <div class="modal">
        <div class="modal-header">
            <span class="modal-title">Add Alert Rule</span>
            <button class="modal-close" onclick="closeModal('add-rule-modal')">✕</button>
        </div>
        <form method="post" asp-page-handler="Save">
            <div class="modal-body">
                <div class="form-group">
                    <label class="form-label">Monitor (leave blank to apply to all)</label>
                    <select name="NewRule.MonitorId" class="form-control">
                        <option value="">All Monitors</option>
                        @foreach (var m in Model.Monitors)
                        { <option value="@m.Id">@m.Name</option> }
                    </select>
                </div>
                <div class="two-col">
                    <div class="form-group">
                        <label class="form-label">Condition *</label>
                        <select name="NewRule.Condition" class="form-control" onchange="updateThresholdVisibility(this.value)">
                            @foreach (var c in Enum.GetValues<AlertRuleCondition>())
                            { <option value="@c">@c</option> }
                        </select>
                    </div>
                    <div class="form-group" id="threshold-group">
                        <label class="form-label">Threshold Value</label>
                        <input type="number" name="NewRule.ThresholdValue" class="form-control" step="any" placeholder="e.g. 500" />
                    </div>
                </div>
                <div class="form-group">
                    <label class="form-label">Webhook URL (optional)</label>
                    <input type="url" name="NewRule.WebhookUrl" class="form-control" placeholder="https://hooks.slack.com/..." />
                </div>
                <div class="form-group">
                    <label class="form-label">Cooldown (minutes)</label>
                    <input type="number" name="NewRule.CooldownMinutes" class="form-control" value="10" min="0" />
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-outline" onclick="closeModal('add-rule-modal')">Cancel</button>
                <button type="submit" class="btn btn-primary">Save Rule</button>
            </div>
        </form>
    </div>
</div>

@section Scripts {
<script>
function updateThresholdVisibility(condition) {
    const needsThreshold = ['ResponseAboveMs', 'CertExpiresWithinDays'].includes(condition);
    document.getElementById('threshold-group').style.opacity = needsThreshold ? '1' : '0.4';
}
</script>
}
