{
  "openapi": "3.1.0",
  "info": {
    "title": "Lurity Exchange API",
    "description": "Programmatic access to DOOH inventory, availability, and pricing for DSP partners.",
    "version": "1.9.0",
    "contact": {
      "name": "Lurity API Support",
      "email": "api-support@lurity.com"
    }
  },
  "servers": [
    {
      "url": "https://tjquyckiekmgokyheclw.supabase.co/functions/v1/exchange-api/v1",
      "description": "Production"
    }
  ],
  "security": [
    { "ApiKeyAuth": [] }
  ],
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "x-api-key",
        "description": "HMAC SHA-256 hashed API key issued per demand source."
      }
    },
    "schemas": {
      "PaginationMeta": {
        "type": "object",
        "properties": {
          "page": { "type": "integer", "example": 1 },
          "page_size": { "type": "integer", "example": 200 },
          "total_count": { "type": "integer", "example": 1245 },
          "total_pages": { "type": "integer", "example": 7 },
          "has_more": { "type": "boolean", "example": true }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "properties": {
              "code": { "type": "string", "example": "UNAUTHORIZED" },
              "message": { "type": "string", "example": "Invalid or missing API key" },
              "retry_after": { "type": "integer", "nullable": true }
            }
          }
        }
      },
      "MediaUnit": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "name": { "type": "string", "example": "Mall Entrance Screen A" },
          "external_id": { "type": "string", "nullable": true },
          "status": { "type": "string", "enum": ["active", "inactive", "maintenance"] },
          "venue_id": { "type": "string", "format": "uuid", "nullable": true },
          "venue_name": { "type": "string", "nullable": true },
          "latitude": { "type": "number", "format": "double", "example": 48.1486 },
          "longitude": { "type": "number", "format": "double", "example": 17.1077 },
          "address_formatted": { "type": "string", "nullable": true },
          "address_city": { "type": "string", "nullable": true },
          "address_country_iso2": { "type": "string", "example": "SK" },
          "screen_type": { "type": "string", "nullable": true },
          "width_px": { "type": "integer", "nullable": true, "example": 1920 },
          "height_px": { "type": "integer", "nullable": true, "example": 1080 },
          "orientation": { "type": "string", "enum": ["landscape", "portrait"], "nullable": true },
          "loop_duration_seconds": { "type": "integer", "nullable": true },
          "slot_duration_seconds": { "type": "integer", "nullable": true },
          "environment": { "type": "string", "enum": ["indoor", "outdoor"], "nullable": true },
          "venue_category": { "type": "string", "nullable": true },
          "openooh_taxonomy_code": { "type": "string", "nullable": true, "example": "retail.mall" },
          "impressions_per_spot": { "type": "integer", "nullable": true },
          "ai_enrichment": { "type": "object", "nullable": true, "description": "Requires include_enrichment=true" },
          "created_at": { "type": "string", "format": "date-time" },
          "updated_at": { "type": "string", "format": "date-time" }
        }
      },
      "Venue": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "name": { "type": "string", "example": "Aupark Shopping Center" },
          "external_id": { "type": "string", "nullable": true },
          "address_formatted": { "type": "string" },
          "address_street": { "type": "string", "nullable": true },
          "address_city": { "type": "string" },
          "address_postal_code": { "type": "string", "nullable": true },
          "address_country_iso2": { "type": "string", "example": "SK" },
          "address_quality": { "type": "string", "enum": ["verified", "corrected"] },
          "latitude": { "type": "number", "format": "double" },
          "longitude": { "type": "number", "format": "double" },
          "venue_type": { "type": "string", "nullable": true },
          "venue_category": { "type": "string", "nullable": true },
          "environment": { "type": "string", "enum": ["indoor", "outdoor", "mixed"], "nullable": true },
          "media_unit_count": { "type": "integer" },
          "buyer_pitch": { "type": "string", "nullable": true, "description": "AI-generated venue description" },
          "approved_at": { "type": "string", "format": "date-time" },
          "created_at": { "type": "string", "format": "date-time" },
          "updated_at": { "type": "string", "format": "date-time" }
        }
      },
      "AvailabilitySlot": {
        "type": "object",
        "properties": {
          "media_unit_id": { "type": "string", "format": "uuid" },
          "media_unit_name": { "type": "string" },
          "venue_id": { "type": "string", "format": "uuid", "nullable": true },
          "date": { "type": "string", "format": "date" },
          "total_slots": { "type": "integer" },
          "booked_slots": { "type": "integer" },
          "available_slots": { "type": "integer" },
          "share_of_voice_available": { "type": "number" },
          "bookable": { "type": "boolean" }
        }
      },
      "Market": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "name": { "type": "string", "example": "Bratislava" },
          "type": { "type": "string", "example": "city" },
          "country_iso2": { "type": "string", "example": "SK" },
          "media_unit_count": { "type": "integer" },
          "venue_count": { "type": "integer" },
          "geometry": { "type": "object", "nullable": true }
        }
      },
      "RateCard": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "name": { "type": "string" },
          "scope_type": { "type": "string", "enum": ["global", "venue", "media_unit", "market"] },
          "scope_value": { "type": "string", "nullable": true },
          "currency": { "type": "string", "example": "EUR" },
          "rules": {
            "type": "object",
            "description": "Pricing rules (base CPM, time-of-day multipliers, etc.)"
          },
          "effective_from": { "type": "string", "format": "date" },
          "effective_to": { "type": "string", "format": "date", "nullable": true }
        }
      },
      "HealthResponse": {
        "type": "object",
        "properties": {
          "status": { "type": "string", "example": "ok" },
          "version": { "type": "string", "example": "1.9.0" },
          "timestamp": { "type": "string", "format": "date-time" }
        }
      }
    },
    "parameters": {
      "PageParam": {
        "name": "page",
        "in": "query",
        "schema": { "type": "integer", "default": 1, "minimum": 1 },
        "description": "Page number (1-indexed)"
      },
      "PageSizeParam": {
        "name": "page_size",
        "in": "query",
        "schema": { "type": "integer", "default": 200, "minimum": 1, "maximum": 500 },
        "description": "Number of records per page (max 500)"
      },
      "UpdatedSinceParam": {
        "name": "updated_since",
        "in": "query",
        "schema": { "type": "string", "format": "date-time" },
        "description": "ISO 8601 timestamp. Returns only records updated after this time."
      }
    }
  },
  "paths": {
    "/media-units": {
      "get": {
        "operationId": "listMediaUnits",
        "summary": "List media units",
        "description": "Returns all DSP-visible media units with specs, geo, pricing, and optional AI enrichment data. Only media units whose parent venue passes all visibility gates are included.",
        "tags": ["Inventory"],
        "parameters": [
          { "$ref": "#/components/parameters/PageParam" },
          { "$ref": "#/components/parameters/PageSizeParam" },
          { "$ref": "#/components/parameters/UpdatedSinceParam" },
          {
            "name": "include_enrichment",
            "in": "query",
            "schema": { "type": "boolean", "default": false },
            "description": "Include AI enrichment data (taxonomy, description, audience insights)"
          },
          {
            "name": "market_id",
            "in": "query",
            "schema": { "type": "string", "format": "uuid" },
            "description": "Filter by market ID"
          },
          {
            "name": "environment",
            "in": "query",
            "schema": { "type": "string", "enum": ["indoor", "outdoor"] },
            "description": "Filter by environment type"
          }
        ],
        "responses": {
          "200": {
            "description": "Paginated list of media units",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": { "type": "array", "items": { "$ref": "#/components/schemas/MediaUnit" } },
                    "meta": { "$ref": "#/components/schemas/PaginationMeta" }
                  }
                }
              }
            }
          },
          "401": { "description": "Unauthorized", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "429": { "description": "Rate limited", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } }
        }
      }
    },
    "/media-units/{id}": {
      "get": {
        "operationId": "getMediaUnit",
        "summary": "Get media unit by ID",
        "description": "Returns a single media unit with full detail. Returns 404 if the unit does not exist or fails visibility gates.",
        "tags": ["Inventory"],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "format": "uuid" }
          },
          {
            "name": "include_enrichment",
            "in": "query",
            "schema": { "type": "boolean", "default": false }
          }
        ],
        "responses": {
          "200": { "description": "Media unit detail", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/MediaUnit" } } } },
          "404": { "description": "Not found or not visible", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } }
        }
      }
    },
    "/venues": {
      "get": {
        "operationId": "listVenues",
        "summary": "List venues",
        "description": "Returns all approved venues with address, geo coordinates, and associated media unit counts.",
        "tags": ["Inventory"],
        "parameters": [
          { "$ref": "#/components/parameters/PageParam" },
          { "$ref": "#/components/parameters/PageSizeParam" },
          { "$ref": "#/components/parameters/UpdatedSinceParam" },
          {
            "name": "country",
            "in": "query",
            "schema": { "type": "string" },
            "description": "Filter by ISO 3166-1 alpha-2 country code"
          },
          {
            "name": "city",
            "in": "query",
            "schema": { "type": "string" },
            "description": "Filter by city name"
          }
        ],
        "responses": {
          "200": {
            "description": "Paginated list of venues",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": { "type": "array", "items": { "$ref": "#/components/schemas/Venue" } },
                    "meta": { "$ref": "#/components/schemas/PaginationMeta" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/venues/{id}": {
      "get": {
        "operationId": "getVenue",
        "summary": "Get venue by ID",
        "description": "Returns a single venue with full detail and nested media units.",
        "tags": ["Inventory"],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "format": "uuid" }
          }
        ],
        "responses": {
          "200": { "description": "Venue detail", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Venue" } } } },
          "404": { "description": "Not found", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } }
        }
      }
    },
    "/availability": {
      "get": {
        "operationId": "queryAvailability",
        "summary": "Query availability",
        "description": "Returns screen-day availability for the specified date range and optional media unit filter.",
        "tags": ["Availability"],
        "parameters": [
          {
            "name": "from",
            "in": "query",
            "required": true,
            "schema": { "type": "string", "format": "date" },
            "description": "Start date (inclusive)"
          },
          {
            "name": "to",
            "in": "query",
            "required": true,
            "schema": { "type": "string", "format": "date" },
            "description": "End date (inclusive)"
          },
          {
            "name": "media_unit_ids",
            "in": "query",
            "schema": { "type": "string" },
            "description": "Comma-separated media unit UUIDs to filter"
          },
          {
            "name": "venue_id",
            "in": "query",
            "schema": { "type": "string", "format": "uuid" },
            "description": "Filter by venue"
          },
          { "$ref": "#/components/parameters/PageParam" },
          { "$ref": "#/components/parameters/PageSizeParam" }
        ],
        "responses": {
          "200": {
            "description": "Availability slots",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": { "type": "array", "items": { "$ref": "#/components/schemas/AvailabilitySlot" } },
                    "meta": { "$ref": "#/components/schemas/PaginationMeta" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/markets": {
      "get": {
        "operationId": "listMarkets",
        "summary": "List markets",
        "description": "Returns defined markets / regions with geometry and media unit counts for campaign planning.",
        "tags": ["Planning"],
        "parameters": [
          { "$ref": "#/components/parameters/PageParam" },
          { "$ref": "#/components/parameters/PageSizeParam" }
        ],
        "responses": {
          "200": {
            "description": "List of markets",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": { "type": "array", "items": { "$ref": "#/components/schemas/Market" } },
                    "meta": { "$ref": "#/components/schemas/PaginationMeta" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/rate-cards": {
      "get": {
        "operationId": "listRateCards",
        "summary": "List rate cards",
        "description": "Returns pricing rate cards with scope, currency, and validity period.",
        "tags": ["Pricing"],
        "parameters": [
          { "$ref": "#/components/parameters/PageParam" },
          { "$ref": "#/components/parameters/PageSizeParam" },
          {
            "name": "media_unit_id",
            "in": "query",
            "schema": { "type": "string", "format": "uuid" },
            "description": "Filter rate cards applicable to a specific media unit"
          }
        ],
        "responses": {
          "200": {
            "description": "List of rate cards",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": { "type": "array", "items": { "$ref": "#/components/schemas/RateCard" } },
                    "meta": { "$ref": "#/components/schemas/PaginationMeta" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/health": {
      "get": {
        "operationId": "healthCheck",
        "summary": "Health check",
        "description": "Returns API health status. Use for monitoring and connectivity verification.",
        "tags": ["System"],
        "responses": {
          "200": {
            "description": "Service healthy",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HealthResponse" }
              }
            }
          }
        }
      }
    }
  }
}
