openapi: 3.1.0
info:
  title: Api
  version: 0.1.0
  description: نظام المحاسبة والمخازن
servers:
  - url: /api
    description: Base API path
tags:
  - name: health
    description: Health operations
  - name: suppliers
    description: إدارة الموردين
  - name: factories
    description: إدارة المصانع
  - name: products
    description: إدارة المنتجات والأصناف
  - name: supplyOrders
    description: أوامر التوريد
  - name: accounts
    description: إدارة الحسابات
  - name: transactions
    description: المعاملات المالية
  - name: payments
    description: الدفعات
  - name: reports
    description: التقارير والملخصات

paths:
  /healthz:
    get:
      operationId: healthCheck
      tags: [health]
      summary: Health check
      responses:
        "200":
          description: Healthy
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/HealthStatus"

  # Suppliers
  /suppliers:
    get:
      operationId: listSuppliers
      tags: [suppliers]
      summary: قائمة الموردين
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/Supplier"
    post:
      operationId: createSupplier
      tags: [suppliers]
      summary: إضافة مورد
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/SupplierInput"
      responses:
        "201":
          description: Created
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Supplier"

  /suppliers/{id}:
    get:
      operationId: getSupplier
      tags: [suppliers]
      summary: تفاصيل مورد
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Supplier"
    patch:
      operationId: updateSupplier
      tags: [suppliers]
      summary: تعديل مورد
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/SupplierUpdate"
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Supplier"
    delete:
      operationId: deleteSupplier
      tags: [suppliers]
      summary: حذف مورد
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      responses:
        "204":
          description: Deleted

  /supplier-statement:
    get:
      operationId: getSupplierStatement
      tags: [suppliers]
      summary: كشف حساب مورد
      parameters:
        - name: supplierId
          in: query
          required: true
          schema:
            type: integer
        - name: from
          in: query
          schema:
            type: ["string", "null"]
        - name: to
          in: query
          schema:
            type: ["string", "null"]
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SupplierStatement"

  # Factories
  /factories:
    get:
      operationId: listFactories
      tags: [factories]
      summary: قائمة المصانع
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/Factory"
    post:
      operationId: createFactory
      tags: [factories]
      summary: إضافة مصنع
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/FactoryInput"
      responses:
        "201":
          description: Created
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Factory"

  /factories/{id}:
    get:
      operationId: getFactory
      tags: [factories]
      summary: تفاصيل مصنع
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Factory"
    patch:
      operationId: updateFactory
      tags: [factories]
      summary: تعديل مصنع
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/FactoryUpdate"
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Factory"
    delete:
      operationId: deleteFactory
      tags: [factories]
      summary: حذف مصنع
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      responses:
        "204":
          description: Deleted

  # Products
  /products:
    get:
      operationId: listProducts
      tags: [products]
      summary: قائمة الأصناف
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/Product"
    post:
      operationId: createProduct
      tags: [products]
      summary: إضافة صنف
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/ProductInput"
      responses:
        "201":
          description: Created
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Product"

  /products/{id}:
    patch:
      operationId: updateProduct
      tags: [products]
      summary: تعديل صنف
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/ProductUpdate"
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Product"
    delete:
      operationId: deleteProduct
      tags: [products]
      summary: حذف صنف
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      responses:
        "204":
          description: Deleted

  # Supply Orders
  /supply-orders:
    get:
      operationId: listSupplyOrders
      tags: [supplyOrders]
      summary: قائمة أوامر التوريد
      parameters:
        - name: supplierId
          in: query
          schema:
            type: ["integer", "null"]
        - name: factoryId
          in: query
          schema:
            type: ["integer", "null"]
        - name: from
          in: query
          schema:
            type: ["string", "null"]
        - name: to
          in: query
          schema:
            type: ["string", "null"]
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/SupplyOrder"
    post:
      operationId: createSupplyOrder
      tags: [supplyOrders]
      summary: إضافة أمر توريد
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/SupplyOrderInput"
      responses:
        "201":
          description: Created
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SupplyOrder"

  /supply-orders/{id}:
    get:
      operationId: getSupplyOrder
      tags: [supplyOrders]
      summary: تفاصيل أمر توريد
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SupplyOrder"
    delete:
      operationId: deleteSupplyOrder
      tags: [supplyOrders]
      summary: حذف أمر توريد
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      responses:
        "204":
          description: Deleted

  # Accounts
  /accounts:
    get:
      operationId: listAccounts
      tags: [accounts]
      summary: قائمة الحسابات
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/Account"
    post:
      operationId: createAccount
      tags: [accounts]
      summary: إضافة حساب
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/AccountInput"
      responses:
        "201":
          description: Created
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Account"

  /accounts/{id}:
    get:
      operationId: getAccount
      tags: [accounts]
      summary: تفاصيل حساب
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Account"
    patch:
      operationId: updateAccount
      tags: [accounts]
      summary: تعديل حساب
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/AccountUpdate"
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Account"
    delete:
      operationId: deleteAccount
      tags: [accounts]
      summary: حذف حساب
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      responses:
        "204":
          description: Deleted

  /account-statement:
    get:
      operationId: getAccountStatement
      tags: [accounts]
      summary: كشف حساب
      parameters:
        - name: accountId
          in: query
          required: true
          schema:
            type: integer
        - name: from
          in: query
          schema:
            type: ["string", "null"]
        - name: to
          in: query
          schema:
            type: ["string", "null"]
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/AccountStatement"

  # Transactions (أذون قبض وصرف)
  /transactions:
    get:
      operationId: listTransactions
      tags: [transactions]
      summary: قائمة المعاملات المالية
      parameters:
        - name: type
          in: query
          schema:
            type: ["string", "null"]
            enum: ["receipt", "payment", null]
        - name: accountId
          in: query
          schema:
            type: ["integer", "null"]
        - name: from
          in: query
          schema:
            type: ["string", "null"]
        - name: to
          in: query
          schema:
            type: ["string", "null"]
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/Transaction"
    post:
      operationId: createTransaction
      tags: [transactions]
      summary: إضافة معاملة مالية
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/TransactionInput"
      responses:
        "201":
          description: Created
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Transaction"

  /transactions/{id}:
    get:
      operationId: getTransaction
      tags: [transactions]
      summary: تفاصيل معاملة
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Transaction"
    patch:
      operationId: updateTransaction
      tags: [transactions]
      summary: تعديل معاملة
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/TransactionUpdate"
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Transaction"
    delete:
      operationId: deleteTransaction
      tags: [transactions]
      summary: حذف معاملة
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      responses:
        "204":
          description: Deleted

  # Payments
  /payments:
    get:
      operationId: listPayments
      tags: [payments]
      summary: قائمة الدفعات
      parameters:
        - name: supplierId
          in: query
          schema:
            type: ["integer", "null"]
        - name: factoryId
          in: query
          schema:
            type: ["integer", "null"]
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/Payment"
    post:
      operationId: createPayment
      tags: [payments]
      summary: إضافة دفعة
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/PaymentInput"
      responses:
        "201":
          description: Created
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Payment"

  /payments/{id}:
    delete:
      operationId: deletePayment
      tags: [payments]
      summary: حذف دفعة
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: integer
      responses:
        "204":
          description: Deleted

  # Reports
  /reports/dashboard:
    get:
      operationId: getDashboardSummary
      tags: [reports]
      summary: ملخص لوحة التحكم
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/DashboardSummary"

  /reports/supplier-summary:
    get:
      operationId: getSupplierSummary
      tags: [reports]
      summary: ملخص الموردين
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/SupplierSummaryItem"

  /reports/factory-summary:
    get:
      operationId: getFactorySummary
      tags: [reports]
      summary: ملخص المصانع
      responses:
        "200":
          description: Success
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/FactorySummaryItem"

