openapi: 3.1.0
info:
  title: Smart Pharmacy Hub API
  version: 0.1.0
  description: Core API contract for auth, tenant context, inventory, prescriptions, POS, and alerts.
servers:
  - url: https://api.smart-pharmacy-hub.example.com/v1
security:
  - bearerAuth: []
paths:
  /auth/login:
    post:
      security: []
      summary: Login with email and password
      tags: [Auth]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/LoginRequest'
      responses:
        '200':
          description: JWT token pair
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AuthResponse'
  /auth/me:
    get:
      summary: Get authenticated user profile
      tags: [Auth]
      responses:
        '200':
          description: Current user
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/UserProfile'
  /inventory/products:
    get:
      summary: List products
      tags: [Inventory]
      parameters:
        - $ref: '#/components/parameters/TenantId'
        - name: q
          in: query
          schema:
            type: string
        - name: barcode
          in: query
          schema:
            type: string
      responses:
        '200':
          description: Products
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Product'
    post:
      summary: Create product
      tags: [Inventory]
      parameters:
        - $ref: '#/components/parameters/TenantId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateProductRequest'
      responses:
        '201':
          description: Created product
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Product'
  /inventory/stock-lots:
    get:
      summary: List stock lots
      tags: [Inventory]
      parameters:
        - $ref: '#/components/parameters/TenantId'
        - name: pharmacyId
          in: query
          required: true
          schema:
            type: string
            format: uuid
        - name: productId
          in: query
          schema:
            type: string
            format: uuid
      responses:
        '200':
          description: Stock lots
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/StockLot'
    post:
      summary: Receive a stock lot
      tags: [Inventory]
      parameters:
        - $ref: '#/components/parameters/TenantId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ReceiveStockLotRequest'
      responses:
        '201':
          description: Created stock lot
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/StockLot'
  /inventory/alerts:
    get:
      summary: List low-stock, near-expiry, and expired inventory alerts
      tags: [Inventory]
      parameters:
        - $ref: '#/components/parameters/TenantId'
        - name: pharmacyId
          in: query
          required: true
          schema:
            type: string
            format: uuid
        - name: windowDays
          in: query
          schema:
            type: integer
            enum: [30, 60, 90]
            default: 90
      responses:
        '200':
          description: Inventory alerts
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/InventoryAlert'
  /prescriptions:
    post:
      summary: Receive e-prescription from doctor or HIS/EMR
      tags: [Prescriptions]
      parameters:
        - $ref: '#/components/parameters/TenantId'
      responses:
        '202':
          description: Accepted for clinical review
  /pos/orders:
    post:
      summary: Create POS order
      tags: [POS]
      parameters:
        - $ref: '#/components/parameters/TenantId'
      responses:
        '201':
          description: POS order created
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
  parameters:
    TenantId:
      name: X-Tenant-Id
      in: header
      required: true
      schema:
        type: string
        format: uuid
  schemas:
    LoginRequest:
      type: object
      required: [email, password, tenantId]
      properties:
        email:
          type: string
          format: email
        password:
          type: string
          format: password
        tenantId:
          type: string
          format: uuid
    AuthResponse:
      type: object
      required: [accessToken, refreshToken, user]
      properties:
        accessToken:
          type: string
        refreshToken:
          type: string
        user:
          $ref: '#/components/schemas/UserProfile'
    UserProfile:
      type: object
      required: [id, tenantId, email, fullName, roles]
      properties:
        id:
          type: string
          format: uuid
        tenantId:
          type: string
          format: uuid
        pharmacyId:
          type: string
          format: uuid
        email:
          type: string
        fullName:
          type: string
        roles:
          type: array
          items:
            type: string
    Product:
      type: object
      required: [id, tenantId, sku, nameEn, nameAr, reorderPoint]
      properties:
        id:
          type: string
          format: uuid
        tenantId:
          type: string
          format: uuid
        sku:
          type: string
        barcode:
          type: string
        qrCode:
          type: string
        nameEn:
          type: string
        nameAr:
          type: string
        activeIngredient:
          type: string
        dosageForm:
          type: string
        strength:
          type: string
        storageType:
          type: string
          enum: [ambient, refrigerated, frozen, controlled]
        reorderPoint:
          type: integer
        controlledDrug:
          type: boolean
    CreateProductRequest:
      allOf:
        - $ref: '#/components/schemas/Product'
        - type: object
          properties:
            id:
              readOnly: true
            tenantId:
              readOnly: true
    StockLot:
      type: object
      required: [id, tenantId, pharmacyId, productId, lotNumber, expiryDate, quantityOnHand]
      properties:
        id:
          type: string
          format: uuid
        tenantId:
          type: string
          format: uuid
        pharmacyId:
          type: string
          format: uuid
        productId:
          type: string
          format: uuid
        supplierId:
          type: string
          format: uuid
        lotNumber:
          type: string
        rfidTag:
          type: string
        expiryDate:
          type: string
          format: date
        quantityOnHand:
          type: integer
        reservedQuantity:
          type: integer
    ReceiveStockLotRequest:
      type: object
      required: [pharmacyId, productId, lotNumber, expiryDate, quantityOnHand]
      properties:
        pharmacyId:
          type: string
          format: uuid
        productId:
          type: string
          format: uuid
        supplierId:
          type: string
          format: uuid
        lotNumber:
          type: string
        rfidTag:
          type: string
        expiryDate:
          type: string
          format: date
        quantityOnHand:
          type: integer
          minimum: 0
        unitCost:
          type: number
    InventoryAlert:
      type: object
      required: [type, severity, productId, message]
      properties:
        type:
          type: string
          enum: [low_stock, near_expiry, expired]
        severity:
          type: string
          enum: [info, warning, critical]
        productId:
          type: string
          format: uuid
        stockLotId:
          type: string
          format: uuid
        message:
          type: string
