{
  "openapi": "3.1.0",
  "info": {
    "title": "AutoAdvia API",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://autoadvia-api.azurewebsites.net"
    }
  ],
  "security": [
    {
      "Entra": []
    }
  ],
  "components": {
    "securitySchemes": {
      "Entra": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT"
      }
    },
    "schemas": {
      "SubmissionId": {
        "type": "string",
        "pattern": "^[A-Za-z0-9_-][A-Za-z0-9_.-]{0,159}$"
      },
      "Submission": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "source",
          "submissionId",
          "submittedAt",
          "fields",
          "contact",
          "referral",
          "consent"
        ],
        "properties": {
          "source": {
            "enum": [
              "website",
              "sandy",
              "partners",
              "vendor"
            ]
          },
          "submissionId": {
            "$ref": "#/components/schemas/SubmissionId"
          },
          "submittedAt": {
            "type": "string",
            "format": "date-time",
            "pattern": "Z$"
          },
          "fields": {
            "type": "object",
            "minProperties": 1
          },
          "contact": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "firstName"
            ],
            "anyOf": [
              {
                "required": [
                  "phone"
                ]
              },
              {
                "required": [
                  "email"
                ]
              }
            ],
            "properties": {
              "firstName": {
                "type": "string",
                "minLength": 1,
                "maxLength": 254
              },
              "lastName": {
                "type": "string",
                "minLength": 1,
                "maxLength": 254
              },
              "phone": {
                "type": "string",
                "minLength": 1,
                "maxLength": 254
              },
              "email": {
                "type": "string",
                "minLength": 1,
                "maxLength": 254,
                "format": "email"
              },
              "zip": {
                "type": "string",
                "minLength": 1,
                "maxLength": 254
              },
              "city": {
                "type": "string",
                "minLength": 1,
                "maxLength": 254
              },
              "address": {
                "type": "string",
                "minLength": 1,
                "maxLength": 500
              }
            }
          },
          "referral": {},
          "consent": {
            "type": [
              "object",
              "null"
            ]
          }
        },
        "allOf": [
          {
            "if": {
              "properties": {
                "source": {
                  "const": "partners"
                }
              }
            },
            "then": {
              "properties": {
                "referral": {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "creditUnionId",
                    "creditUnionName",
                    "tenantId",
                    "employeeId",
                    "employeeName",
                    "employeeEmail"
                  ],
                  "properties": {
                    "creditUnionId": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 320
                    },
                    "creditUnionName": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 320
                    },
                    "tenantId": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 320
                    },
                    "employeeId": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 320
                    },
                    "employeeName": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 320
                    },
                    "employeeEmail": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 320,
                      "format": "email"
                    }
                  }
                }
              }
            },
            "else": {
              "properties": {
                "referral": {
                  "oneOf": [
                    {
                      "type": "null"
                    },
                    {
                      "type": "object",
                      "additionalProperties": false,
                      "anyOf": [
                        {
                          "required": [
                            "employeeName"
                          ]
                        },
                        {
                          "required": [
                            "creditUnionId"
                          ]
                        }
                      ],
                      "properties": {
                        "employeeName": {
                          "type": "string",
                          "minLength": 1,
                          "maxLength": 256
                        },
                        "creditUnionId": {
                          "type": "string",
                          "description": "ID from the AutoAdvia referral dropdown."
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        ]
      },
      "Receipt": {
        "type": "object",
        "required": [
          "receiptId",
          "status",
          "receivedAt"
        ],
        "properties": {
          "receiptId": {
            "type": "string",
            "pattern": "^[0-9a-f]{64}$"
          },
          "status": {
            "enum": [
              "pending",
              "delivered"
            ]
          },
          "receivedAt": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "Claim": {
        "type": "object",
        "properties": {
          "receiptId": {
            "type": "string",
            "pattern": "^[0-9a-f]{64}$"
          },
          "organization": {
            "type": "string"
          },
          "clientId": {
            "type": "string",
            "format": "uuid"
          },
          "leaseToken": {
            "type": "string",
            "format": "uuid"
          },
          "leaseExpiresAt": {
            "type": "string",
            "format": "date-time"
          },
          "submission": {
            "$ref": "#/components/schemas/Submission"
          }
        }
      }
    }
  },
  "paths": {
    "/v1/submissions": {
      "post": {
        "operationId": "submitLead",
        "parameters": [
          {
            "name": "X-AutoAdvia-Organization",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[a-z0-9][a-z0-9-]{0,62}$"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Submission"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Identical replay",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Receipt"
                }
              }
            }
          },
          "202": {
            "description": "Durably accepted",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Receipt"
                }
              }
            }
          },
          "400": {
            "description": "Invalid request"
          },
          "401": {
            "description": "Token missing, invalid or expired"
          },
          "403": {
            "description": "Identity or grant does not authorize the operation"
          },
          "409": {
            "description": "Submission ID reused with changed content"
          },
          "413": {
            "description": "Payload exceeds 64 KiB"
          },
          "429": {
            "description": "Client rate limit reached"
          },
          "503": {
            "description": "Storage or service unavailable; retry with backoff"
          }
        }
      }
    },
    "/v1/receipts/{receiptId}": {
      "get": {
        "operationId": "getOwnReceipt",
        "parameters": [
          {
            "name": "X-AutoAdvia-Organization",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[a-z0-9][a-z0-9-]{0,62}$"
            }
          },
          {
            "name": "receiptId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[0-9a-f]{64}$"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Caller-owned receipt",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Receipt"
                }
              }
            }
          },
          "400": {
            "description": "Invalid request"
          },
          "401": {
            "description": "Token missing, invalid or expired"
          },
          "403": {
            "description": "Identity or grant does not authorize the operation"
          },
          "404": {
            "description": "Receipt not available to this caller"
          },
          "413": {
            "description": "Payload exceeds 64 KiB"
          },
          "429": {
            "description": "Client rate limit reached"
          },
          "503": {
            "description": "Storage or service unavailable; retry with backoff"
          }
        }
      }
    },
    "/v1/crm/claims": {
      "post": {
        "operationId": "claimDeliveries",
        "parameters": [
          {
            "name": "X-AutoAdvia-Organization",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[a-z0-9][a-z0-9-]{0,62}$"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "limit": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 20,
                    "default": 5
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Deliveries leased for 120 seconds",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "items": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Claim"
                      }
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Invalid request"
          },
          "401": {
            "description": "Token missing, invalid or expired"
          },
          "403": {
            "description": "Identity or grant does not authorize the operation"
          },
          "413": {
            "description": "Payload exceeds 64 KiB"
          },
          "429": {
            "description": "Client rate limit reached"
          },
          "503": {
            "description": "Storage or service unavailable; retry with backoff"
          }
        }
      }
    },
    "/v1/crm/claims/{receiptId}/ack": {
      "post": {
        "operationId": "acknowledgeCommittedLead",
        "parameters": [
          {
            "name": "X-AutoAdvia-Organization",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[a-z0-9][a-z0-9-]{0,62}$"
            }
          },
          {
            "name": "receiptId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^[0-9a-f]{64}$"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "leaseToken",
                  "leadId"
                ],
                "properties": {
                  "leaseToken": {
                    "type": "string",
                    "format": "uuid"
                  },
                  "leadId": {
                    "type": "string",
                    "format": "uuid"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "204": {
            "description": "Acknowledged; identical replay is also successful"
          },
          "400": {
            "description": "Invalid request"
          },
          "401": {
            "description": "Token missing, invalid or expired"
          },
          "403": {
            "description": "Identity or grant does not authorize the operation"
          },
          "404": {
            "description": "Unknown receipt"
          },
          "409": {
            "description": "Stale lease or conflicting acknowledgment"
          },
          "413": {
            "description": "Payload exceeds 64 KiB"
          },
          "429": {
            "description": "Client rate limit reached"
          },
          "503": {
            "description": "Storage or service unavailable; retry with backoff"
          }
        }
      }
    }
  }
}
