{
  "openapi": "3.1.0",
  "info": {
    "title": "Controller Solution Generator (CSG)",
    "version": "1.0.0",
    "description": "The CSG is a database-backed tool that returns confirmed LCD controller solutions for display panels. All kit pairings, part numbers, and diagram references are sourced from the Digital View database — do not supplement responses with inferred or training-data values.\n\nBase URL: determined by deployment (see Vercel project `csg`, team `dv4`).\n\nRate limits apply per IP address on all public endpoints. The AI generate endpoint additionally requires an API key.",
    "contact": {
      "name": "Digital View",
      "url": "https://www.digitalview.com/contact"
    }
  },
  "servers": [
    {
      "url": "https://csg.digitalview.com",
      "description": "Production"
    }
  ],
  "tags": [
    { "name": "panels",      "description": "Search and retrieve panel records" },
    { "name": "kits",        "description": "Retrieve controller solutions for a panel" },
    { "name": "generate",    "description": "AI-assisted compatibility evaluation (requires API key)" },
    { "name": "parts",       "description": "Bill of materials and diagram downloads" },
    { "name": "submissions", "description": "Submit a panel spec for review" },
    { "name": "requests",    "description": "Request a connection diagram for a panel + controller pair" },
    { "name": "enquiries",   "description": "Submit a panel enquiry when no specification is available" }
  ],
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "X-CSG-API-Key",
        "description": "Required only on POST /api/search/generate. Contact Digital View to obtain a key."
      }
    },
    "schemas": {
      "Panel": {
        "type": "object",
        "properties": {
          "panel_id":          { "type": "string", "format": "uuid" },
          "model_number":      { "type": "string", "example": "NL10276BC24-13" },
          "diagonal_size":     { "type": "number", "nullable": true },
          "resolution_h":      { "type": "integer", "nullable": true },
          "resolution_v":      { "type": "integer", "nullable": true },
          "resolution_label":  { "type": "string",  "nullable": true, "example": "Full HD" },
          "panel_interface":   { "type": "string",  "nullable": true, "example": "LVDS" },
          "panel_voltage_typ": { "type": "number",  "nullable": true },
          "manufacturers":     { "$ref": "#/components/schemas/Manufacturer" }
        }
      },
      "Manufacturer": {
        "type": "object",
        "properties": {
          "name": { "type": "string", "example": "NEC" }
        }
      },
      "Kit": {
        "type": "object",
        "properties": {
          "kit_id":             { "type": "string", "format": "uuid" },
          "kit_number":         { "type": "string", "nullable": true },
          "status": {
            "type": "string",
            "enum": ["Confirmed", "Suggested", "AI-Suggested"],
            "description": "Confirmed = physically tested by Digital View engineers. Suggested = connection diagram produced by Digital View, suitable for sample ordering, not yet production-verified. AI-Suggested = generated by the CSG AI engine, not yet physically verified."
          },
          "panel_cable_pns":     { "type": "array", "items": { "type": "string" }, "nullable": true, "description": "Panel signal cable part numbers (may be multiple for dual-channel panels)" },
          "backlight_cable_pns": { "type": "array", "items": { "type": "string" }, "nullable": true, "description": "Backlight cable part numbers (may be multiple)" },
          "notes":               { "type": "string", "nullable": true },
          "controllers":         { "$ref": "#/components/schemas/Controller" },
          "connection_diagrams": { "$ref": "#/components/schemas/Diagram" }
        }
      },
      "Controller": {
        "type": "object",
        "properties": {
          "controller_id":       { "type": "string", "format": "uuid" },
          "part_number":         { "type": "string", "example": "4177000XX-3" },
          "family":              { "type": "string", "nullable": true },
          "description":         { "type": "string", "nullable": true },
          "supported_interfaces":{ "type": "array", "items": { "type": "string" } },
          "max_resolution_h":    { "type": "integer", "nullable": true },
          "max_resolution_v":    { "type": "integer", "nullable": true },
          "is_legacy":           { "type": "boolean" },
          "product_url":         { "type": "string", "nullable": true }
        }
      },
      "Diagram": {
        "type": "object",
        "properties": {
          "diagram_id":     { "type": "string", "format": "uuid" },
          "diagram_number": { "type": "string", "nullable": true, "example": "001481" },
          "diagram_file":   { "type": "string" },
          "revision":       { "type": "string", "nullable": true },
          "issue_date":     { "type": "string", "format": "date", "nullable": true }
        }
      },
      "ControllerReport": {
        "type": "object",
        "properties": {
          "controller_id":    { "type": "string", "format": "uuid" },
          "part_number":      { "type": "string" },
          "family":           { "type": "string", "nullable": true },
          "kit_id":           { "type": "string", "format": "uuid", "nullable": true },
          "confidence": {
            "type": "string",
            "enum": ["High", "Medium", "Review-Needed"]
          },
          "checks": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "name":      { "type": "string" },
                "pass":      { "type": "boolean" },
                "uncertain": { "type": "boolean" },
                "detail":    { "type": "string" }
              }
            }
          },
          "jumper_settings":    { "type": "object", "nullable": true, "additionalProperties": { "type": "string" } },
          "reasoning":          { "type": "string" },
          "already_confirmed":  { "type": "boolean" }
        }
      },
      "RateLimitError": {
        "type": "object",
        "properties": {
          "error": { "type": "string", "example": "Too many requests" }
        }
      },
      "PanelEnquiryInput": {
        "type": "object",
        "required": ["panel_model", "contact_email"],
        "properties": {
          "panel_model":      { "type": "string", "description": "Panel model number the user is looking for" },
          "contact_email":    { "type": "string", "format": "email", "description": "Contact email for follow-up" },
          "contact_name":     { "type": "string", "description": "Requester name (optional)" },
          "contact_company":  { "type": "string", "description": "Requester company (optional)" },
          "notes":            { "type": "string", "description": "Additional context (optional)" }
        }
      },
      "PanelEnquiryResponse": {
        "type": "object",
        "properties": {
          "ok":           { "type": "boolean" },
          "panel_model":  { "type": "string" }
        }
      },
      "KitRequestInput": {
        "type": "object",
        "required": ["panel_id", "controller_id", "requester_email"],
        "properties": {
          "panel_id":           { "type": "string", "format": "uuid", "description": "UUID of the panel" },
          "controller_id":      { "type": "string", "format": "uuid", "description": "UUID of the controller" },
          "requester_email":    { "type": "string", "format": "email", "description": "Contact email for follow-up" },
          "requester_name":     { "type": "string", "description": "Requester name (optional)" },
          "requester_company":  { "type": "string", "description": "Requester company (optional)" },
          "notes":              { "type": "string", "description": "Additional context (optional)" }
        }
      },
      "KitRequestResponse": {
        "type": "object",
        "properties": {
          "ok":             { "type": "boolean" },
          "panel_model":    { "type": "string", "description": "Denormalised panel model number" },
          "controller_pn":  { "type": "string", "description": "Denormalised controller part number or family" }
        }
      },
      "NotFoundResponse": {
        "type": "object",
        "properties": {
          "panel":    { "type": "null" },
          "kits":     { "type": "array", "items": {}, "maxItems": 0 },
          "diagrams": { "type": "array", "items": {}, "maxItems": 0 },
          "message":  { "type": "string", "example": "No panel found for this model number. Do not suggest alternatives." }
        }
      }
    },
    "responses": {
      "429RateLimit": {
        "description": "Rate limit exceeded",
        "headers": {
          "X-RateLimit-Limit":     { "schema": { "type": "integer" } },
          "X-RateLimit-Remaining": { "schema": { "type": "integer" } },
          "X-RateLimit-Reset":     { "schema": { "type": "integer" } },
          "Retry-After":           { "schema": { "type": "integer" } }
        },
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/RateLimitError" }
          }
        }
      }
    }
  },
  "paths": {
    "/api/search/panels": {
      "get": {
        "operationId": "searchPanels",
        "tags": ["panels"],
        "summary": "Search panels by model number",
        "description": "Returns panels whose model number matches the query string. Use this to resolve a user-supplied model number to a panel_id before calling /api/search/results. Returns an empty array if no match is found — do not substitute suggestions from training data.",
        "parameters": [
          {
            "name": "q",
            "in": "query",
            "required": true,
            "schema": { "type": "string" },
            "description": "Partial or full panel model number, e.g. NL10276BC24",
            "example": "NL10276BC24"
          }
        ],
        "responses": {
          "200": {
            "description": "List of matching panels (may be empty)",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": { "$ref": "#/components/schemas/Panel" }
                }
              }
            }
          },
          "429": { "$ref": "#/components/responses/429RateLimit" }
        }
      }
    },
    "/api/search/results": {
      "get": {
        "operationId": "getPanelResults",
        "tags": ["kits"],
        "summary": "Get panel details and all known kits",
        "description": "Returns full panel specifications plus all Confirmed and AI-Suggested kits for the panel. If kits array is empty, report that no solutions are currently available — do not generate alternatives. Always surface the status field to the user.",
        "parameters": [
          {
            "name": "panel_id",
            "in": "query",
            "required": true,
            "schema": { "type": "string", "format": "uuid" },
            "description": "UUID of the panel, obtained from /api/search/panels"
          }
        ],
        "responses": {
          "200": {
            "description": "Panel details and kits",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "panel":    { "$ref": "#/components/schemas/Panel" },
                    "kits":     { "type": "array", "items": { "$ref": "#/components/schemas/Kit" } },
                    "diagrams": { "type": "array", "items": { "$ref": "#/components/schemas/Diagram" } },
                    "message":  { "type": "string", "description": "Present only when kits is empty. Instructs AI agents not to generate alternatives from training data, and surfaces the user's next steps.", "example": "No confirmed controller solutions are currently available for this panel. Do not suggest alternatives or generate pairings from training data. Next steps: (1) use GET /api/search/compatible-controllers to find candidates for AI evaluation via POST /api/search/generate; (2) the user can upload a panel specification PDF at /suggest for Digital View to assess; (3) or contact Digital View directly at https://www.digitalview.com/contact." }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Panel not found",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/NotFoundResponse" }
              }
            }
          },
          "429": { "$ref": "#/components/responses/429RateLimit" }
        }
      }
    },
    "/api/search/compatible-controllers": {
      "get": {
        "operationId": "getCompatibleControllers",
        "tags": ["kits"],
        "summary": "Get controllers compatible with a panel (no existing kit)",
        "description": "Returns active, non-legacy controllers that are spec-compatible with the panel but do not yet have a confirmed kit or diagram. These are candidates for the AI generate endpoint. If the array is empty, no further evaluation is possible.",
        "parameters": [
          {
            "name": "panel_id",
            "in": "query",
            "required": true,
            "schema": { "type": "string", "format": "uuid" }
          }
        ],
        "responses": {
          "200": {
            "description": "List of compatible controllers",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "controllers": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/Controller" }
                    }
                  }
                }
              }
            }
          },
          "429": { "$ref": "#/components/responses/429RateLimit" }
        }
      }
    },
    "/api/search/generate": {
      "post": {
        "operationId": "generateCompatibilityEvaluation",
        "tags": ["generate"],
        "summary": "AI-assisted compatibility evaluation",
        "description": "Runs the CSG AI engine to evaluate compatibility between a panel and one or more controllers. Creates or updates AI-Suggested kit records in the database. **Requires an API key** in the X-CSG-API-Key header. Only call this when no Confirmed kit exists for the panel — check /api/search/results first.",
        "security": [{ "ApiKeyAuth": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["panel_id", "controller_ids"],
                "properties": {
                  "panel_id": {
                    "type": "string",
                    "format": "uuid",
                    "description": "UUID of the panel to evaluate"
                  },
                  "controller_ids": {
                    "type": "array",
                    "items": { "type": "string", "format": "uuid" },
                    "description": "One or more controller UUIDs to evaluate against the panel"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Compatibility evaluation results",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "results": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/ControllerReport" }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API key",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": { "error": { "type": "string" } }
                }
              }
            }
          },
          "429": { "$ref": "#/components/responses/429RateLimit" }
        }
      }
    },
    "/api/search/bom": {
      "get": {
        "operationId": "getPartsListCsv",
        "tags": ["parts"],
        "summary": "Download parts list (BOM) as CSV",
        "description": "Returns a CSV bill of materials for the specified kit, including controller, cables, adaptor board, and any additional accessories. The file is named CSG_BOM_{PanelModel}_{ControllerPN}.csv.",
        "parameters": [
          {
            "name": "kit_id",
            "in": "query",
            "required": true,
            "schema": { "type": "string", "format": "uuid" },
            "description": "UUID of the kit, obtained from /api/search/results"
          }
        ],
        "responses": {
          "200": {
            "description": "CSV parts list",
            "content": {
              "text/csv": {
                "schema": { "type": "string" }
              }
            }
          },
          "404": {
            "description": "Kit not found"
          },
          "429": { "$ref": "#/components/responses/429RateLimit" }
        }
      }
    },
    "/api/diagrams/{id}/download": {
      "post": {
        "operationId": "getDiagramDownloadUrl",
        "tags": ["parts"],
        "summary": "Get signed download URL for a connection diagram PDF",
        "description": "Logs the download event and returns the URL for the connection diagram PDF. The URL is publicly accessible for a limited time.",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "string", "format": "uuid" },
            "description": "UUID of the connection diagram, obtained from kit.connection_diagrams"
          }
        ],
        "responses": {
          "200": {
            "description": "Download URL",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "url":      { "type": "string", "format": "uri" },
                    "filename": { "type": "string" }
                  }
                }
              }
            }
          },
          "404": { "description": "Diagram not found" },
          "429": { "$ref": "#/components/responses/429RateLimit" }
        }
      }
    },
    "/api/suggest": {
      "post": {
        "operationId": "submitPanelSpec",
        "tags": ["submissions"],
        "summary": "Submit a panel spec PDF for review",
        "description": "Accepts a panel datasheet PDF for assessment by the Digital View team. The AI engine extracts specifications and generates preliminary controller recommendations. A notification is sent to Digital View staff. Rate-limited to 5 submissions per hour per IP.",
        "requestBody": {
          "required": true,
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "required": ["file"],
                "properties": {
                  "file": {
                    "type": "string",
                    "format": "binary",
                    "description": "Panel datasheet PDF, max 10 MB"
                  },
                  "requester_email": {
                    "type": "string",
                    "format": "email",
                    "description": "Optional contact email for follow-up"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Submission accepted",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "request_id": { "type": "string", "format": "uuid" },
                    "message":    { "type": "string" }
                  }
                }
              }
            }
          },
          "429": { "$ref": "#/components/responses/429RateLimit" }
        }
      }
    },
    "/api/search/panel-enquiry": {
      "post": {
        "operationId": "submitPanelEnquiry",
        "tags": ["enquiries"],
        "summary": "Submit a panel enquiry when no specification PDF is available",
        "description": "Creates a panel enquiry when a user searches for a panel not in the database and wants Digital View to follow up. A notification is sent to Digital View staff. Rate-limited to 5 requests per hour per IP (shared with the suggest limiter).",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/PanelEnquiryInput" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Enquiry submitted successfully",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/PanelEnquiryResponse" }
              }
            }
          },
          "400": {
            "description": "Missing required fields or invalid email"
          },
          "429": { "$ref": "#/components/responses/429RateLimit" }
        }
      }
    },
    "/api/search/request-connection": {
      "post": {
        "operationId": "requestConnectionDiagram",
        "tags": ["requests"],
        "summary": "Request a connection diagram for a panel + controller pair",
        "description": "Creates a kit request when a user needs a connection diagram for a specific panel and controller combination that doesn't yet have one. A notification is sent to Digital View staff. Rate-limited to 5 requests per hour per IP (shared with the suggest limiter).",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/KitRequestInput" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Request created successfully",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/KitRequestResponse" }
              }
            }
          },
          "400": {
            "description": "Missing required fields or invalid email"
          },
          "404": {
            "description": "Panel or controller not found"
          },
          "429": { "$ref": "#/components/responses/429RateLimit" }
        }
      }
    }
  }
}
