{
  "openapi": "3.0.3",
  "info": {
    "title": "Tour Expense Tracker API",
    "description": "REST API for the Tour Expense Tracker platform. Manage tours, leads, and access financial reports.",
    "version": "1.0.0",
    "contact": {
      "name": "TourTracker Support",
      "url": "https://tourtracker.app/contact"
    }
  },
  "servers": [
    {
      "url": "https://tourtracker.app",
      "description": "Production"
    }
  ],
  "security": [
    {
      "ApiKeyAuth": []
    }
  ],
  "paths": {
    "/api/leads": {
      "get": {
        "summary": "List leads",
        "description": "Returns all leads for the authenticated organization, optionally filtered by status.",
        "operationId": "getLeads",
        "tags": ["Leads"],
        "parameters": [
          {
            "name": "status",
            "in": "query",
            "required": false,
            "description": "Filter leads by status",
            "schema": {
              "type": "string",
              "enum": ["NEW", "CONTACTED", "QUALIFIED", "PROPOSAL", "WON", "LOST", "STALE"]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "List of leads",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Lead"
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      },
      "post": {
        "summary": "Create a lead",
        "description": "Creates a new lead in the authenticated organization.",
        "operationId": "createLead",
        "tags": ["Leads"],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateLeadRequest"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Lead created",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Lead"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/api/v1/leads": {
      "post": {
        "summary": "Ingest a lead (API key auth)",
        "description": "Creates a lead using an API key. Intended for external integrations (website forms, WhatsApp, etc.).",
        "operationId": "ingestLead",
        "tags": ["Leads"],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateLeadRequest"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Lead created",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Lead"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/api/reports/financial": {
      "get": {
        "summary": "Financial report",
        "description": "Returns a multi-currency financial report for tours in the given period.",
        "operationId": "getFinancialReport",
        "tags": ["Reports"],
        "parameters": [
          {
            "name": "currency",
            "in": "query",
            "required": false,
            "description": "Target display currency (ISO 4217). Defaults to USD.",
            "schema": {
              "type": "string",
              "default": "USD",
              "example": "EUR"
            }
          },
          {
            "name": "period",
            "in": "query",
            "required": false,
            "description": "Reporting period",
            "schema": {
              "type": "string",
              "enum": ["month", "quarter", "year", "all"],
              "default": "month"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Financial report data",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/FinancialReport"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "429": {
            "description": "Rate limit exceeded"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Key",
        "description": "API key issued from the organization settings page."
      },
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT",
        "description": "Session-based JWT token from NextAuth."
      }
    },
    "schemas": {
      "Lead": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "contact_name": { "type": "string" },
          "company_name": { "type": "string", "nullable": true },
          "email": { "type": "string", "format": "email", "nullable": true },
          "phone": { "type": "string", "nullable": true },
          "country": { "type": "string", "nullable": true },
          "destination": { "type": "string", "nullable": true },
          "tour_type": { "type": "string", "nullable": true },
          "estimated_pax": { "type": "integer", "nullable": true },
          "estimated_start": { "type": "string", "format": "date", "nullable": true },
          "estimated_end": { "type": "string", "format": "date", "nullable": true },
          "source": {
            "type": "string",
            "enum": ["WEBSITE", "EMAIL", "PHONE", "WHATSAPP", "REFERRAL", "PARTNER", "SOCIAL_MEDIA", "TRADE_SHOW", "OTHER"]
          },
          "status": {
            "type": "string",
            "enum": ["NEW", "CONTACTED", "QUALIFIED", "PROPOSAL", "WON", "LOST", "STALE"]
          },
          "priority": { "type": "integer" },
          "notes": { "type": "string", "nullable": true },
          "inquiry_date": { "type": "string", "format": "date-time" },
          "created_at": { "type": "string", "format": "date-time" }
        },
        "required": ["id", "contact_name", "source", "status"]
      },
      "CreateLeadRequest": {
        "type": "object",
        "properties": {
          "contact_name": { "type": "string", "maxLength": 255 },
          "company_name": { "type": "string", "maxLength": 255 },
          "email": { "type": "string", "format": "email", "maxLength": 255 },
          "phone": { "type": "string", "maxLength": 50 },
          "country": { "type": "string", "maxLength": 100 },
          "tour_type": { "type": "string", "maxLength": 100 },
          "destination": { "type": "string", "maxLength": 255 },
          "estimated_pax": { "type": "integer", "minimum": 1 },
          "estimated_start": { "type": "string", "format": "date" },
          "estimated_end": { "type": "string", "format": "date" },
          "source": {
            "type": "string",
            "enum": ["WEBSITE", "EMAIL", "PHONE", "WHATSAPP", "REFERRAL", "PARTNER", "SOCIAL_MEDIA", "TRADE_SHOW", "OTHER"],
            "default": "OTHER"
          },
          "priority": { "type": "integer", "minimum": 0, "maximum": 10, "default": 0 },
          "notes": { "type": "string", "maxLength": 5000 },
          "budget_range": { "type": "string", "maxLength": 100 }
        },
        "required": ["contact_name"]
      },
      "FinancialReport": {
        "type": "object",
        "properties": {
          "currency": { "type": "string", "example": "USD" },
          "period": { "type": "string", "example": "month" },
          "tours": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "id": { "type": "string", "format": "uuid" },
                "name": { "type": "string" },
                "status": { "type": "string", "enum": ["PLANNED", "ACTIVE", "CLOSED", "CANCELLED"] },
                "total_income": { "type": "number" },
                "total_expenses": { "type": "number" },
                "net_profit": { "type": "number" },
                "margin_pct": { "type": "number" }
              }
            }
          },
          "summary": {
            "type": "object",
            "properties": {
              "total_income": { "type": "number" },
              "total_expenses": { "type": "number" },
              "net_profit": { "type": "number" }
            }
          }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": { "type": "string" }
        },
        "required": ["error"]
      }
    },
    "responses": {
      "Unauthorized": {
        "description": "Authentication required",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      },
      "Forbidden": {
        "description": "Insufficient permissions",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      },
      "BadRequest": {
        "description": "Validation error",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" }
          }
        }
      }
    }
  }
}
