openapi: 3.0.3
info:
  title: Lightyshare API
  description: |
    API for client access/integrations via user token.
    
    ## Versioning
    
    Inspired by Stripe's versioning strategy, we use date-based versioning to maintain backwards compatibility.
    
    ### Version Format
    API versions use the format `YYYY-MM-DD` (e.g., `2025-08-16`).
    
    ### How Versioning Works
    
    1. **Token-based versioning**: When you create an API token, it's automatically tied to the latest API version available at that time
    2. **Request-level override**: You can override the token's version for specific requests using the `lighty-version` header
    3. **Stable URLs**: API URLs remain stable - version is handled transparently via token or header
    
    ### Examples
    
    ```bash
    # Using token's default version (2025-08-16 in this example)
    curl -H "Authorization: Bearer lsh_live_abc123..." \
         https://lightyshare.com/api/token-secured/rental/123
    
    # Override with header to use a different version
    curl -H "Authorization: Bearer lsh_live_abc123..." \
         -H "lighty-version: 2025-01-15" \
         https://lightyshare.com/api/token-secured/rental/123
    ```
    
    The API version used is always returned in the `X-API-Version` response header.
  version: 0.1.0
  contact:
    name: Support Lightyshare
    url: https://support.lightyshare.com
servers:
  - url: https://lightyshare.com/api/token-secured
    description: Stable API endpoint (version handled via token/header)

security:
  - tokenAuth: []

paths:
  /rental/{id}:
    get:
      summary: Get a rental
      description: Returns rental details if the authenticated user is the tenant or owner
      operationId: getRental
      tags:
        - Rentals
      parameters:
        - $ref: '#/components/parameters/idParam'
        - $ref: '#/components/parameters/tokenParam'
      responses:
        '200':
          description: Rental details
          headers:
            X-API-Version:
              $ref: '#/components/headers/ApiVersion'
          content:
            application/json:
              schema:
                type: object
                properties:
                  rentals:
                    type: object
                    description: Rental details (rental and selectAccounts serialization groups if paid)
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'

  /product/{id}:
    get:
      summary: Get a product
      description: Returns product details belonging to the authenticated user
      operationId: getProduct
      tags:
        - Products
      parameters:
        - $ref: '#/components/parameters/idParam'
        - $ref: '#/components/parameters/tokenParam'
      responses:
        '200':
          description: Product details
          headers:
            X-API-Version:
              $ref: '#/components/headers/ApiVersion'
          content:
            application/json:
              schema:
                type: object
                properties:
                  product:
                    type: object
                    description: Product details (ads group)
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'

    put:
      summary: Update a product
      description: Updates a product belonging to the authenticated user
      operationId: updateProduct
      tags:
        - Products
      parameters:
        - $ref: '#/components/parameters/idParam'
        - $ref: '#/components/parameters/tokenParam'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ProductUpdate'
      responses:
        '200':
          description: Product successfully updated
          headers:
            X-API-Version:
              $ref: '#/components/headers/ApiVersion'
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '404':
          $ref: '#/components/responses/NotFound'

  /product:
    post:
      summary: Create a product
      description: Creates a new product for the authenticated user
      operationId: createProduct
      tags:
        - Products
      parameters:
        - $ref: '#/components/parameters/tokenParam'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ProductCreate'
      responses:
        '201':
          description: Product successfully created
          headers:
            X-API-Version:
              $ref: '#/components/headers/ApiVersion'
            Location:
              schema:
                type: string
                example: /api/v1/token-secured/product/14567
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  id:
                    type: integer
                    example: 14567
                  location:
                    type: string
                    example: /api/token-secured/product/14567
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '429':
          $ref: '#/components/responses/TooManyRequests'

  /bundle:
    post:
      summary: Create a bundle
      description: Creates a new bundle (product pack) for the authenticated user
      operationId: createBundle
      tags:
        - Bundles
      parameters:
        - $ref: '#/components/parameters/tokenParam'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/BundleCreate'
      responses:
        '201':
          description: Bundle successfully created
          headers:
            X-API-Version:
              $ref: '#/components/headers/ApiVersion'
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  id:
                    type: integer
                    example: 98765
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '429':
          $ref: '#/components/responses/TooManyRequests'

