{
  "openapi": "3.1.0",
  "info": {
    "title": "Al-Gomaei CAD/CAM — Partner API",
    "version": "1.0.0",
    "description": "واجهة ربط أنظمة العيادات بمعمل الجُماعي كاد كام. التوثيق الكامل: https://cadcam.algomaei.com/docs/integration.md"
  },
  "servers": [
    {
      "url": "https://cadcam.lovable.app/api/public/partner"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "مفتاح الربط lab_live_…"
      }
    },
    "schemas": {
      "CaseItem": {
        "type": "object",
        "required": [
          "work_type_id"
        ],
        "properties": {
          "work_type_id": {
            "type": "string",
            "format": "uuid"
          },
          "teeth": {
            "type": "array",
            "maxItems": 32,
            "items": {
              "type": "integer",
              "minimum": 11,
              "maximum": 85
            }
          },
          "bridges": {
            "type": "array",
            "maxItems": 16,
            "items": {
              "type": "array",
              "items": {
                "type": "integer"
              }
            }
          },
          "quantity": {
            "type": "integer",
            "minimum": 1,
            "maximum": 200
          },
          "shade": {
            "type": "string",
            "maxLength": 40
          },
          "notes": {
            "type": "string",
            "maxLength": 1000
          },
          "field_values": {
            "type": "object",
            "additionalProperties": true
          }
        }
      },
      "CreateCase": {
        "type": "object",
        "required": [
          "patient_name",
          "items"
        ],
        "properties": {
          "external_ref": {
            "type": "string",
            "maxLength": 120,
            "description": "معرّف العمل في نظام العيادة — يمنع التكرار"
          },
          "patient_name": {
            "type": "string",
            "maxLength": 160
          },
          "patient_age": {
            "type": "integer",
            "minimum": 0,
            "maximum": 130
          },
          "patient_gender": {
            "type": "string",
            "enum": [
              "male",
              "female"
            ]
          },
          "case_type": {
            "type": "string",
            "enum": [
              "digital",
              "traditional"
            ]
          },
          "priority": {
            "type": "string",
            "enum": [
              "normal",
              "urgent"
            ]
          },
          "due_date": {
            "type": "string",
            "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
          },
          "notes": {
            "type": "string",
            "maxLength": 2000
          },
          "items": {
            "type": "array",
            "minItems": 1,
            "maxItems": 30,
            "items": {
              "$ref": "#/components/schemas/CaseItem"
            }
          }
        }
      },
      "CreatedCase": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "format": "uuid"
          },
          "code": {
            "type": "string"
          },
          "duplicate": {
            "type": "boolean"
          }
        }
      },
      "UploadFile": {
        "type": "object",
        "required": [
          "case",
          "name",
          "content_base64"
        ],
        "properties": {
          "case": {
            "type": "string",
            "description": "رقم الحالة أو معرّفها"
          },
          "name": {
            "type": "string",
            "maxLength": 200
          },
          "content_base64": {
            "type": "string"
          },
          "content_type": {
            "type": "string",
            "maxLength": 120
          }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string"
          },
          "message": {
            "type": "string"
          }
        }
      }
    },
    "responses": {
      "Unauthorized": {
        "description": "مفتاح غير صالح أو معطّل",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      }
    }
  },
  "paths": {
    "/connect/exchange": {
      "post": {
        "summary": "استبدال رمز الموافقة بمفتاح الربط (من خادم نظام العيادة)",
        "security": [],
        "parameters": [
          {
            "name": "x-app-id",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "x-app-secret",
            "in": "header",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "code"
                ],
                "properties": {
                  "code": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "api_key + account"
          },
          "400": {
            "description": "رمز غير صالح أو منتهٍ أو مستخدم"
          },
          "401": {
            "description": "بيانات النظام غير صحيحة"
          }
        }
      }
    },
    "/connect/revoke": {
      "post": {
        "summary": "فصل الربط بالمفتاح نفسه",
        "responses": {
          "200": {
            "description": "ok"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/ping": {
      "get": {
        "summary": "اختبار الاتصال ومعرفة الحساب",
        "responses": {
          "200": {
            "description": "ok"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/catalog": {
      "get": {
        "summary": "كتالوج الأعمال بأسعار حساب العيادة",
        "responses": {
          "200": {
            "description": "groups + work_types"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/cases": {
      "get": {
        "summary": "قائمة حالات العيادة",
        "parameters": [
          {
            "name": "status",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "search",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 200,
              "default": 50
            }
          }
        ],
        "responses": {
          "200": {
            "description": "cases[]"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      },
      "post": {
        "summary": "إرسال حالة جديدة",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateCase"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "طلب مكرر — أُعيدت نفس الحالة (duplicate: true)"
          },
          "201": {
            "description": "أُنشئت الحالة",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CreatedCase"
                }
              }
            }
          },
          "400": {
            "description": "بيانات غير صحيحة أو رفض من المعمل"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/cases/{id}": {
      "get": {
        "summary": "تفاصيل حالة (بالرقم أو المعرّف)",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "case, items, stages, files, reviews, payments, balance"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "description": "غير موجودة لهذا الحساب"
          }
        }
      }
    },
    "/files": {
      "post": {
        "summary": "إرفاق ملف بحالة",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/UploadFile"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "file + case"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "413": {
            "description": "الملف أكبر من 25MB"
          }
        }
      }
    },
    "/statement": {
      "get": {
        "summary": "كشف الحساب مفصولاً بالعملة",
        "parameters": [
          {
            "name": "from",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date-time"
            }
          },
          {
            "name": "to",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "date-time"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "totals, cases, payments"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    }
  },
  "x-webhooks": {
    "case.updated": {
      "description": "تغيّر حالة الإنتاج أو التسليم"
    },
    "payment.recorded": {
      "description": "تسجيل دفعة"
    },
    "review.requested": {
      "description": "طلب موافقة على التصميم"
    },
    "x-signature": "X-Lab-Signature = HMAC SHA-256 (hex) للنص الخام بسر التوقيع"
  }
}