openapi: 3.0.0
info:
  title: Material
  version: 2.0.0
  description: >
    A **Material** represents a type of material managed in an office.

    Each Material is a model/type, not a single physical item —

    individual items are tracked as **MaterialItem** entries.


    Currently materials are only PPE (personal protective equipment,
    `material_type: PPE`),

    but other types may be added in the future.


    Examples: "Safety Helmet Model X", "Heat-Resistant Gloves Type Y", "Fall
    Protection Harness".


    Belongs to an office (via `subtenant_id` = office_id).


    Use this API to:

    - Register types of PPE available in an office

    - Get the `material_id` needed to create individual items (MaterialItem)

    - Browse the material inventory for an office (filter by `subtenant_id`)


    Supports historicization.
servers:
  - url: http://service.4hse.com/
security:
  - OAuth2: []
  - AccessToken: []
paths:
  /v2/material/index:
    post:
      summary: index
      operationId: indexMaterial
      tags:
        - Materials
      description: |
        Returns a paginated list of materials.

        Most useful filters:
        - `subtenant_id` (office_id): all materials in a specific office
        - `material_type`: filter by type (currently only `PPE`)
        - `name`: search by name
      requestBody:
        description: Parameters for searching materials
        required: false
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/MaterialIndexParams"
            example:
              filter:
                subtenant_id: a1b2c3d4-e5f6-7890-abcd-ef1234567890
                material_type: PPE
              per-page: 20
              page: 1
              sort: name
      responses:
        "200":
          description: List of materials
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/ViewMaterial"
          headers:
            X-Pagination-Current-Page:
              schema:
                type: integer
              description: Current page
            X-Pagination-Page-Count:
              schema:
                type: integer
              description: Total number of pages
            X-Pagination-Per-Page:
              schema:
                type: integer
              description: Number of items per page
            X-Pagination-Total-Count:
              schema:
                type: integer
              description: Total number of items
  /v2/material/create:
    post:
      summary: create
      operationId: createMaterial
      tags:
        - Materials
      description: >
        Registers a new material type in an office.


        Requires `material_type`, `subtenant_id` (office_id), `tenant_id`
        (project_id), and `name`.

        The `material_id` is auto-generated if not provided.


        After creating the material type, create **MaterialItem** entries for
        individual items or batches.
      requestBody:
        description: Material to register
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Material"
      responses:
        "201":
          description: Material registered successfully
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Material"
  /v2/material/view/{id}:
    get:
      summary: view
      operationId: viewMaterial
      tags:
        - Materials
      description: Retrieves a single material type by its ID.
      parameters:
        - name: id
          in: path
          description: The material_id to retrieve.
          required: true
          schema:
            type: string
            format: uuid
      responses:
        "200":
          description: Material found
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ViewMaterial"
        "404":
          description: Material not found
  /v2/material/update/{id}:
    put:
      summary: update
      operationId: updateMaterial
      tags:
        - Materials
      description: Updates an existing material type.
      parameters:
        - name: id
          in: path
          description: The material_id to update.
          required: true
          schema:
            type: string
            format: uuid
      requestBody:
        description: Fields to update
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Material"
      responses:
        "200":
          description: Material updated successfully
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Material"
        "404":
          description: Material not found
  /v2/material/delete/{id}:
    delete:
      summary: delete
      operationId: deleteMaterial
      tags:
        - Materials
      description: >
        Deletes a material type.


        If `force=false` (default), the response lists child entities
        (MaterialItem entries).

        If `force=true`, the material and all items are deleted.
      parameters:
        - name: id
          in: path
          description: The material_id to delete.
          required: true
          schema:
            type: string
            format: uuid
        - name: force
          in: query
          description: If true, deletes the material and all child entities.
          schema:
            type: boolean
            default: false
      responses:
        "204":
          description: Material deleted successfully
        "400":
          description: Preview of entities that would be deleted (returned when force=false)
components:
  schemas:
    Material:
      type: object
      required:
        - material_type
        - subtenant_id
        - tenant_id
        - name
      properties:
        material_id:
          type: string
          format: uuid
          description: Unique identifier of the material type. Auto-generated if not
            provided.
        material_type:
          type: string
          maxLength: 50
          enum:
            - PPE
          description: Type of material. Currently only `PPE` (personal protective
            equipment). Other types may be added in the future.
        code:
          type: string
          maxLength: 50
          description: Identifier code.
        name:
          type: string
          maxLength: 255
          description: Name of the material type.
        description:
          type: string
          nullable: true
          description: Optional description.
        category:
          type: string
          maxLength: 50
          description: Material category.
        data:
          type: object
          description: Additional structured data in JSON format.
        subtenant_id:
          type: string
          format: uuid
          description: The office this material belongs to. Pass the `office_id`.
        tenant_id:
          type: string
          format: uuid
          description: The project (company). Pass the `project_id`.
    ViewMaterial:
      type: object
      properties:
        material_id:
          type: string
          format: uuid
        material_type:
          type: string
          enum:
            - PPE
        code:
          type: string
        name:
          type: string
        description:
          type: string
        category:
          type: string
        data:
          type: object
        subtenant_id:
          type: string
          format: uuid
        tenant_id:
          type: string
          format: uuid
        office_name:
          type: string
          description: Name of the office.
        project_name:
          type: string
          description: Name of the project.
        project_type:
          type: string
          enum:
            - safety
            - template
        material_item_count:
          type: integer
          description: Number of available items (total minus items already assigned via
            certificates).
        owned_active:
          type: boolean
          nullable: true
          description: Whether this material is currently active.
        parent_active:
          type: boolean
          nullable: true
          description: Whether the parent office is currently active.
    MaterialIndexParams:
      type: object
      properties:
        filter:
          $ref: "#/components/schemas/ViewMaterial"
        per-page:
          type: integer
          minimum: 1
          default: 100
        page:
          type: integer
          minimum: 1
          default: 1
        sort:
          type: string
          enum:
            - code
            - name
            - category
            - material_type
        history:
          type: boolean
          description: If true, includes historicized entries.
      example:
        filter:
          subtenant_id: a1b2c3d4-e5f6-7890-abcd-ef1234567890
          material_type: PPE
        per-page: 100
        page: 1
        sort: name
  securitySchemes:
    OAuth2:
      type: oauth2
      flows:
        password:
          tokenUrl: https://auth.4hse.com/realms/4hse/protocol/openid-connect/token
          scopes: {}
    AccessToken:
      type: apiKey
      in: query
      name: access-token
