{
  "openapi": "3.1.0",
  "info": {
    "title": "CapitalXTend.Pro CRM Partner API",
    "version": "1.0.0",
    "description": "Server-to-server API for approved advertisers, affiliates and partners to submit leads, query partner-scoped lead status and exchange shared comments. API keys must never be embedded in browser or mobile client code."
  },
  "servers": [
    {
      "url": "https://capitalxtend.pro/api/crm/v1",
      "description": "Production"
    }
  ],
  "security": [
    {
      "partnerBearer": []
    }
  ],
  "tags": [
    { "name": "Connection", "description": "Authentication and connection checks" },
    { "name": "Leads", "description": "Partner-scoped lead submission and lookup" },
    { "name": "Statuses", "description": "Privacy-minimized operational CRM lead statuses" },
    { "name": "Comments", "description": "Shared CRM/partner comments" }
  ],
  "paths": {
    "/health": {
      "get": {
        "tags": ["Connection"],
        "summary": "Validate the API key and connection",
        "operationId": "getPartnerApiHealth",
        "x-required-api-scope": "health:read",
        "parameters": [
          { "$ref": "#/components/parameters/XRequestId" }
        ],
        "responses": {
          "200": {
            "description": "The API key and partner tenant are active.",
            "headers": { "X-Request-Id": { "$ref": "#/components/headers/XRequestId" } },
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HealthResponse" } } }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    },
    "/leads": {
      "post": {
        "tags": ["Leads"],
        "summary": "Create one lead",
        "description": "Creates one unassigned lead in NEW operational status. The status is assigned by CRM and must not be supplied in the request. The supplied externalId is unique within the API key's partner tenant. This operation does not create an exchange account.",
        "operationId": "createPartnerLead",
        "x-required-api-scope": "leads:write",
        "parameters": [
          { "$ref": "#/components/parameters/IdempotencyKey" },
          { "$ref": "#/components/parameters/XRequestId" }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/PartnerLeadCreate" },
              "examples": {
                "affiliateLead": {
                  "summary": "Affiliate lead",
                  "value": {
                    "externalId": "AFF:LEAD-10482",
                    "firstName": "Ayse",
                    "lastName": "Yilmaz",
                    "email": "ayse@example.com",
                    "phone": "+905551112233",
                    "age": 31,
                    "source": "affiliate",
                    "campaign": "TR-Q3-Search",
                    "utmSource": "google",
                    "utmMedium": "cpc",
                    "utmCampaign": "yatirim-2026",
                    "landingPage": "https://partner.example/landing",
                    "preferredLanguage": "tr-TR",
                    "timezone": "Europe/Istanbul",
                    "tags": ["TR", "search"],
                    "marketingConsent": true,
                    "consentAt": "2026-09-07T10:15:00+03:00",
                    "consentSource": "lead-form-v3"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Lead created.",
            "headers": {
              "X-Request-Id": { "$ref": "#/components/headers/XRequestId" }
            },
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CreateLeadResponse" } } }
          },
          "200": {
            "description": "The same idempotency key and request body were replayed. No new lead was created; idempotent and duplicate.detected are true and duplicate.reasons contains IDEMPOTENCY_REPLAY.",
            "headers": {
              "X-Request-Id": { "$ref": "#/components/headers/XRequestId" },
              "X-Idempotent-Replay": { "$ref": "#/components/headers/XIdempotentReplay" }
            },
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CreateLeadResponse" } } }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "409": { "$ref": "#/components/responses/Conflict" },
          "413": { "$ref": "#/components/responses/PayloadTooLarge" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "503": { "$ref": "#/components/responses/ServiceUnavailable" },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      },
      "get": {
        "tags": ["Leads"],
        "summary": "Get one lead by query parameter",
        "description": "Compatibility alias for GET /leads/{externalId}. The path form is preferred.",
        "operationId": "getPartnerLeadByQuery",
        "x-required-api-scope": "leads:read",
        "parameters": [
          {
            "name": "externalId",
            "in": "query",
            "required": true,
            "description": "Case-sensitive, source-namespaced opaque lead identifier.",
            "schema": { "$ref": "#/components/schemas/ExternalId" }
          },
          { "$ref": "#/components/parameters/XRequestId" }
        ],
        "responses": {
          "200": {
            "description": "Partner-scoped lead status and timestamps only.",
            "headers": { "X-Request-Id": { "$ref": "#/components/headers/XRequestId" } },
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/GetLeadResponse" } } }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    },
    "/leads/{externalId}": {
      "get": {
        "tags": ["Leads"],
        "summary": "Get one lead",
        "description": "Returns an operational view without name, email, phone, exchange UID, KYC documents or balances.",
        "operationId": "getPartnerLead",
        "x-required-api-scope": "leads:read",
        "parameters": [
          { "$ref": "#/components/parameters/ExternalIdPath" },
          { "$ref": "#/components/parameters/XRequestId" }
        ],
        "responses": {
          "200": {
            "description": "Partner-scoped lead status and timestamps only.",
            "headers": { "X-Request-Id": { "$ref": "#/components/headers/XRequestId" } },
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/GetLeadResponse" } } }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    },
    "/lead-statuses/{externalId}": {
      "get": {
        "tags": ["Statuses"],
        "summary": "Get one operational CRM lead status",
        "description": "Returns the current operational CRM lead status and timestamps. All 12 operational statuses, including DEPOSITOR, are visible for a lead owned by the authenticated partner tenant. It does not return lifecycle/KYC status, personal data, KYC documents, assigned staff or balances.",
        "operationId": "getAdvertiserLeadStatus",
        "x-required-api-scope": "lead-statuses:read",
        "parameters": [
          { "$ref": "#/components/parameters/ExternalIdPath" },
          { "$ref": "#/components/parameters/XRequestId" }
        ],
        "responses": {
          "200": {
            "description": "Privacy-minimized operational status view.",
            "headers": { "X-Request-Id": { "$ref": "#/components/headers/XRequestId" } },
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/SingleStatusResponse" } } }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    },
    "/lead-statuses": {
      "get": {
        "tags": ["Statuses"],
        "summary": "List operational CRM lead statuses by date with pagination",
        "description": "Use startDate and endDate together to list partner-owned leads created within an inclusive UTC date range. Results are ordered newest first and support page/limit pagination up to 500 rows per page. For backward compatibility, externalId may instead be repeated up to 100 times. All 12 operational statuses, including DEPOSITOR, are visible only within the authenticated partner tenant.",
        "operationId": "getAdvertiserLeadStatuses",
        "x-required-api-scope": "lead-statuses:read",
        "parameters": [
          {
            "name": "externalId",
            "in": "query",
            "required": false,
            "description": "Optional compatibility filter: one to 100 case-sensitive external IDs. When omitted, startDate and endDate are required.",
            "style": "form",
            "explode": true,
            "schema": {
              "type": "array",
              "minItems": 1,
              "maxItems": 100,
              "items": { "$ref": "#/components/schemas/ExternalId" }
            }
          },
          {
            "name": "startDate",
            "in": "query",
            "required": false,
            "description": "Inclusive UTC lead creation date in YYYY-MM-DD format. Required with endDate when externalId is omitted.",
            "schema": { "type": "string", "format": "date", "example": "2026-08-10" }
          },
          {
            "name": "endDate",
            "in": "query",
            "required": false,
            "description": "Inclusive UTC lead creation date in YYYY-MM-DD format. Required with startDate when externalId is omitted.",
            "schema": { "type": "string", "format": "date", "example": "2026-09-10" }
          },
          {
            "name": "page",
            "in": "query",
            "required": false,
            "description": "One-based page number for date-range listings.",
            "schema": { "type": "integer", "minimum": 1, "maximum": 10000, "default": 1 }
          },
          {
            "name": "limit",
            "in": "query",
            "required": false,
            "description": "Rows per page for date-range listings.",
            "schema": { "type": "integer", "minimum": 1, "maximum": 500, "default": 50 }
          },
          { "$ref": "#/components/parameters/XRequestId" }
        ],
        "responses": {
          "200": {
            "description": "A tenant-scoped page of statuses plus pagination metadata. Date-only boundaries are inclusive and evaluated in UTC.",
            "headers": { "X-Request-Id": { "$ref": "#/components/headers/XRequestId" } },
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/BatchStatusResponse" } } }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    },
    "/leads/{externalId}/comments": {
      "get": {
        "tags": ["Comments"],
        "summary": "List shared comments",
        "description": "Returns only SHARED comments, ordered newest first. Internal CRM comments are never exposed.",
        "operationId": "listPartnerLeadComments",
        "x-required-api-scope": "comments:read",
        "parameters": [
          { "$ref": "#/components/parameters/ExternalIdPath" },
          { "$ref": "#/components/parameters/Limit" },
          { "$ref": "#/components/parameters/Cursor" },
          { "$ref": "#/components/parameters/XRequestId" }
        ],
        "responses": {
          "200": {
            "description": "A cursor-paginated page of shared comments.",
            "headers": { "X-Request-Id": { "$ref": "#/components/headers/XRequestId" } },
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CommentsResponse" } } }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      },
      "post": {
        "tags": ["Comments"],
        "summary": "Add one shared comment",
        "description": "Creates an append-only SHARED comment. Comment update and delete operations are not available.",
        "operationId": "createPartnerLeadComment",
        "x-required-api-scope": "comments:write",
        "parameters": [
          { "$ref": "#/components/parameters/ExternalIdPath" },
          { "$ref": "#/components/parameters/IdempotencyKey" },
          { "$ref": "#/components/parameters/XRequestId" }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CommentCreate" },
              "example": { "body": "Musteri yeni gorusme saati paylasti." }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Comment created, or a stored successful response replayed.",
            "headers": {
              "X-Request-Id": { "$ref": "#/components/headers/XRequestId" },
              "X-Idempotent-Replay": { "$ref": "#/components/headers/XIdempotentReplay" }
            },
            "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CreateCommentResponse" } } }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "409": { "$ref": "#/components/responses/Conflict" },
          "413": { "$ref": "#/components/responses/PayloadTooLarge" },
          "415": { "$ref": "#/components/responses/UnsupportedMediaType" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "500": { "$ref": "#/components/responses/ServerError" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "partnerBearer": {
        "type": "http",
        "scheme": "bearer",
        "description": "Opaque API key issued once by a CapitalXTend.Pro CRM administrator. This is not a JWT."
      }
    },
    "parameters": {
      "ExternalIdPath": {
        "name": "externalId",
        "in": "path",
        "required": true,
        "description": "Case-sensitive, source-namespaced opaque lead identifier. URL-encode this value in the path.",
        "schema": { "$ref": "#/components/schemas/ExternalId" }
      },
      "IdempotencyKey": {
        "name": "Idempotency-Key",
        "in": "header",
        "required": true,
        "description": "Globally unique per API key and logical mutation. Keep the same value and body when retrying. Successful receipts are retained for 24 hours.",
        "schema": {
          "type": "string",
          "minLength": 8,
          "maxLength": 128,
          "pattern": "^[A-Za-z0-9._:-]{8,128}$",
          "example": "lead-aff-10482-v1"
        }
      },
      "XRequestId": {
        "name": "X-Request-Id",
        "in": "header",
        "required": false,
        "description": "Optional caller correlation ID. Invalid values are replaced by a server-generated ID.",
        "schema": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128,
          "pattern": "^[A-Za-z0-9._:-]{1,128}$"
        }
      },
      "Limit": {
        "name": "limit",
        "in": "query",
        "required": false,
        "description": "Page size for shared comments.",
        "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 50 }
      },
      "Cursor": {
        "name": "cursor",
        "in": "query",
        "required": false,
        "description": "Opaque cursor returned as nextCursor. Pass it back unchanged.",
        "schema": { "type": "string" }
      }
    },
    "headers": {
      "XRequestId": {
        "description": "Correlation ID assigned to the request.",
        "schema": { "type": "string" }
      },
      "XIdempotentReplay": {
        "description": "Present with value 1 only when a stored successful response was replayed.",
        "schema": { "type": "string", "const": "1" }
      }
    },
    "responses": {
      "BadRequest": {
        "description": "Invalid input, JSON, cursor or header.",
        "headers": { "X-Request-Id": { "$ref": "#/components/headers/XRequestId" } },
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiError" } } }
      },
      "Unauthorized": {
        "description": "API key is missing, invalid, inactive, revoked or expired.",
        "headers": { "X-Request-Id": { "$ref": "#/components/headers/XRequestId" } },
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiError" } } }
      },
      "Forbidden": {
        "description": "Partner tenant is inactive or the key lacks the required scope.",
        "headers": { "X-Request-Id": { "$ref": "#/components/headers/XRequestId" } },
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiError" } } }
      },
      "NotFound": {
        "description": "Lead or lead status not found in this partner tenant.",
        "headers": { "X-Request-Id": { "$ref": "#/components/headers/XRequestId" } },
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiError" } } }
      },
      "Conflict": {
        "description": "Duplicate resource, idempotency conflict or incompatible lead settings.",
        "headers": { "X-Request-Id": { "$ref": "#/components/headers/XRequestId" } },
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiError" } } }
      },
      "PayloadTooLarge": {
        "description": "Request body exceeds the endpoint limit.",
        "headers": { "X-Request-Id": { "$ref": "#/components/headers/XRequestId" } },
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiError" } } }
      },
      "UnsupportedMediaType": {
        "description": "Content-Type must be application/json.",
        "headers": { "X-Request-Id": { "$ref": "#/components/headers/XRequestId" } },
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiError" } } }
      },
      "RateLimited": {
        "description": "Per-IP or per-key minute limit exceeded. Retry with exponential backoff and jitter.",
        "headers": { "X-Request-Id": { "$ref": "#/components/headers/XRequestId" } },
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiError" } } }
      },
      "ServiceUnavailable": {
        "description": "Transient database or transaction failure. Wait for Retry-After, then retry mutations with the same body and Idempotency-Key.",
        "headers": {
          "X-Request-Id": { "$ref": "#/components/headers/XRequestId" },
          "Retry-After": { "description": "Minimum retry delay in seconds.", "schema": { "type": "integer", "minimum": 1 } }
        },
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiError" } } }
      },
      "ServerError": {
        "description": "Unexpected server failure. Retry mutations with the same idempotency key and body.",
        "headers": { "X-Request-Id": { "$ref": "#/components/headers/XRequestId" } },
        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ApiError" } } }
      }
    },
    "schemas": {
      "ExternalId": {
        "type": "string",
        "minLength": 3,
        "maxLength": 128,
        "pattern": "^[A-Za-z][A-Za-z0-9]{1,31}[-_:][A-Za-z0-9](?:[A-Za-z0-9._-]*[A-Za-z0-9])?$",
        "description": "Source-namespaced opaque identifier. It must not be an email address, phone number, government ID, URL or use a reserved PII namespace. It is case-sensitive.",
        "examples": ["AFF:LEAD-10482", "META-123456789"]
      },
      "LeadStatus": {
        "type": "string",
        "description": "Current operational CRM status. Lifecycle and KYC workflow states are not exposed through the partner API.",
        "enum": ["NEW", "RECOVER", "CALLBACK", "IN_CALL", "NO_ANSWER", "NEVER_ANSWERS", "NOT_INTERESTED", "INVALID", "WRONG_NUMBER", "FAKE_INFO", "DEPOSITOR", "ACTIVE_TRADER"]
      },
      "LeadPriority": {
        "type": "string",
        "enum": ["LOW", "NORMAL", "HIGH", "URGENT"],
        "default": "NORMAL"
      },
      "PartnerLeadCreate": {
        "type": "object",
        "required": ["externalId"],
        "additionalProperties": false,
        "anyOf": [
          { "required": ["firstName"] },
          { "required": ["fullName"] },
          { "required": ["email"] },
          { "required": ["phone"] }
        ],
        "properties": {
          "externalId": { "$ref": "#/components/schemas/ExternalId" },
          "firstName": { "type": ["string", "null"], "maxLength": 100 },
          "lastName": { "type": ["string", "null"], "maxLength": 100 },
          "fullName": { "type": ["string", "null"], "maxLength": 200 },
          "email": { "type": ["string", "null"], "format": "email", "maxLength": 254 },
          "phone": { "type": ["string", "null"], "maxLength": 40, "description": "Must normalize to 7-18 digits. International format is recommended." },
          "age": { "type": ["integer", "null"], "minimum": 18, "maximum": 120 },
          "source": { "type": ["string", "null"], "maxLength": 100, "description": "Defaults to PARTNER_API when omitted." },
          "campaign": { "type": ["string", "null"], "maxLength": 160 },
          "utmSource": { "type": ["string", "null"], "maxLength": 160 },
          "utmMedium": { "type": ["string", "null"], "maxLength": 160 },
          "utmCampaign": { "type": ["string", "null"], "maxLength": 160 },
          "utmTerm": { "type": ["string", "null"], "maxLength": 160 },
          "utmContent": { "type": ["string", "null"], "maxLength": 300 },
          "landingPage": { "type": ["string", "null"], "format": "uri", "maxLength": 1000, "description": "HTTP(S) only, without URL credentials. Query and fragment are discarded." },
          "referrer": { "type": ["string", "null"], "format": "uri", "maxLength": 1000, "description": "HTTP(S) only, without URL credentials. Query and fragment are discarded." },
          "priority": { "$ref": "#/components/schemas/LeadPriority" },
          "tags": { "type": "array", "maxItems": 30, "items": { "type": "string", "minLength": 1, "maxLength": 40 } },
          "marketingConsent": { "type": ["boolean", "null"] },
          "consentAt": { "type": ["string", "null"], "format": "date-time", "description": "ISO 8601 timestamp with an explicit UTC offset." },
          "consentSource": { "type": ["string", "null"], "maxLength": 160 },
          "doNotCall": { "type": "boolean", "default": false },
          "preferredLanguage": { "type": ["string", "null"], "pattern": "^[A-Za-z]{2,3}([-_][A-Za-z]{2})?$" },
          "timezone": { "type": ["string", "null"], "maxLength": 80 },
          "nextCallbackAt": { "type": ["string", "null"], "format": "date-time", "description": "Cannot be used together with doNotCall=true." }
        }
      },
      "PartnerLeadView": {
        "type": "object",
        "description": "Privacy-minimized view for the owning partner tenant. Comments are available only through the scoped comments endpoints.",
        "required": ["externalId", "status", "createdAt", "updatedAt"],
        "properties": {
          "externalId": { "$ref": "#/components/schemas/ExternalId" },
          "status": { "$ref": "#/components/schemas/LeadStatus" },
          "createdAt": { "type": "string", "format": "date-time" },
          "updatedAt": { "type": "string", "format": "date-time" }
        }
      },
      "DuplicateSummary": {
        "type": "object",
        "required": ["detected", "reasons"],
        "properties": {
          "detected": { "type": "boolean" },
          "reasons": { "type": "array", "items": { "type": "string", "enum": ["EMAIL", "PHONE", "IDEMPOTENCY_REPLAY"] } }
        }
      },
      "CreateLeadResponse": {
        "type": "object",
        "required": ["lead", "duplicate", "idempotent"],
        "properties": {
          "lead": { "$ref": "#/components/schemas/PartnerLeadView" },
          "duplicate": { "$ref": "#/components/schemas/DuplicateSummary" },
          "idempotent": { "type": "boolean", "description": "True when this response is a safe replay of a previously completed request; false for a newly created lead." }
        }
      },
      "GetLeadResponse": {
        "type": "object",
        "required": ["lead"],
        "properties": { "lead": { "$ref": "#/components/schemas/PartnerLeadView" } }
      },
      "AdvertiserStatus": {
        "type": "string",
        "description": "Current operational CRM lead status exposed to the owning partner tenant. DEPOSITOR is included; lifecycle and KYC workflow states are not exposed.",
        "enum": ["NEW", "RECOVER", "CALLBACK", "IN_CALL", "NO_ANSWER", "NEVER_ANSWERS", "NOT_INTERESTED", "INVALID", "WRONG_NUMBER", "FAKE_INFO", "DEPOSITOR", "ACTIVE_TRADER"]
      },
      "AdvertiserLeadStatus": {
        "type": "object",
        "required": ["externalId", "status", "statusChangedAt", "createdAt"],
        "properties": {
          "externalId": { "$ref": "#/components/schemas/ExternalId" },
          "status": { "$ref": "#/components/schemas/AdvertiserStatus" },
          "statusChangedAt": { "type": "string", "format": "date-time", "description": "Timestamp when the operational CRM lead status most recently changed." },
          "createdAt": { "type": "string", "format": "date-time" }
        }
      },
      "SingleStatusResponse": {
        "type": "object",
        "required": ["leadStatus", "requestId"],
        "properties": {
          "leadStatus": { "$ref": "#/components/schemas/AdvertiserLeadStatus" },
          "requestId": { "type": "string" }
        }
      },
      "BatchStatusResponse": {
        "type": "object",
        "required": ["items", "missingExternalIds", "pagination", "requestId"],
        "properties": {
          "items": { "type": "array", "items": { "$ref": "#/components/schemas/AdvertiserLeadStatus" } },
          "missingExternalIds": { "type": "array", "items": { "$ref": "#/components/schemas/ExternalId" } },
          "pagination": {
            "type": "object",
            "required": ["page", "limit", "total", "totalPages", "hasNext", "hasPrevious"],
            "properties": {
              "page": { "type": "integer", "minimum": 1 },
              "limit": { "type": "integer", "minimum": 1, "maximum": 500 },
              "total": { "type": "integer", "minimum": 0 },
              "totalPages": { "type": "integer", "minimum": 0 },
              "hasNext": { "type": "boolean" },
              "hasPrevious": { "type": "boolean" }
            }
          },
          "requestId": { "type": "string" }
        }
      },
      "CommentCreate": {
        "type": "object",
        "required": ["body"],
        "additionalProperties": false,
        "properties": { "body": { "type": "string", "minLength": 1, "maxLength": 4000 } }
      },
      "CommentAuthor": {
        "type": "object",
        "required": ["type", "name"],
        "properties": {
          "type": { "type": "string", "enum": ["CRM_STAFF", "PARTNER", "AFFILIATE", "ADVERTISER"] },
          "name": { "type": "string" }
        }
      },
      "SharedComment": {
        "type": "object",
        "required": ["id", "body", "createdAt", "author"],
        "properties": {
          "id": { "type": "string" },
          "body": { "type": "string" },
          "createdAt": { "type": "string", "format": "date-time" },
          "author": { "$ref": "#/components/schemas/CommentAuthor" }
        }
      },
      "CommentsResponse": {
        "type": "object",
        "required": ["items", "nextCursor", "hasMore"],
        "properties": {
          "items": { "type": "array", "items": { "$ref": "#/components/schemas/SharedComment" } },
          "nextCursor": { "type": ["string", "null"] },
          "hasMore": { "type": "boolean" }
        }
      },
      "CreateCommentResponse": {
        "type": "object",
        "required": ["comment", "idempotent"],
        "properties": {
          "comment": { "$ref": "#/components/schemas/SharedComment" },
          "idempotent": { "type": "boolean", "description": "May remain false on replay; use the X-Idempotent-Replay response header." }
        }
      },
      "HealthResponse": {
        "type": "object",
        "required": ["ok", "tenant", "apiKey", "serverTime", "requestId"],
        "properties": {
          "ok": { "type": "boolean", "const": true },
          "tenant": {
            "type": "object",
            "required": ["id", "slug", "type"],
            "properties": {
              "id": { "type": "string", "description": "Stable Partner ID generated by CapitalXTend.Pro CRM and shown on the partner card." },
              "slug": { "type": "string" },
              "type": { "type": "string", "enum": ["PARTNER", "AFFILIATE", "ADVERTISER"] }
            }
          },
          "apiKey": {
            "type": "object",
            "required": ["prefix", "name", "scopes"],
            "properties": {
              "prefix": { "type": "string" },
              "name": { "type": "string" },
              "scopes": { "type": "array", "items": { "type": "string", "enum": ["lead-statuses:read", "leads:read", "leads:write", "comments:read", "comments:write", "health:read"] } }
            }
          },
          "serverTime": { "type": "string", "format": "date-time" },
          "requestId": { "type": "string" }
        }
      },
      "ValidationIssue": {
        "type": "object",
        "required": ["path", "code", "message"],
        "properties": {
          "path": { "type": "string" },
          "code": { "type": "string" },
          "message": { "type": "string" }
        }
      },
      "ApiError": {
        "type": "object",
        "required": ["error", "code", "requestId"],
        "properties": {
          "error": { "type": "string" },
          "code": { "type": "string" },
          "requestId": { "type": "string" },
          "issues": { "type": "array", "maxItems": 50, "items": { "$ref": "#/components/schemas/ValidationIssue" } }
        }
      }
    }
  }
}
