{
  "openapi": "3.1.0",
  "info": {
    "title": "SilverEdge Platform API",
    "description": "Tenant-scoped API for integrating partner CRMs, dialers, BI tools, and white-label agency portals against the SilverEdge platform.\n\nAuth: pass a per-tenant API key in the `X-API-Key` header. Issue keys from `/portal-admin/api-keys/`.\n\nAll endpoints are tenant-scoped via the key — you cannot read another tenant's data.",
    "version": "1.0.0",
    "contact": {
      "name": "SilverEdge Platform",
      "url": "https://seinsurance.com",
      "email": "platform@seinsurance.com"
    }
  },
  "servers": [
    { "url": "https://seinsurance.com", "description": "Production" }
  ],
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": { "type": "apiKey", "in": "header", "name": "X-API-Key" }
    },
    "schemas": {
      "Lead": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "type": { "type": "string", "enum": ["aca", "medicare", "unsure"] },
          "name": { "type": "string" },
          "email": { "type": "string", "format": "email" },
          "phone": { "type": "string" },
          "zip": { "type": "string" },
          "state": { "type": "string" },
          "intent": { "type": "string" },
          "source": { "type": "string" },
          "utm_source": { "type": "string" },
          "utm_medium": { "type": "string" },
          "utm_campaign": { "type": "string" },
          "status": { "type": "string" },
          "created_at": { "type": "string", "format": "date-time" },
          "assigned_to": { "type": "string" },
          "assignment_status": { "type": "string" }
        }
      },
      "LeadCreate": {
        "type": "object",
        "required": [],
        "properties": {
          "type": { "type": "string", "enum": ["aca", "medicare", "unsure"], "default": "unsure" },
          "name": { "type": "string", "maxLength": 120 },
          "email": { "type": "string", "format": "email" },
          "phone": { "type": "string" },
          "zip": { "type": "string", "maxLength": 5 },
          "state": { "type": "string" },
          "intent": { "type": "string" },
          "notes": { "type": "string" },
          "source": { "type": "string" },
          "utm_source": { "type": "string" },
          "utm_medium": { "type": "string" },
          "utm_campaign": { "type": "string" }
        },
        "description": "At least one of phone / email / zip required."
      },
      "Commission": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "agent_email": { "type": "string", "format": "email" },
          "period_start": { "type": "string", "format": "date" },
          "period_end": { "type": "string", "format": "date" },
          "carrier": { "type": "string" },
          "product": { "type": "string" },
          "policy_count": { "type": "integer" },
          "amount_paid": { "type": "number" },
          "bonus_amount": { "type": "number" },
          "ingested_at": { "type": "string", "format": "date-time" }
        }
      },
      "MetricsSnapshot": {
        "type": "object",
        "properties": {
          "ok": { "type": "boolean" },
          "window_days": { "type": "integer" },
          "leads": { "type": "integer" },
          "enrolled": { "type": "integer" },
          "conversion_rate": { "type": "number" },
          "revenue": { "type": "number" },
          "producers": { "type": "integer" }
        }
      },
      "Tenant": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "slug": { "type": "string" },
          "name": { "type": "string" },
          "primary_color": { "type": "string" },
          "accent_color": { "type": "string" },
          "navy_color": { "type": "string" },
          "logo_url": { "type": "string" },
          "support_phone": { "type": "string" },
          "support_email": { "type": "string" },
          "domain": { "type": "string" }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": { "type": "string" },
          "hint": { "type": "string" }
        }
      }
    }
  },
  "security": [{ "ApiKeyAuth": [] }],
  "paths": {
    "/api/tenant": {
      "get": {
        "summary": "Get branding for the current tenant",
        "description": "Returns the resolved tenant's branding (colors, logo, support contacts). Public — no API key required. Resolves by domain → falls back to silveredge.",
        "security": [],
        "responses": {
          "200": {
            "description": "OK",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Tenant" } } }
          }
        }
      }
    },
    "/api/public/leads": {
      "get": {
        "summary": "List leads",
        "description": "Tenant-scoped list of consumer leads. Most-recent first. Scope: `leads:read`",
        "parameters": [
          { "name": "limit", "in": "query", "schema": { "type": "integer", "default": 100, "maximum": 500 } },
          { "name": "status", "in": "query", "schema": { "type": "string", "enum": ["new", "contacted", "quoted", "enrolled", "no-contact", "lost"] } }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": { "application/json": { "schema": {
              "type": "object",
              "properties": {
                "ok": { "type": "boolean" },
                "leads": { "type": "array", "items": { "$ref": "#/components/schemas/Lead" } }
              }
            } } }
          },
          "401": { "description": "Missing or invalid X-API-Key", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } },
          "403": { "description": "Key lacks `leads:read` scope", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } }
        }
      },
      "post": {
        "summary": "Create a lead",
        "description": "Insert a new consumer lead into the tenant. Auto-routing fires asynchronously. Scope: `leads:write`",
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": { "$ref": "#/components/schemas/LeadCreate" } } }
        },
        "responses": {
          "200": {
            "description": "Created",
            "content": { "application/json": { "schema": {
              "type": "object",
              "properties": {
                "ok": { "type": "boolean" },
                "lead_id": { "type": "integer" }
              }
            } } }
          },
          "400": { "description": "Validation error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } }
        }
      }
    },
    "/api/public/commissions": {
      "get": {
        "summary": "List commission rows",
        "description": "Tenant-scoped commissions over a rolling window. Scope: `commissions:read`",
        "parameters": [
          { "name": "days", "in": "query", "schema": { "type": "integer", "default": 90, "maximum": 365 } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "default": 500, "maximum": 2000 } },
          { "name": "agent_email", "in": "query", "schema": { "type": "string", "format": "email" } }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": { "application/json": { "schema": {
              "type": "object",
              "properties": {
                "ok": { "type": "boolean" },
                "window_days": { "type": "integer" },
                "totals": { "type": "object" },
                "rows": { "type": "array", "items": { "$ref": "#/components/schemas/Commission" } }
              }
            } } }
          }
        }
      }
    },
    "/api/public/metrics": {
      "get": {
        "summary": "Tenant metrics snapshot",
        "description": "Headline numbers (leads, enrolled, conversion, revenue) over a rolling window. Mirrors what a BI tool would pull. Scope: `metrics:read`",
        "parameters": [
          { "name": "days", "in": "query", "schema": { "type": "integer", "default": 90, "maximum": 365 } }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/MetricsSnapshot" } } }
          }
        }
      }
    }
  }
}
