openapi: 3.0.3
info:
  title: Product Rentals API
  version: 1.0.0
  description: API for managing product rental functionality in Medusa.js e-commerce platform
servers:
  - url: '{BACKEND_URL}'
    description: Backend server
    variables:
      BACKEND_URL:
        default: http://localhost:9000
        description: Base URL of the backend server
tags:
  - name: Admin
  - name: Store
  - name: Admin > Auth
  - name: Admin > Products
  - name: Admin > Orders
  - name: Admin > Rentals
  - name: Store > Products
  - name: Store > Carts
  - name: Store > Rentals
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
  schemas:
    # Authentication Schemas
    LoginRequest:
      type: object
      required: [email, password]
      properties:
        email:
          type: string
          format: email
        password:
          type: string
          format: password
      example:
        email: admin@medusajs.com
        password: supersecret

    LoginResponse:
      type: object
      properties:
        token:
          type: string
          description: JWT access token
      example:
        token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

    # Rental Configuration Schemas
    RentalConfiguration:
      type: object
      properties:
        id:
          type: string
        product_id:
          type: string
        min_rental_days:
          type: number
          default: 1
        max_rental_days:
          type: number
          nullable: true
        status:
          type: string
          enum: [active, inactive]
          default: active
      example:
        id: rental_config_01K097J3012YJ0CMZRM7WNGMGN
        product_id: prod_01K097J3012YJ0CMZRM7WNGMGN
        min_rental_days: 1
        max_rental_days: 30
        status: active

    RentalConfigRequest:
      type: object
      properties:
        min_rental_days:
          type: number
        max_rental_days:
          type: number
          nullable: true
        status:
          type: string
          enum: [active, inactive]
      example:
        min_rental_days: 1
        max_rental_days: 30
        status: active

    RentalConfigResponse:
      type: object
      properties:
        rental_config:
          $ref: '#/components/schemas/RentalConfiguration'

    # Rental Schemas
    Rental:
      type: object
      properties:
        id:
          type: string
        variant_id:
          type: string
        customer_id:
          type: string
        order_id:
          type: string
          nullable: true
        line_item_id:
          type: string
          nullable: true
        rental_start_date:
          type: string
          format: date-time
        rental_end_date:
          type: string
          format: date-time
        actual_return_date:
          type: string
          format: date-time
          nullable: true
        rental_days:
          type: number
        status:
          type: string
          enum: [pending, active, returned, cancelled]
          default: pending
        product_variant:
          type: object
          properties:
            id:
              type: string
            title:
              type: string
            product:
              type: object
              properties:
                id:
                  type: string
                title:
                  type: string
                thumbnail:
                  type: string
      example:
        id: rental_01K097J3012YJ0CMZRM7WNGMGN
        variant_id: variant_01K097J3012YJ0CMZRM7WNGMGN
        customer_id: cus_01K097J3012YJ0CMZRM7WNGMGN
        order_id: order_01K097J3012YJ0CMZRM7WNGMGN
        line_item_id: li_01K097J3012YJ0CMZRM7WNGMGN
        rental_start_date: "2024-01-15T00:00:00.000Z"
        rental_end_date: "2024-01-20T23:59:59.999Z"
        actual_return_date: null
        rental_days: 6
        status: pending

    RentalStatusRequest:
      type: object
      required: [status]
      properties:
        status:
          type: string
          enum: [active, returned, cancelled]
      example:
        status: active

    RentalResponse:
      type: object
      properties:
        rental:
          $ref: '#/components/schemas/Rental'

    RentalsListResponse:
      type: object
      properties:
        rentals:
          type: array
          items:
            $ref: '#/components/schemas/Rental'

    # Rental Availability Schemas
    RentalAvailabilityQuery:
      type: object
      required: [variant_id, start_date]
      properties:
        variant_id:
          type: string
        start_date:
          type: string
          format: date
          description: Start date in YYYY-MM-DD format
        end_date:
          type: string
          format: date
          description: End date in YYYY-MM-DD format (optional, defaults to same day)
        currency_code:
          type: string
          description: Currency code for price calculation
      example:
        variant_id: variant_01K097J3012YJ0CMZRM7WNGMGN
        start_date: "2024-01-15"
        end_date: "2024-01-20"
        currency_code: "usd"

    RentalAvailabilityResponse:
      type: object
      properties:
        available:
          type: boolean
        message:
          type: string
          description: Error message when rental is not available
        price:
          type: object
          properties:
            amount:
              type: number
            currency_code:
              type: string
      example:
        available: true
        price:
          amount: 15000
          currency_code: "usd"

    # Cart Rental Schemas
    CartRentalRequest:
      type: object
      required: [variant_id, quantity]
      properties:
        variant_id:
          type: string
        quantity:
          type: number
        metadata:
          type: object
          additionalProperties: true
          description: Additional metadata for the rental
      example:
        variant_id: variant_01K097J3012YJ0CMZRM7WNGMGN
        quantity: 1
        metadata:
          rental_start_date: "2024-01-15"
          rental_end_date: "2024-01-20"

    CartResponse:
      type: object
      properties:
        cart:
          type: object
          description: Updated cart object
      example:
        cart:
          id: cart_01K097J3012YJ0CMZRM7WNGMGN
          items: []

    # Order Creation Schemas
    OrderCreationResponse:
      type: object
      properties:
        type:
          type: string
          enum: [order]
        order:
          type: object
          description: Created order object
      example:
        type: order
        order:
          id: order_01K097J3012YJ0CMZRM7WNGMGN
          status: pending

    # Error Schemas
    ErrorResponse:
      type: object
      properties:
        message:
          type: string
        code:
          type: string
        type:
          type: string
          enum: [error, warning]
      example:
        message: "Product is not rentable"
        code: "NOT_FOUND"
        type: "error"

