openapi: 3.0.3
info:
  title: Lemis Dashboard API
  description: OpenAPI spec for the Lemis Backend (compatible with both Python Flask and PHP Slim)
  version: '2.0.0'
servers:
  - url: http://localhost:5000/api
    description: Python Flask Backend
  - url: http://localhost:8080/api
    description: PHP Slim Backend
  - url: http://localhost:80/api
    description: Production Environment
paths:
  /auth/login:
    post:
      summary: User login
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                username:
                  type: string
                password:
                  type: string
              required: [username, password]
      responses:
        '200':
          description: Login successful
        '401':
          description: Invalid credentials

  /auth/register:
    post:
      summary: Register a new user
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                username: { type: string }
                email: { type: string }
                password: { type: string }
              required: [username, email, password]
      responses:
        '201': { description: User registered }

  /auth/refresh:
    post:
      summary: Refresh JWT token (use refresh token)
      responses:
        '200': { description: Token refreshed }

  /auth/logout:
    post:
      summary: Logout (invalidates on client)
      security:
        - bearerAuth: []
      responses:
        '200': { description: Logout successful }

  /dashboard/summary:
    get:
      summary: Dashboard summary data
      security:
        - bearerAuth: []
      responses:
        '200':
          description: Dashboard summary data
          content:
            application/json:
              schema:
                type: object
                properties:
                  total_sales: { type: number }
                  total_orders: { type: number }
                  average_ticket: { type: number }
                  total_products: { type: integer }

  /orders/status:
    get:
      summary: Orders by status
      security:
        - bearerAuth: []
      responses:
        '200':
          description: Orders grouped by status
          content:
            application/json:
              schema:
                type: object
                properties:
                  pending: { type: integer }
                  processing: { type: integer }
                  shipped: { type: integer }
                  delivered: { type: integer }
                  cancelled: { type: integer }

  /orders/marketplace-breakdown:
    get:
      summary: Marketplace breakdown
      security:
        - bearerAuth: []
      responses:
        '200':
          description: Orders breakdown by marketplace
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  properties:
                    platform: { type: string }
                    orders: { type: integer }
                    percentage: { type: number }

  /orders/detailed-status:
    get:
      summary: Detailed order status table
      security:
        - bearerAuth: []
      responses:
        '200':
          description: Detailed order status by platform
          content:
            application/json:
              schema:
                type: object
                properties:
                  headers: { type: array, items: { type: string } }
                  data:
                    type: array
                    items:
                      type: object
                      properties:
                        platform: { type: string }
                        received: { type: integer }
                        separated: { type: integer }
                        labeled: { type: integer }
                        packed: { type: integer }
                        problem: { type: integer }

  /sales/monthly:
    get:
      summary: Monthly sales data
      security:
        - bearerAuth: []
      responses:
        '200':
          description: Monthly sales metrics
          content:
            application/json:
              schema:
                type: object
                properties:
                  monthly_total: { type: number }
                  monthly_average: { type: number }
                  monthly_expected: { type: number }
                  monthly_goal: { type: number }
                  progress_percentage: { type: number }

  /sales/chart-data:
    get:
      summary: Sales chart data
      security:
        - bearerAuth: []
      responses:
        '200':
          description: Daily sales data for charts
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  properties:
                    date: { type: string }
                    weekday: { type: string }
                    sales: { type: number }
                    goal: { type: number }
                    average: { type: number }

  /sales/platform-breakdown:
    get:
      summary: Platform breakdown for pie chart
      security:
        - bearerAuth: []
      responses:
        '200':
          description: Sales breakdown by platform
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  properties:
                    platform: { type: string }
                    sales: { type: number }
                    percentage: { type: number }

  /business/daily-sales/{date}:
    get:
      summary: Daily sales for date
      security:
        - bearerAuth: []
      parameters:
        - in: path
          name: date
          required: true
          schema: { type: string, format: date }
      responses:
        '200':
          description: Daily sales data with platform breakdown
          content:
            application/json:
              schema:
                type: object
                properties:
                  date: { type: string }
                  total_orders: { type: integer }
                  total_sales: { type: number }
                  average_ticket: { type: number }
                  platforms:
                    type: array
                    items:
                      type: object
                      properties:
                        platform: { type: string }
                        orders: { type: integer }
                        total_sales: { type: number }
                        average_ticket: { type: number }

  /business/sales-period:
    get:
      summary: Sales for date range (with 7-day averages)
      security:
        - bearerAuth: []
      parameters:
        - in: query
          name: dataIni
          schema: { type: string, format: date }
        - in: query
          name: dataFin
          schema: { type: string, format: date }
      responses:
        '200':
          description: Sales data for date range with 7-day averages
          content:
            application/json:
              schema:
                type: object
                properties:
                  summary:
                    type: object
                    properties:
                      total_sales: { type: number }
                      total_orders: { type: integer }
                      average_ticket: { type: number }
                  daily_sales:
                    type: array
                    items:
                      type: object
                      properties:
                        date: { type: string }
                        sales: { type: number }
                        average: { type: number }
                        goal: { type: number }

  /business/product-analysis:
    get:
      summary: Product analysis
      security:
        - bearerAuth: []
      parameters:
        - in: query
          name: dataIni
          schema: { type: string, format: date }
        - in: query
          name: dataFin
          schema: { type: string, format: date }
      responses:
        '200':
          description: Product performance analysis
          content:
            application/json:
              schema:
                type: object
                properties:
                  products:
                    type: array
                    items:
                      type: object
                      properties:
                        name: { type: string }
                        sku: { type: string }
                        total_sold: { type: integer }
                        total_revenue: { type: number }
                        average_price: { type: number }
                        margin_percentage: { type: number }

  /business/margin-analysis:
    get:
      summary: Margin analysis
      security:
        - bearerAuth: []
      parameters:
        - in: query
          name: dataIni
          schema: { type: string, format: date }
        - in: query
          name: dataFin
          schema: { type: string, format: date }
      responses:
        '200':
          description: Product margin analysis
          content:
            application/json:
              schema:
                type: object
                properties:
                  overall_margin: { type: number }
                  total_cost: { type: number }
                  total_revenue: { type: number }
                  products:
                    type: array
                    items:
                      type: object
                      properties:
                        product_name: { type: string }
                        cost_price: { type: number }
                        sale_price: { type: number }
                        margin: { type: number }
                        margin_percentage: { type: number }

  /advanced/xml-process:
    post:
      summary: Process uploaded XML
      security:
        - bearerAuth: []
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  type: string
                  format: binary
                  description: XML file to process
      responses:
        '200':
          description: Processed XML data
          content:
            application/json:
              schema:
                type: object
                properties:
                  success: { type: boolean }
                  processed_data: { type: array }
                  count: { type: integer }

  /advanced/barcode/generate:
    post:
      summary: Generate barcode image
      security:
        - bearerAuth: []
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                code: { type: string }
                type: { type: string, default: "CODE128" }
      responses:
        '200':
          description: Generated barcode image
          content:
            application/json:
              schema:
                type: object
                properties:
                  barcode_data: { type: string }
                  format: { type: string }
                  type: { type: string }

  /advanced/barcode/qrcode:
    post:
      summary: Generate QR code
      security:
        - bearerAuth: []
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                data: { type: string }
                size: { type: integer, default: 200 }
      responses:
        '200':
          description: Generated QR code image
          content:
            application/json:
              schema:
                type: object
                properties:
                  qrcode_data: { type: string }
                  size: { type: integer }

  /advanced/xml/list-files:
    get:
      summary: List uploaded XML files
      security:
        - bearerAuth: []
      responses:
        '200':
          description: List of XML files
          content:
            application/json:
              schema:
                type: object
                properties:
                  files: { type: array, items: { type: string } }
                  count: { type: integer }

  /export/daily-sales-csv:
    get:
      summary: Export daily sales CSV
      security:
        - bearerAuth: []
      parameters:
        - in: query
          name: date
          schema: { type: string, format: date }
      responses:
        '200':
          description: CSV file download
          content:
            text/csv:
              schema:
                type: string
                description: CSV file content

  /export/period-sales-csv:
    get:
      summary: Export period sales CSV
      security:
        - bearerAuth: []
      parameters:
        - in: query
          name: start_date
          schema: { type: string, format: date }
        - in: query
          name: end_date
          schema: { type: string, format: date }
      responses:
        '200':
          description: CSV file download
          content:
            text/csv:
              schema:
                type: string
                description: CSV file content

  /export/product-analysis-csv:
    get:
      summary: Export product analysis CSV
      security:
        - bearerAuth: []
      parameters:
        - in: query
          name: start_date
          schema: { type: string, format: date }
        - in: query
          name: end_date
          schema: { type: string, format: date }
      responses:
        '200':
          description: CSV file download
          content:
            text/csv:
              schema:
                type: string
                description: CSV file content

  # === NEW ENDPOINTS FROM PHP SLIM REPLICA ===

  /auth/profile:
    get:
      summary: Get user profile
      security:
        - bearerAuth: []
      responses:
        '200':
          description: User profile data
        '401':
          description: Unauthorized

  /auth/change-password:
    post:
      summary: Change user password
      security:
        - bearerAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                current_password: { type: string }
                new_password: { type: string }
              required: [current_password, new_password]
      responses:
        '200': { description: Password changed successfully }

  /dashboard/sales-performance:
    get:
      summary: Get sales performance data
      security:
        - bearerAuth: []
      responses:
        '200': { description: Sales performance metrics }

  /sales:
    post:
      summary: Create a new sale
      security:
        - bearerAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                customer_name: { type: string }
                total_amount: { type: string }
                items: { type: array, items: { type: object } }
      responses:
        '201': { description: Sale created successfully }

    get:
      summary: Get sales with filters
      security:
        - bearerAuth: []
      parameters:
        - in: query
          name: limit
          schema: { type: integer, default: 50 }
        - in: query
          name: offset
          schema: { type: integer, default: 0 }
        - in: query
          name: date_from
          schema: { type: string, format: date }
        - in: query
          name: date_to
          schema: { type: string, format: date }
      responses:
        '200': { description: Sales list with pagination }

  /sales/{sale_id}:
    get:
      summary: Get specific sale by ID
      security:
        - bearerAuth: []
      parameters:
        - in: path
          name: sale_id
          required: true
          schema: { type: integer }
      responses:
        '200': { description: Sale details }
        '404': { description: Sale not found }

  /products/extended:
    post:
      summary: Create extended product
      security:
        - bearerAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                sku: { type: string }
                name: { type: string }
                description: { type: string }
                cost_price: { type: string }
                sale_price: { type: string }
      responses:
        '201': { description: Extended product created }

    get:
      summary: Search products
      security:
        - bearerAuth: []
      parameters:
        - in: query
          name: q
          schema: { type: string }
        - in: query
          name: category
          schema: { type: string }
        - in: query
          name: platform
          schema: { type: string }
        - in: query
          name: limit
          schema: { type: integer, default: 50 }
      responses:
        '200': { description: Products matching search criteria }

  /products/categories:
    get:
      summary: Get product categories
      security:
        - bearerAuth: []
      responses:
        '200': { description: List of product categories }

  /products/attributes/linha:
    get:
      summary: Get linha attribute options
      security:
        - bearerAuth: []
      responses:
        '200': { description: List of linha options }

  /products/{product_id}/extended:
    get:
      summary: Get extended product by ID
      security:
        - bearerAuth: []
      parameters:
        - in: path
          name: product_id
          required: true
          schema: { type: integer }
      responses:
        '200': { description: Extended product details }
        '404': { description: Product not found }

  /upload/product-image:
    post:
      summary: Upload product image
      security:
        - bearerAuth: []
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                image:
                  type: string
                  format: binary
                  description: Product image file
      responses:
        '200': { description: Image uploaded successfully }

  /admin/users:
    get:
      summary: Get all users (admin only)
      security:
        - bearerAuth: []
        - adminAuth: []
      responses:
        '200': { description: List of all users }

  /admin/users/{user_id}:
    delete:
      summary: Delete user by ID (admin only)
      security:
        - bearerAuth: []
        - adminAuth: []
      parameters:
        - in: path
          name: user_id
          required: true
          schema: { type: integer }
      responses:
        '200': { description: User deleted successfully }
        '404': { description: User not found }

  /admin/system-status:
    get:
      summary: Get system status (admin only)
      security:
        - bearerAuth: []
        - adminAuth: []
      responses:
        '200': { description: System status information }

  /admin/audit-log:
    get:
      summary: Get audit log (admin only)
      security:
        - bearerAuth: []
        - adminAuth: []
      parameters:
        - in: query
          name: limit
          schema: { type: integer, default: 100 }
        - in: query
          name: offset
          schema: { type: integer, default: 0 }
      responses:
        '200': { description: Audit log entries }

  /notifications:
    get:
      summary: Get user notifications
      security:
        - bearerAuth: []
      responses:
        '200': { description: User notifications }

  /advanced/barcode/types:
    get:
      summary: Get available barcode types
      security:
        - bearerAuth: []
      responses:
        '200': { description: List of supported barcode types }