components:
  schemas:
    HealthStatus:
      type: object
      properties:
        status:
          type: string
      required:
        - status

    # Supplier
    Supplier:
      type: object
      required: [id, name, createdAt]
      properties:
        id:
          type: integer
        name:
          type: string
        phone:
          type: ["string", "null"]
        address:
          type: ["string", "null"]
        notes:
          type: ["string", "null"]
        balance:
          type: number
        createdAt:
          type: string

    SupplierInput:
      type: object
      required: [name]
      properties:
        name:
          type: string
        phone:
          type: string
        address:
          type: string
        notes:
          type: string

    SupplierUpdate:
      type: object
      properties:
        name:
          type: string
        phone:
          type: string
        address:
          type: string
        notes:
          type: string

    SupplierStatement:
      type: object
      required: [supplier, transactions, totalDebit, totalCredit, balance]
      properties:
        supplier:
          $ref: "#/components/schemas/Supplier"
        transactions:
          type: array
          items:
            $ref: "#/components/schemas/Transaction"
        totalDebit:
          type: number
        totalCredit:
          type: number
        balance:
          type: number

    # Factory
    Factory:
      type: object
      required: [id, name, createdAt]
      properties:
        id:
          type: integer
        name:
          type: string
        address:
          type: ["string", "null"]
        phone:
          type: ["string", "null"]
        notes:
          type: ["string", "null"]
        createdAt:
          type: string

    FactoryInput:
      type: object
      required: [name]
      properties:
        name:
          type: string
        address:
          type: string
        phone:
          type: string
        notes:
          type: string

    FactoryUpdate:
      type: object
      properties:
        name:
          type: string
        address:
          type: string
        phone:
          type: string
        notes:
          type: string

    # Product
    Product:
      type: object
      required: [id, name, unit, createdAt]
      properties:
        id:
          type: integer
        name:
          type: string
        unit:
          type: string
        createdAt:
          type: string

    ProductInput:
      type: object
      required: [name, unit]
      properties:
        name:
          type: string
        unit:
          type: string

    ProductUpdate:
      type: object
      properties:
        name:
          type: string
        unit:
          type: string

    # Supply Order
    SupplyOrderItem:
      type: object
      required: [id, productId, productName, unit, quantity, unitPrice, total]
      properties:
        id:
          type: integer
        productId:
          type: integer
        productName:
          type: string
        unit:
          type: string
        quantity:
          type: number
        unitPrice:
          type: number
        total:
          type: number

    SupplyOrder:
      type: object
      required: [id, supplierId, supplierName, factoryId, factoryName, date, documentNo, totalAmount, items, createdAt]
      properties:
        id:
          type: integer
        supplierId:
          type: integer
        supplierName:
          type: string
        factoryId:
          type: integer
        factoryName:
          type: string
        date:
          type: string
        documentNo:
          type: string
        totalAmount:
          type: number
        notes:
          type: ["string", "null"]
        items:
          type: array
          items:
            $ref: "#/components/schemas/SupplyOrderItem"
        createdAt:
          type: string

    SupplyOrderItemInput:
      type: object
      required: [productId, quantity, unitPrice]
      properties:
        productId:
          type: integer
        quantity:
          type: number
        unitPrice:
          type: number

    SupplyOrderInput:
      type: object
      required: [supplierId, factoryId, date, documentNo, items]
      properties:
        supplierId:
          type: integer
        factoryId:
          type: integer
        date:
          type: string
        documentNo:
          type: string
        notes:
          type: string
        items:
          type: array
          items:
            $ref: "#/components/schemas/SupplyOrderItemInput"

    # Account
    Account:
      type: object
      required: [id, name, type, balance, createdAt]
      properties:
        id:
          type: integer
        name:
          type: string
        type:
          type: string
          enum: [cash, bank, supplier, customer, expense, revenue, other]
        balance:
          type: number
        notes:
          type: ["string", "null"]
        createdAt:
          type: string

    AccountInput:
      type: object
      required: [name, type]
      properties:
        name:
          type: string
        type:
          type: string
          enum: [cash, bank, supplier, customer, expense, revenue, other]
        balance:
          type: number
        notes:
          type: string

    AccountUpdate:
      type: object
      properties:
        name:
          type: string
        type:
          type: string
          enum: [cash, bank, supplier, customer, expense, revenue, other]
        notes:
          type: string

    AccountStatement:
      type: object
      required: [account, transactions, totalDebit, totalCredit, balance]
      properties:
        account:
          $ref: "#/components/schemas/Account"
        transactions:
          type: array
          items:
            $ref: "#/components/schemas/Transaction"
        totalDebit:
          type: number
        totalCredit:
          type: number
        balance:
          type: number

    # Transaction
    Transaction:
      type: object
      required: [id, type, date, documentNo, accountId, accountName, amount, createdAt]
      properties:
        id:
          type: integer
        type:
          type: string
          enum: [receipt, payment]
        date:
          type: string
        documentNo:
          type: string
        accountId:
          type: integer
        accountName:
          type: string
        supplierId:
          type: ["integer", "null"]
        supplierName:
          type: ["string", "null"]
        amount:
          type: number
        notes:
          type: ["string", "null"]
        createdAt:
          type: string

    TransactionInput:
      type: object
      required: [type, date, documentNo, accountId, amount]
      properties:
        type:
          type: string
          enum: [receipt, payment]
        date:
          type: string
        documentNo:
          type: string
        accountId:
          type: integer
        supplierId:
          type: ["integer", "null"]
        amount:
          type: number
        notes:
          type: string

    TransactionUpdate:
      type: object
      properties:
        date:
          type: string
        documentNo:
          type: string
        accountId:
          type: integer
        supplierId:
          type: ["integer", "null"]
        amount:
          type: number
        notes:
          type: string

    # Payment
    Payment:
      type: object
      required: [id, date, amount, accountId, accountName, createdAt]
      properties:
        id:
          type: integer
        date:
          type: string
        amount:
          type: number
        accountId:
          type: integer
        accountName:
          type: string
        supplierId:
          type: ["integer", "null"]
        supplierName:
          type: ["string", "null"]
        factoryId:
          type: ["integer", "null"]
        factoryName:
          type: ["string", "null"]
        notes:
          type: ["string", "null"]
        createdAt:
          type: string

    PaymentInput:
      type: object
      required: [date, amount, accountId]
      properties:
        date:
          type: string
        amount:
          type: number
        accountId:
          type: integer
        supplierId:
          type: ["integer", "null"]
        factoryId:
          type: ["integer", "null"]
        notes:
          type: string

    # Reports
    DashboardSummary:
      type: object
      required: [totalSuppliers, totalFactories, totalSupplyOrders, totalSupplyAmount, totalReceipts, totalPayments, cashBalance, recentTransactions]
      properties:
        totalSuppliers:
          type: integer
        totalFactories:
          type: integer
        totalSupplyOrders:
          type: integer
        totalSupplyAmount:
          type: number
        totalReceipts:
          type: number
        totalPayments:
          type: number
        cashBalance:
          type: number
        recentTransactions:
          type: array
          items:
            $ref: "#/components/schemas/Transaction"

    SupplierSummaryItem:
      type: object
      required: [supplierId, supplierName, totalOrders, totalAmount, totalPaid, balance]
      properties:
        supplierId:
          type: integer
        supplierName:
          type: string
        totalOrders:
          type: integer
        totalAmount:
          type: number
        totalPaid:
          type: number
        balance:
          type: number

    FactorySummaryItem:
      type: object
      required: [factoryId, factoryName, totalOrders, totalAmount, totalItems]
      properties:
        factoryId:
          type: integer
        factoryName:
          type: string
        totalOrders:
          type: integer
        totalAmount:
          type: number
        totalItems:
          type: integer
