import { CodeBlock } from "../components/CodeBlock";
import { Callout } from "../components/Callout";
import { ApiEndpoint } from "../components/ApiEndpoint";

export function AdminFlagsPage() {
  return (
    <div>
      <h1 id="admin-flags">Admin — Feature Flags</h1>
      <p>
        Feature flags control behavior at runtime without redeployment. Flags are stored in
        PostgreSQL with an in-memory cache (updated every 30 seconds). If PostgreSQL is
        unavailable, the in-memory defaults are used.
      </p>

      <Callout type="info" title="Required role">
        Feature flag management requires the <code>admin</code> role.
      </Callout>

      <h2 id="list">List All Flags</h2>
      <ApiEndpoint method="GET" path="/v1/admin/flags" description="Return all feature flags and their current values." />
      <CodeBlock
        language="bash"
        code={`curl http://localhost:8090/v1/admin/flags \\
  -H "Authorization: Bearer $TOKEN"

# Response:
{
  "data": [
    {"name": "pii_detection",       "enabled": true,  "description": "Enable PII detection pipeline"},
    {"name": "pii_pseudonymization","enabled": true,  "description": "Store PII mappings in Redis"},
    {"name": "audit_logging",       "enabled": true,  "description": "Write to ClickHouse audit log"},
    {"name": "playground",          "enabled": true,  "description": "Public playground endpoint"},
    {"name": "streaming",           "enabled": true,  "description": "SSE streaming support"},
    {"name": "cost_tracking",       "enabled": true,  "description": "Track token costs per request"},
    {"name": "circuit_breaker",     "enabled": true,  "description": "Provider circuit breaker"}
  ]
}`}
      />

      <h2 id="get">Get a Flag</h2>
      <ApiEndpoint method="GET" path="/v1/admin/flags/{name}" description="Get the current value of a named flag." />

      <h2 id="set">Set a Flag</h2>
      <ApiEndpoint method="PUT" path="/v1/admin/flags/{name}" description="Enable or disable a feature flag." />
      <CodeBlock
        language="bash"
        code={`# Disable PII detection (e.g. for debugging)
curl -X PUT http://localhost:8090/v1/admin/flags/pii_detection \\
  -H "Authorization: Bearer $TOKEN" \\
  -H "Content-Type: application/json" \\
  -d '{"enabled": false}'`}
      />

      <h2 id="delete">Delete a Flag</h2>
      <ApiEndpoint method="DELETE" path="/v1/admin/flags/{name}" description="Delete a feature flag (it reverts to the compiled-in default)." />

      <h2 id="defaults">Default Flag Values</h2>
      <div className="overflow-x-auto my-4">
        <table className="w-full text-sm border rounded-lg overflow-hidden">
          <thead>
            <tr className="bg-muted/50 border-b">
              <th className="text-left px-4 py-2.5 font-semibold">Flag</th>
              <th className="text-left px-4 py-2.5 font-semibold">Default</th>
              <th className="text-left px-4 py-2.5 font-semibold">Effect when disabled</th>
            </tr>
          </thead>
          <tbody>
            {[
              { flag: "pii_detection", def: "true", effect: "Prompts forwarded without PII scanning" },
              { flag: "pii_pseudonymization", def: "true", effect: "PII detected but not stored in Redis" },
              { flag: "audit_logging", def: "true", effect: "Requests not written to ClickHouse" },
              { flag: "playground", def: "true", effect: "POST /playground/analyze returns 404" },
              { flag: "streaming", def: "true", effect: "SSE requests return 400" },
              { flag: "cost_tracking", def: "true", effect: "Token cost not computed or stored" },
              { flag: "circuit_breaker", def: "true", effect: "Failures not counted, no fallback" },
            ].map((row) => (
              <tr key={row.flag} className="border-b last:border-0">
                <td className="px-4 py-2.5 font-mono text-xs">{row.flag}</td>
                <td className="px-4 py-2.5 text-xs">
                  <span className="bg-green-100 dark:bg-green-900/40 text-green-700 dark:text-green-300 px-1.5 py-0.5 rounded font-medium">
                    {row.def}
                  </span>
                </td>
                <td className="px-4 py-2.5 text-muted-foreground text-xs">{row.effect}</td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>

      <Callout type="warning" title="Production safety">
        Disabling <code>audit_logging</code> or <code>pii_detection</code> in production may
        violate GDPR obligations and internal SLA requirements. Always coordinate with your DPO
        before making changes.
      </Callout>
    </div>
  );
}
