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

export function AdminPoliciesPage() {
  return (
    <div>
      <h1 id="admin-policies">Admin — Routing Policies</h1>
      <p>
        Routing policies (rules) define how AI requests are dispatched to providers. Rules are
        evaluated in ascending priority order; the first matching rule wins.
      </p>

      <Callout type="info" title="Required role">
        All <code>/v1/admin/policies</code> endpoints require the <code>admin</code> or{" "}
        <code>manager</code> role.
      </Callout>

      <h2 id="list">List Policies</h2>
      <ApiEndpoint method="GET" path="/v1/admin/policies" description="Return all routing rules for the current tenant, sorted by priority." />
      <CodeBlock
        language="bash"
        code={`curl http://localhost:8090/v1/admin/policies \\
  -H "Authorization: Bearer $TOKEN"

# Response:
{
  "data": [
    {
      "id": "550e8400-e29b-41d4-a716-446655440000",
      "name": "Legal → Anthropic",
      "priority": 10,
      "provider": "anthropic",
      "target_model": "claude-3-5-sonnet-20241022",
      "fallback_providers": ["openai"],
      "conditions": [
        {"field": "user.department", "operator": "eq", "value": "legal"}
      ],
      "enabled": true,
      "created_at": "2026-01-15T10:00:00Z"
    }
  ]
}`}
      />

      <h2 id="create">Create Policy</h2>
      <ApiEndpoint method="POST" path="/v1/admin/policies" description="Create a new routing rule." />
      <ParamTable
        params={[
          { name: "name", type: "string", required: true, description: "Human-readable rule name." },
          { name: "priority", type: "integer", required: true, description: "Evaluation order (lower = first). Must be unique per tenant." },
          { name: "provider", type: "string", required: true, description: "Target provider: openai | anthropic | azure | mistral | ollama" },
          { name: "target_model", type: "string", required: false, description: "Override the model requested by the client." },
          { name: "fallback_providers", type: "[]string", required: false, description: "Ordered list of fallback providers if the primary fails or circuit is open." },
          { name: "conditions", type: "[]Condition", required: false, description: "Array of conditions (all must match, AND-joined). Empty = catch-all." },
          { name: "enabled", type: "boolean", required: false, default: "true", description: "Whether the rule is active." },
        ]}
      />

      <h3 id="conditions">Condition Fields</h3>
      <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">Field</th>
              <th className="text-left px-4 py-2.5 font-semibold">Operators</th>
              <th className="text-left px-4 py-2.5 font-semibold">Value example</th>
            </tr>
          </thead>
          <tbody>
            {[
              { field: "user.role", ops: "eq, neq, in, nin", ex: '"admin"' },
              { field: "user.department", ops: "eq, neq, in, nin, contains", ex: '"legal"' },
              { field: "request.model", ops: "eq, neq, in, nin", ex: '"gpt-4o"' },
              { field: "request.sensitivity", ops: "eq, neq, gte, lte", ex: '"high"' },
              { field: "request.use_case", ops: "eq, neq, contains, matches", ex: '"code_review"' },
              { field: "request.token_estimate", ops: "gte, lte", ex: '"4000"' },
            ].map((row) => (
              <tr key={row.field} className="border-b last:border-0">
                <td className="px-4 py-2.5 font-mono text-xs">{row.field}</td>
                <td className="px-4 py-2.5 text-muted-foreground text-xs">{row.ops}</td>
                <td className="px-4 py-2.5 font-mono text-xs text-blue-600 dark:text-blue-400">{row.ex}</td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>

      <CodeBlock
        language="bash"
        code={`curl -X POST http://localhost:8090/v1/admin/policies \\
  -H "Authorization: Bearer $TOKEN" \\
  -H "Content-Type: application/json" \\
  -d '{
    "name": "Legal → Anthropic",
    "priority": 10,
    "provider": "anthropic",
    "target_model": "claude-3-5-sonnet-20241022",
    "fallback_providers": ["openai"],
    "conditions": [
      {"field": "user.department", "operator": "eq", "value": "legal"}
    ]
  }'`}
      />

      <h2 id="update">Update Policy</h2>
      <ApiEndpoint method="PUT" path="/v1/admin/policies/{id}" description="Update an existing routing rule (full replacement)." />

      <h2 id="delete">Delete Policy</h2>
      <ApiEndpoint method="DELETE" path="/v1/admin/policies/{id}" description="Permanently delete a routing rule." />

      <h2 id="seed">Seed from Template</h2>
      <ApiEndpoint method="POST" path="/v1/admin/policies/seed/{template}" description="Create a pre-built set of routing rules from a template." />
      <p>Available templates:</p>
      <ul>
        <li><code>default</code> — Single catch-all rule routing to OpenAI</li>
        <li><code>rbac</code> — Rules per RBAC role with model restrictions</li>
        <li><code>department</code> — Rules per department (legal, hr, engineering, finance)</li>
        <li><code>cost-optimized</code> — Routes to cheaper models for simple queries</li>
      </ul>
    </div>
  );
}