security:
  - bearerAuth: []

paths:
  # Authentication Endpoints
  /auth/user/emailpass:
    post:
      tags:
        - Admin > Auth
      summary: Admin Login
      description: Authenticate admin user with email and password
      security: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/LoginRequest'
      responses:
        '200':
          description: Successful authentication
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/LoginResponse'
        '401':
          description: Invalid credentials
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  # Admin Product Rental Configuration Endpoints
  /admin/products/{id}/rental-config:
    get:
      tags:
        - Admin > Products
      summary: Get Product Rental Configuration
      description: Retrieve rental configuration for a specific product
      security:
        - bearerAuth: []
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
          description: Product ID
          example: prod_01K097J3012YJ0CMZRM7WNGMGN
      responses:
        '200':
          description: Product rental configuration
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RentalConfigResponse'
        '404':
          description: Product not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

    post:
      tags:
        - Admin > Products
      summary: Create or Update Product Rental Configuration
      description: Create or update rental configuration for a product
      security:
        - bearerAuth: []
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
          description: Product ID
          example: prod_01K097J3012YJ0CMZRM7WNGMGN
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RentalConfigRequest'
      responses:
        '200':
          description: Rental configuration created/updated
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RentalConfigResponse'
        '400':
          description: Invalid request data
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  # Admin Rental Management Endpoints
  /admin/rentals/{id}:
    post:
      tags:
        - Admin > Rentals
      summary: Update Rental Status
      description: Update the status of a specific rental
      security:
        - bearerAuth: []
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
          description: Rental ID
          example: rental_01K097J3012YJ0CMZRM7WNGMGN
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RentalStatusRequest'
      responses:
        '200':
          description: Rental status updated
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RentalResponse'
        '400':
          description: Invalid request data
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '404':
          description: Rental not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  # Admin Order Rentals Endpoints
  /admin/orders/{id}/rentals:
    get:
      tags:
        - Admin > Orders
      summary: Get Order Rentals
      description: Retrieve all rentals associated with a specific order
      security:
        - bearerAuth: []
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
          description: Order ID
          example: order_01K097J3012YJ0CMZRM7WNGMGN
      responses:
        '200':
          description: Order rentals
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RentalsListResponse'
        '404':
          description: Order not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  # Store Product Rental Availability Endpoints
  /store/products/{id}/rental-availability:
    get:
      tags:
        - Store > Products
      summary: Check Product Rental Availability
      description: Check if a product variant is available for rental during specified dates
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
          description: Product ID
          example: prod_01K097J3012YJ0CMZRM7WNGMGN
        - name: variant_id
          in: query
          required: true
          schema:
            type: string
          description: Product variant ID
          example: variant_01K097J3012YJ0CMZRM7WNGMGN
        - name: start_date
          in: query
          required: true
          schema:
            type: string
            format: date
          description: Rental start date (YYYY-MM-DD)
          example: "2024-01-15"
        - name: end_date
          in: query
          required: false
          schema:
            type: string
            format: date
          description: Rental end date (YYYY-MM-DD), defaults to same day
          example: "2024-01-20"
        - name: currency_code
          in: query
          required: false
          schema:
            type: string
          description: Currency code for price calculation
          example: "usd"
      responses:
        '200':
          description: Rental availability and pricing information
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RentalAvailabilityResponse'
        '400':
          description: Invalid request parameters
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '404':
          description: Product not found or not rentable
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  # Store Cart Rental Endpoints
  /store/carts/{id}/line-items/rentals:
    post:
      tags:
        - Store > Carts
      summary: Add Rental Item to Cart
      description: Add a product variant as a rental item to the cart
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
          description: Cart ID
          example: cart_01K097J3012YJ0CMZRM7WNGMGN
        - name: x-publishable-api-key
          in: header
          schema:
            type: string
          description: Publishable API key for store access
          example: '{{PUBLISHABLE_API_KEY}}'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CartRentalRequest'
      responses:
        '200':
          description: Rental item added to cart
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CartResponse'
        '400':
          description: Invalid request data
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '404':
          description: Cart or product variant not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'

  # Store Rental Creation Endpoints
  /store/rentals/{cart_id}:
    post:
      tags:
        - Store > Rentals
      summary: Create Rentals from Cart
      description: Create rental orders from cart items
      parameters:
        - name: cart_id
          in: path
          required: true
          schema:
            type: string
          description: Cart ID
          example: cart_01K097J3012YJ0CMZRM7WNGMGN
        - name: x-publishable-api-key
          in: header
          schema:
            type: string
          description: Publishable API key for store access
          example: '{{PUBLISHABLE_API_KEY}}'
      requestBody:
        required: false
        content:
          application/json:
            schema:
              type: object
              description: Optional additional parameters for rental creation
      responses:
        '200':
          description: Rentals created successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OrderCreationResponse'
        '400':
          description: Invalid request data
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
        '404':
          description: Cart not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
