collections:
  # ==================== DEPARTMENTS ====================
  - collection: departments
    meta:
      icon: domain
      display_template: "{{name}}"
      sort: 1
      note: "Company departments that can be managed through the application"
    schema:
      name: departments
    fields:
      - field: id
        type: string
        schema:
          is_primary_key: true
          has_auto_increment: true
        meta:
          hidden: true
      - field: name
        type: string
        schema:
          is_nullable: false
          max_length: 100
          is_unique: true
        meta:
          interface: input
          required: true
          sort: 1
      - field: active
        type: boolean
        schema:
          default_value: true
          is_nullable: false
        meta:
          interface: boolean
          sort: 2
      - field: description
        type: text
        schema:
          is_nullable: true
        meta:
          interface: input-multiline
          sort: 3
      - field: created_at
        type: timestamp
        schema:
          default_value: "$NOW"
          is_nullable: false
        meta:
          interface: datetime
          sort: 4
          readonly: true

  # ==================== EMPLOYEES ====================
  - collection: employees
    meta:
      icon: person
      display_template: "{{name}}"
      sort: 2
      note: "Company employees for resource planning"
    schema:
      name: employees
    fields:
      - field: id
        type: string
        schema:
          is_primary_key: true
          has_auto_increment: true
        meta:
          hidden: true
      - field: name
        type: string
        schema:
          is_nullable: false
          max_length: 255
        meta:
          interface: input
          required: true
          sort: 1
      - field: department_id
        type: string
        schema:
          is_nullable: false
          foreign_key_table: departments
          foreign_key_column: id
        meta:
          interface: select-dropdown-m2o
          required: true
          sort: 2
          display: related-values
          display_options:
            template: "{{name}}"
      - field: active
        type: boolean
        schema:
          default_value: true
          is_nullable: false
        meta:
          interface: boolean
          sort: 3
      - field: email
        type: string
        schema:
          is_nullable: true
          max_length: 255
          is_unique: true
        meta:
          interface: input
          sort: 4
          options:
            iconLeft: email
      - field: position
        type: string
        schema:
          is_nullable: true
          max_length: 255
        meta:
          interface: input
          sort: 5
      - field: hire_date
        type: date
        schema:
          is_nullable: true
        meta:
          interface: datetime
          sort: 6

  # ==================== PROJECTS ====================
  - collection: projects
    meta:
      icon: work
      display_template: "{{name}}"
      sort: 3
      note: "Projects that need resource allocation"
    schema:
      name: projects
    fields:
      - field: id
        type: string
        schema:
          is_primary_key: true
          has_auto_increment: true
        meta:
          hidden: true
      - field: name
        type: string
        schema:
          is_nullable: false
          max_length: 255
        meta:
          interface: input
          required: true
          sort: 1
      - field: color
        type: string
        schema:
          default_value: "#3b82f6"
          max_length: 7
        meta:
          interface: select-color
          sort: 2
      - field: active
        type: boolean
        schema:
          default_value: true
          is_nullable: false
        meta:
          interface: boolean
          sort: 3
      - field: milestone_date
        type: date
        schema:
          is_nullable: true
        meta:
          interface: datetime
          sort: 4
      - field: description
        type: text
        schema:
          is_nullable: true
        meta:
          interface: input-multiline
          sort: 5
      - field: start_date
        type: date
        schema:
          is_nullable: true
        meta:
          interface: datetime
          sort: 6
      - field: end_date
        type: date
        schema:
          is_nullable: true
        meta:
          interface: datetime
          sort: 7
      - field: budget
        type: float
        schema:
          is_nullable: true
        meta:
          interface: numeric
          sort: 8

  # ==================== BLOCKS ====================
  - collection: blocks
    meta:
      icon: calendar_month
      display_template: "{{name}} ({{week_start}})"
      sort: 4
      note: "Work blocks for resource planning with multiple employees and projects"
      sort_field: week_start
    schema:
      name: blocks
    fields:
      - field: id
        type: string
        schema:
          is_primary_key: true
          has_auto_increment: true
        meta:
          hidden: true
      - field: name
        type: string
        schema:
          is_nullable: false
          max_length: 255
        meta:
          interface: input
          required: true
          sort: 1
      - field: week_start
        type: date
        schema:
          is_nullable: false
        meta:
          interface: datetime
          required: true
          sort: 2
      - field: is_milestone
        type: boolean
        schema:
          default_value: false
          is_nullable: true
        meta:
          interface: boolean
          sort: 3
      - field: employee_ids
        type: text
        schema:
          is_nullable: true
        meta:
          interface: input-code
          sort: 4
          width: full
          options:
            language: json
          note: "JSON array of employee IDs"
      - field: project_ids
        type: text
        schema:
          is_nullable: true
        meta:
          interface: input-code
          sort: 5
          width: full
          options:
            language: json
          note: "JSON array of project IDs"
      - field: allocations
        type: text
        schema:
          is_nullable: true
        meta:
          interface: input-code
          sort: 6
          width: full
          options:
            language: json
          note: "JSON object with project IDs as keys and percentages as values"
      - field: notes
        type: text
        schema:
          is_nullable: true
        meta:
          interface: input-multiline
          sort: 7
      - field: created_at
        type: timestamp
        schema:
          default_value: "$NOW"
          is_nullable: false
        meta:
          interface: datetime
          sort: 8
          readonly: true
      - field: updated_at
        type: timestamp
        schema:
          default_value: "$NOW"
          is_nullable: false
        meta:
          interface: datetime
          sort: 9
          readonly: true

  # ==================== APP_USERS (Authentication) ====================
  - collection: app_users
    meta:
      icon: person
      display_template: "{{email}}"
      sort: 5
      note: "Application users for authentication"
      singleton: false
    schema:
      name: app_users
    fields:
      - field: id
        type: string
        schema:
          is_primary_key: true
          has_auto_increment: true
        meta:
          hidden: true
      - field: email
        type: string
        schema:
          is_nullable: false
          max_length: 255
          is_unique: true
        meta:
          interface: input
          required: true
          sort: 1
          options:
            iconLeft: email
      - field: password
        type: string
        schema:
          is_nullable: false
        meta:
          interface: password
          required: true
          sort: 2
          hidden: true
      - field: role
        type: string
        schema:
          is_nullable: false
          default_value: "user"
          max_length: 50
        meta:
          interface: select-dropdown
          sort: 3
          options:
            choices:
              - text: User
                value: user
              - text: Admin
                value: admin
      - field: active
        type: boolean
        schema:
          default_value: true
          is_nullable: false
        meta:
          interface: boolean
          sort: 4
      - field: last_login
        type: timestamp
        schema:
          is_nullable: true
        meta:
          interface: datetime
          sort: 5
          readonly: true
      - field: created_at
        type: timestamp
        schema:
          default_value: "$NOW"
          is_nullable: false
        meta:
          interface: datetime
          sort: 6
          readonly: true

  # ==================== USER_SESSIONS ====================
  - collection: user_sessions
    meta:
      icon: schedule
      display_template: "Session for {{app_users_id.email}}"
      sort: 6
      note: "User authentication sessions"
      archive_field: expires_at
      archive_app_filter: true
    schema:
      name: user_sessions
    fields:
      - field: id
        type: string
        schema:
          is_primary_key: true
          has_auto_increment: true
        meta:
          hidden: true
      - field: app_users_id
        type: string
        schema:
          is_nullable: false
          foreign_key_table: app_users
          foreign_key_column: id
        meta:
          interface: select-dropdown-m2o
          required: true
          sort: 1
          display: related-values
          display_options:
            template: "{{email}}"
      - field: token
        type: string
        schema:
          is_nullable: false
          max_length: 255
          is_unique: true
        meta:
          interface: input
          required: true
          sort: 2
          hidden: true
      - field: expires_at
        type: timestamp
        schema:
          is_nullable: false
        meta:
          interface: datetime
          required: true
          sort: 3
      - field: created_at
        type: timestamp
        schema:
          default_value: "$NOW"
          is_nullable: false
        meta:
          interface: datetime
          sort: 4
          readonly: true
      - field: ip_address
        type: string
        schema:
          is_nullable: true
          max_length: 45
        meta:
          interface: input
          sort: 5
          readonly: true
      - field: user_agent
        type: text
        schema:
          is_nullable: true
        meta:
          interface: input-multiline
          sort: 6
          readonly: true

# ==================== RELATIONS ====================
relations:
  # Departments to Employees
  - collection: departments
    field: id
    related_collection: employees
    meta:
      one_field: department
    schema:
      on_delete: RESTRICT

  # User Sessions to App Users
  - collection: user_sessions
    field: app_users_id
    related_collection: app_users
    meta:
      one_field: sessions
    schema:
      on_delete: CASCADE
