# ------------------------------------------------------------
# LIST ALL ASSIGNMENTS (admin/debug)
# ------------------------------------------------------------
query listAssignments(
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  assignments(offset: $offset, limit: $limit) {
    id
    title
    status
    createdAt

    workforce {
      id
      workforceNumber
      staff { id fullName }
    }

    shiftRole {
      id
      count
      assigned
      startTime
      endTime
      hours
      totalValue
      role { id name costPerHour }

      shift {
        id
        title
        date
        location
        locationAddress
        latitude
        longitude
        status

        order {
          id
          eventName
          business { id businessName email contactName }
          vendor { id companyName }
        }
      }
    }
  }
}

# ------------------------------------------------------------
# GET ASSIGNMENT BY ID
# ------------------------------------------------------------
query getAssignmentById($id: UUID!) @auth(level: USER) {
  assignment(id: $id) {
    id
    title
    description
    instructions
    status
    tipsAvailable
    travelTime
    mealProvided
    parkingAvailable
    gasCompensation
    managers
    createdAt
    updatedAt
    createdBy

    workforce {
      id
      workforceNumber
      status
      staff { id fullName }
    }

    shiftRole {
      id
      startTime
      endTime
      hours
      totalValue
      breakType
      uniform
      department
      role { id name costPerHour }

      shift {
        id
        title
        date
        location
        locationAddress
        latitude
        longitude
        status
        managers

        order {
          id
          eventName
          orderType
          business { id businessName email contactName }
          vendor { id companyName }
        }
      }
    }
  }
}

# ------------------------------------------------------------
# MY ASSIGNMENTS (by workforceId) - Staff view
# ------------------------------------------------------------
query listAssignmentsByWorkforceId(
  $workforceId: UUID!
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  assignments(
    where: { workforceId: { eq: $workforceId } }
    offset: $offset
    limit: $limit
    orderBy: { createdAt: DESC }
  ) {
    id
    title
    status
    createdAt

    workforce {
      id
      workforceNumber
      staff { id fullName }
    }

    shiftRole {
      id
      startTime
      endTime
      hours
      totalValue
      role { id name costPerHour }

      shift {
        id
        title
        date
        location
        status

        order {
          id
          eventName
          business { id businessName }
          vendor { id companyName }
        }
      }
    }
  }
}

# ------------------------------------------------------------
# ASSIGNMENTS FOR A VENDOR (Vendor dashboard)
# Approach: filter by workforce.vendorId (relation)
# If Data Connect can't filter nested, use 2-step:
#   1) listWorkforceByVendorId => workforce ids
#   2) assignments(where: { workforceId: { in: [...] } })
# ------------------------------------------------------------
query listAssignmentsByWorkforceIds(
  $workforceIds: [UUID!]!
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  assignments(
    where: { workforceId: { in: $workforceIds } }
    offset: $offset
    limit: $limit
    orderBy: { createdAt: DESC }
  ) {
    id
    title
    status
    createdAt

    workforce { id workforceNumber staff { id fullName } }

    shiftRole {
      id
      role { id name }
      shift {
        id
        title
        date
        order {
          id
          eventName
          business { id businessName }
          vendor { id companyName }
        }
      }
    }
  }
}

# ------------------------------------------------------------
# ASSIGNMENTS BY SHIFT ROLE (useful for staffing)
# ------------------------------------------------------------
query listAssignmentsByShiftRole(
  $shiftId: UUID!
  $roleId: UUID!
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  assignments(
    where: { 
      shiftId: { eq: $shiftId }
      roleId: { eq: $roleId }
    }
    offset: $offset
    limit: $limit
  ) {
    id
    title
    status
    createdAt
    workforce { id workforceNumber staff { id fullName } }
  }
}

# ------------------------------------------------------------

# FILTER ASSIGNMENTS (status + date range)
# Date range is based on Shift.date through the relation (NOT filterable directly).
#
# Since ShiftRole uses a composite key (shiftId + roleId),
# Assignments must be filtered using BOTH fields.
#
# So the filtering flow is:
#   1) Get Shifts in the date range => shiftIds
#   2) Get ShiftRoles where shiftId IN shiftIds => (shiftId, roleId) pairs
#   3) Get Assignments where:
#        - shiftId matches
#        - roleId matches
#        - status matches (optional)
#
# This query represents step 3.
# ------------------------------------------------------------
query filterAssignments(
  $shiftIds: [UUID!]!
  $roleIds: [UUID!]!
  $status: AssignmentStatus
  $offset: Int
  $limit: Int
) @auth(level: USER) {
  assignments(
    where: {
      shiftId: { in: $shiftIds }
      roleId: { in: $roleIds }
      status: { eq: $status }
    }
    offset: $offset
    limit: $limit
  ) {
    id
    title
    status
    createdAt

    workforce { id workforceNumber staff { id fullName } }

    shiftRole {
      id
      role { id name }
      shift { id title date location status }
    }
  }
}
