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

export function AdminLogsPage() {
  return (
    <div>
      <h1 id="admin-logs">Admin — Audit Logs & Costs</h1>
      <p>
        Query the immutable audit trail and cost breakdown for AI requests. All data is stored in
        ClickHouse (append-only — no DELETE operations).
      </p>

      <h2 id="audit-logs">Audit Logs</h2>
      <ApiEndpoint method="GET" path="/v1/admin/logs" description="Query audit log entries with optional filters." />

      <ParamTable
        title="Query Parameters"
        params={[
          { name: "user_id", type: "string", required: false, description: "Filter by user UUID." },
          { name: "provider", type: "string", required: false, description: "Filter by provider: openai | anthropic | azure | mistral | ollama" },
          { name: "model", type: "string", required: false, description: "Filter by model used." },
          { name: "from", type: "string (ISO 8601)", required: false, description: "Start of time range, e.g. 2026-01-01T00:00:00Z" },
          { name: "to", type: "string (ISO 8601)", required: false, description: "End of time range." },
          { name: "has_pii", type: "boolean", required: false, description: "Filter entries where PII was detected." },
          { name: "limit", type: "integer", required: false, default: "50", description: "Max entries to return (max 1000)." },
          { name: "offset", type: "integer", required: false, default: "0", description: "Pagination offset." },
        ]}
      />

      <CodeBlock
        language="bash"
        code={`curl "http://localhost:8090/v1/admin/logs?from=2026-01-01T00:00:00Z&has_pii=true&limit=10" \\
  -H "Authorization: Bearer $TOKEN"

# Response:
{
  "data": [
    {
      "id": "log-uuid",
      "tenant_id": "tenant-uuid",
      "user_id": "user-uuid",
      "user_email": "alice@acme.com",
      "provider": "anthropic",
      "model_requested": "gpt-4o",
      "model_used": "claude-3-5-sonnet-20241022",
      "prompt_tokens": 128,
      "completion_tokens": 345,
      "total_tokens": 473,
      "cost_usd": 0.003412,
      "latency_ms": 1423,
      "pii_detected": true,
      "pii_entities": ["PERSON", "EMAIL_ADDRESS"],
      "policy_matched": "Legal → Anthropic",
      "status_code": 200,
      "timestamp": "2026-01-15T14:32:11Z"
    }
  ],
  "total": 142,
  "limit": 10,
  "offset": 0
}`}
      />

      <Callout type="info" title="Audit-of-the-audit">
        All accesses to audit logs are themselves logged. This satisfies the "audit-of-the-audit"
        requirement for sensitive compliance use cases.
      </Callout>

      <h2 id="costs">Cost Breakdown</h2>
      <ApiEndpoint method="GET" path="/v1/admin/costs" description="Aggregate cost breakdown by provider, model, or department." />

      <ParamTable
        title="Query Parameters"
        params={[
          { name: "group_by", type: "string", required: false, default: "provider", description: "Grouping dimension: provider | model | department | user" },
          { name: "from", type: "string (ISO 8601)", required: false, description: "Start of billing period." },
          { name: "to", type: "string (ISO 8601)", required: false, description: "End of billing period." },
        ]}
      />

      <CodeBlock
        language="bash"
        code={`curl "http://localhost:8090/v1/admin/costs?group_by=provider&from=2026-01-01T00:00:00Z" \\
  -H "Authorization: Bearer $TOKEN"

# Response:
{
  "data": [
    {
      "key": "openai",
      "request_count": 1423,
      "total_tokens": 2840000,
      "prompt_tokens": 1200000,
      "completion_tokens": 1640000,
      "total_cost_usd": 28.40
    },
    {
      "key": "anthropic",
      "request_count": 231,
      "total_tokens": 462000,
      "prompt_tokens": 230000,
      "completion_tokens": 232000,
      "total_cost_usd": 6.93
    }
  ],
  "period_from": "2026-01-01T00:00:00Z",
  "period_to": "2026-01-31T23:59:59Z"
}`}
      />

      <h2 id="rate-limits">Rate Limit Overrides</h2>
      <ApiEndpoint method="GET" path="/v1/admin/rate-limits" description="List per-tenant rate limit overrides." />
      <ApiEndpoint method="GET" path="/v1/admin/rate-limits/{tenant_id}" description="Get rate limit configuration for a specific tenant." />

      <CodeBlock
        language="bash"
        code={`# Get rate limit for a tenant
curl http://localhost:8090/v1/admin/rate-limits/tenant-uuid \\
  -H "Authorization: Bearer $TOKEN"

# Response:
{
  "tenant_id": "tenant-uuid",
  "rpm": 500,        # requests per minute
  "tpm": 50000,      # tokens per minute
  "daily_token_limit": 1000000
}`}
      />
    </div>
  );
}