components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
    adminAuth:
      type: apiKey
      in: header
      name: X-Admin-Role
      description: Admin role required for admin endpoints

  schemas:
    User:
      type: object
      properties:
        id: { type: integer }
        username: { type: string }
        email: { type: string }
        role: { type: string }
        created_at: { type: string, format: date-time }
        last_login: { type: string, format: date-time }
        is_active: { type: boolean }

    Sale:
      type: object
      properties:
        id: { type: integer }
        order_number: { type: string }
        customer_name: { type: string }
        total_amount: { type: number }
        status: { type: string }
        platform: { type: string }
        created_at: { type: string, format: date-time }

    Product:
      type: object
      properties:
        id: { type: integer }
        sku: { type: string }
        name: { type: string }
        description: { type: string }
        cost_price: { type: number }
        sale_price: { type: number }
        stock_quantity: { type: integer }
        category: { type: string }
        is_active: { type: boolean }

    Order:
      type: object
      properties:
        id: { type: integer }
        order_number: { type: string }
        customer_name: { type: string }
        customer_email: { type: string }
        total_amount: { type: number }
        freight_amount: { type: number }
        platform: { type: string }
        status: { type: string, enum: [pending, processing, shipped, delivered, cancelled] }
        created_at: { type: string, format: date-time }
        updated_at: { type: string, format: date-time }

    OrderItem:
      type: object
      properties:
        id: { type: integer }
        order_id: { type: integer }
        product_id: { type: integer }
        quantity: { type: integer }
        unit_price: { type: number }
        total_price: { type: number }
        product_code: { type: string }
        product_size: { type: string }

    Notification:
      type: object
      properties:
        id: { type: integer }
        type: { type: string, enum: [order_update, system, sales_alert] }
        title: { type: string }
        message: { type: string }
        read: { type: boolean }
        created_at: { type: string, format: date-time }
        data: { type: object }

    Error:
      type: object
      properties:
        error: { type: string }
        message: { type: string }
        timestamp: { type: string, format: date-time }

    Success:
      type: object
      properties:
        message: { type: string }
        data: { type: object }
        timestamp: { type: string, format: date-time }


