{
  "openapi": "3.1.0",
  "info": {
    "title": "Standard Bureau Agentic CRM API",
    "version": "1.0.0",
    "description": "Versioned API for leads, opportunities, pipelines, proposals, deliveries, and agent actions."
  },
  "servers": [
    {
      "url": "https://proposals.standardbureau.com"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "paths": {
    "/api/v1/dashboard": {
      "get": {
        "summary": "Get CRM dashboard snapshot",
        "responses": {
          "200": {
            "description": "Dashboard data"
          }
        }
      }
    },
    "/api/v1/contacts": {
      "get": {
        "summary": "List contacts",
        "parameters": [
          {
            "name": "search",
            "in": "query",
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Contacts"
          }
        }
      },
      "post": {
        "summary": "Create or update a lead/contact",
        "parameters": [
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ContactInput"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Contact created or updated"
          }
        }
      }
    },
    "/api/v1/pipelines": {
      "get": {
        "summary": "List pipelines with stages and entries",
        "responses": {
          "200": {
            "description": "Pipelines"
          }
        }
      },
      "post": {
        "summary": "Create or update a pipeline",
        "parameters": [
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/PipelineInput"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Pipeline created or updated"
          }
        }
      }
    },
    "/api/v1/pipelines/{pipeline_id}/stages": {
      "post": {
        "summary": "Create a stage in a pipeline",
        "parameters": [
          {
            "$ref": "#/components/parameters/PipelineId"
          },
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/StageInput"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Stage created"
          }
        }
      }
    },
    "/api/v1/pipelines/{pipeline_id}/entries": {
      "post": {
        "summary": "Attach a lead or opportunity to a pipeline",
        "parameters": [
          {
            "$ref": "#/components/parameters/PipelineId"
          },
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/PipelineEntryInput"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Entry created"
          }
        }
      }
    },
    "/api/v1/opportunities": {
      "get": {
        "summary": "List opportunities",
        "responses": {
          "200": {
            "description": "Opportunities"
          }
        }
      },
      "post": {
        "summary": "Create an opportunity",
        "parameters": [
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/OpportunityInput"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Opportunity created"
          }
        }
      }
    },
    "/api/v1/opportunities/{opportunity_id}/contacts": {
      "post": {
        "summary": "Attach a contact to an opportunity",
        "parameters": [
          {
            "$ref": "#/components/parameters/OpportunityId"
          },
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "responses": {
          "201": {
            "description": "Contact attached"
          }
        }
      }
    },
    "/api/v1/opportunities/{opportunity_id}/pipeline-entries": {
      "post": {
        "summary": "Add an opportunity to a pipeline",
        "parameters": [
          {
            "$ref": "#/components/parameters/OpportunityId"
          },
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "responses": {
          "201": {
            "description": "Pipeline entry created"
          }
        }
      }
    },
    "/api/v1/pipeline-entries/{entry_id}/move": {
      "post": {
        "summary": "Move a pipeline entry to another stage",
        "parameters": [
          {
            "$ref": "#/components/parameters/EntryId"
          },
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "stage_id": {
                    "type": "string"
                  },
                  "stage_name": {
                    "type": "string"
                  },
                  "notes": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Entry moved"
          }
        }
      }
    },
    "/api/v1/proposals": {
      "get": {
        "summary": "List proposals",
        "responses": {
          "200": {
            "description": "Proposals"
          }
        }
      },
      "post": {
        "summary": "Create a proposal",
        "parameters": [
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ProposalInput"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Proposal created"
          }
        }
      }
    },
    "/api/v1/proposals/{token}/attach": {
      "post": {
        "summary": "Attach a proposal to a lead or opportunity",
        "parameters": [
          {
            "$ref": "#/components/parameters/ProposalToken"
          },
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "responses": {
          "201": {
            "description": "Proposal attached"
          }
        }
      }
    },
    "/api/v1/proposals/{token}/deliver": {
      "post": {
        "summary": "Record delivery and return tracked proposal link",
        "parameters": [
          {
            "$ref": "#/components/parameters/ProposalToken"
          },
          {
            "$ref": "#/components/parameters/IdempotencyKey"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ProposalDeliveryInput"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Delivery created"
          }
        }
      }
    },
    "/api/v1/activity": {
      "get": {
        "summary": "List recent activity",
        "responses": {
          "200": {
            "description": "Activity"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer"
      }
    },
    "parameters": {
      "IdempotencyKey": {
        "name": "Idempotency-Key",
        "in": "header",
        "schema": {
          "type": "string"
        }
      },
      "PipelineId": {
        "name": "pipeline_id",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string"
        }
      },
      "OpportunityId": {
        "name": "opportunity_id",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string"
        }
      },
      "EntryId": {
        "name": "entry_id",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string"
        }
      },
      "ProposalToken": {
        "name": "token",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string"
        }
      }
    },
    "schemas": {
      "ContactInput": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "external_id": {
            "type": "string"
          },
          "full_name": {
            "type": "string"
          },
          "email": {
            "type": "string"
          },
          "company": {
            "type": "string"
          },
          "title": {
            "type": "string"
          },
          "phone": {
            "type": "string"
          },
          "source": {
            "type": "string"
          },
          "tags": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "notes": {
            "type": "string"
          }
        }
      },
      "PipelineInput": {
        "type": "object",
        "required": [
          "name"
        ],
        "properties": {
          "name": {
            "type": "string"
          },
          "description": {
            "type": "string"
          },
          "stages": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/StageInput"
            }
          }
        }
      },
      "StageInput": {
        "type": "object",
        "required": [
          "name"
        ],
        "properties": {
          "name": {
            "type": "string"
          },
          "position": {
            "type": "integer"
          },
          "probability": {
            "type": "integer"
          },
          "is_closed": {
            "type": "boolean"
          },
          "outcome": {
            "type": "string"
          }
        }
      },
      "PipelineEntryInput": {
        "type": "object",
        "properties": {
          "contact_id": {
            "type": "string"
          },
          "opportunity_id": {
            "type": "string"
          },
          "stage_id": {
            "type": "string"
          },
          "stage_name": {
            "type": "string"
          },
          "notes": {
            "type": "string"
          }
        }
      },
      "OpportunityInput": {
        "type": "object",
        "required": [
          "name"
        ],
        "properties": {
          "name": {
            "type": "string"
          },
          "contact_id": {
            "type": "string"
          },
          "contact": {
            "$ref": "#/components/schemas/ContactInput"
          },
          "pipeline_id": {
            "type": "string"
          },
          "pipeline_name": {
            "type": "string"
          },
          "stage_id": {
            "type": "string"
          },
          "stage_name": {
            "type": "string"
          },
          "value_cents": {
            "type": "integer"
          },
          "notes": {
            "type": "string"
          }
        }
      },
      "ProposalInput": {
        "type": "object",
        "required": [
          "title"
        ],
        "properties": {
          "title": {
            "type": "string"
          },
          "contact_id": {
            "type": "string"
          },
          "opportunity_id": {
            "type": "string"
          },
          "markdown": {
            "type": "string"
          },
          "html": {
            "type": "string"
          },
          "status": {
            "type": "string"
          },
          "access_code": {
            "type": "string"
          }
        }
      },
      "ProposalDeliveryInput": {
        "type": "object",
        "properties": {
          "contact_id": {
            "type": "string"
          },
          "opportunity_id": {
            "type": "string"
          },
          "recipient_email": {
            "type": "string"
          },
          "recipient_name": {
            "type": "string"
          },
          "delivery_method": {
            "type": "string",
            "default": "link"
          }
        }
      }
    }
  }
}