components:
  securitySchemes:
    tokenAuth:
      type: http
      scheme: bearer
      bearerFormat: token
      description: |
        Use your API token in the Authorization header with Bearer scheme.
        Alternative: You can also pass the token as a query parameter `?token=your_token` for backward compatibility.

  parameters:
    idParam:
      name: id
      in: path
      required: true
      description: Resource identifier
      schema:
        type: integer
        example: 12345

    tokenParam:
      name: token
      in: query
      required: true
      description: User authentication token
      schema:
        type: string
        example: YOUR_TOKEN

  headers:
    ApiVersion:
      description: API version in YYYY-MM-DD format
      schema:
        type: string
        example: "2025-08-16"

  schemas:
    ProductCreate:
      type: object
      required:
        - product
      properties:
        product:
          type: object
          required:
            - title
            - rent
          properties:
            title:
              type: string
              maxLength: 255
              example: "4K Pro Camera"
            description:
              type: string
              description: Description in Markdown or HTML
              example: "Professional camera with full frame sensor"
            rent:
              type: number
              format: float
              minimum: 0.01
              example: 250.0
            cost:
              type: number
              format: float
              minimum: 0
              example: 3500.0
            productType:
              type: integer
              enum: [0, 1]
              default: 0
              description: "0 = product, 1 = bundle"
            stock_type:
              type: integer
              enum: [0, 1]
              default: 0
              description: "0 = unit stock, 1 = without unit management"
            sku:
              type: string
              description: Required if stock_type = 0
              example: "CAM-4K-001"
            quantity:
              type: integer
              minimum: 1
              default: 1
              example: 2
            equipments:
              type: array
              maxItems: 50
              items:
                type: object
                required:
                  - name
                  - quantity
                properties:
                  name:
                    type: string
                    example: "Battery"
                  quantity:
                    type: integer
                    minimum: 1
                    example: 4
            images:
              type: array
              maxItems: 5
              items:
                type: object
                required:
                  - src
                properties:
                  src:
                    type: string
                    format: uri
                    example: "https://cdn.example.com/camera-front.jpg"
            online:
              type: boolean
              default: true
            archived:
              type: boolean
              default: false

    ProductUpdate:
      type: object
      required:
        - product
      properties:
        product:
          type: object
          properties:
            title:
              type: string
              maxLength: 255
            description:
              type: string
            rent:
              type: number
              format: float
              minimum: 0.01
            cost:
              type: number
              format: float
              minimum: 0
            stock_type:
              type: integer
              enum: [0, 1]
            sku:
              type: string
            quantity:
              type: integer
              minimum: 1
            equipments:
              type: array
              maxItems: 50
              items:
                type: object
                required:
                  - name
                  - quantity
                properties:
                  name:
                    type: string
                  quantity:
                    type: integer
                    minimum: 1
            images:
              type: array
              maxItems: 5
              items:
                type: object
                required:
                  - src
                properties:
                  src:
                    type: string
                    format: uri
            online:
              type: boolean
            archived:
              type: boolean

    BundleCreate:
      type: object
      required:
        - bundle
      properties:
        bundle:
          type: object
          required:
            - title
            - items
          properties:
            title:
              type: string
              maxLength: 255
              example: "Complete LED Studio Pack"
            description:
              type: string
              example: "Professional kit for studio shooting"
            rent:
              type: number
              format: float
              description: Optional, automatically calculated if absent
              example: 190.0
            items:
              type: array
              minItems: 1
              maxItems: 30
              items:
                type: object
                required:
                  - ad_id
                  - quantity
                properties:
                  ad_id:
                    type: integer
                    example: 123
                  quantity:
                    type: integer
                    minimum: 1
                    example: 2
            images:
              type: array
              maxItems: 5
              items:
                type: object
                required:
                  - src
                properties:
                  src:
                    type: string
                    format: uri
                    example: "https://cdn.example.com/bundle-studio.jpg"
            online:
              type: boolean
              default: true
            archived:
              type: boolean
              default: false

    ErrorResponse:
      type: object
      properties:
        success:
          type: boolean
          example: false
        errors:
          type: array
          items:
            type: object
            properties:
              field:
                type: string
                example: "sku"
              code:
                type: string
                example: "REQUIRED"
              message:
                type: string
                example: "SKU is required for unit stock"

  responses:
    BadRequest:
      description: Invalid request
      headers:
        X-API-Version:
          $ref: '#/components/headers/ApiVersion'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'

    Unauthorized:
      description: Missing or invalid token
      headers:
        X-API-Version:
          $ref: '#/components/headers/ApiVersion'
      content:
        application/json:
          schema:
            type: object
            properties:
              error:
                type: string
                example: "Invalid or missing token"

    Forbidden:
      description: Access denied
      headers:
        X-API-Version:
          $ref: '#/components/headers/ApiVersion'
      content:
        application/json:
          schema:
            type: object
            properties:
              error:
                type: string
                example: "Access denied to this resource"

    NotFound:
      description: Resource not found
      headers:
        X-API-Version:
          $ref: '#/components/headers/ApiVersion'
      content:
        application/json:
          schema:
            type: object
            properties:
              error:
                type: string
                example: "Resource not found"

    TooManyRequests:
      description: Too many requests
      headers:
        X-API-Version:
          $ref: '#/components/headers/ApiVersion'
        Retry-After:
          description: Number of seconds before retry is allowed
          schema:
            type: integer
            example: 60
      content:
        application/json:
          schema:
            type: object
            properties:
              error:
                type: string
                example: "Request limit exceeded"