"use strict";
class PrismaAttributeRepository {
  constructor(prismaClient) {
    this.prismaClient = prismaClient;
  }
  async findManyByNamesAndOrgIdIncludeOptions({
    attributeNames,
    orgId
  }) {
    return this.prismaClient.attribute.findMany({
      where: {
        name: { in: attributeNames, mode: "insensitive" },
        teamId: orgId
      },
      include: {
        options: {
          select: {
            id: true,
            value: true,
            slug: true
          }
        }
      }
    });
  }
  async findManyByOrgId({ orgId }) {
    const result = await this.prismaClient.attribute.findMany({
      where: {
        teamId: orgId
      },
      select: {
        id: true,
        name: true,
        type: true,
        slug: true,
        options: true
      }
    });
    return result;
  }
  async findAllByOrgIdWithOptions({ orgId }) {
    return await this.prismaClient.attribute.findMany({
      where: {
        teamId: orgId
      },
      include: {
        options: true
      }
    });
  }
  async findUniqueWithWeights({
    teamId,
    attributeId,
    isWeightsEnabled = true
  }) {
    return await this.prismaClient.attribute.findUnique({
      where: {
        id: attributeId,
        teamId,
        isWeightsEnabled
      },
      select: {
        id: true,
        name: true,
        slug: true,
        type: true,
        options: {
          select: {
            id: true,
            value: true,
            slug: true,
            assignedUsers: {
              select: {
                member: {
                  select: {
                    userId: true
                  }
                },
                weight: true
              }
            }
          }
        }
      }
    });
  }
}
class HostRepository {
  constructor(prismaClient) {
    this.prismaClient = prismaClient;
  }
  async updateHostsSchedule(userId, oldScheduleId, newScheduleId) {
    return await this.prismaClient.host.updateMany({
      where: {
        userId,
        scheduleId: oldScheduleId
      },
      data: {
        scheduleId: newScheduleId
      }
    });
  }
  async findHostsCreatedInInterval({
    eventTypeId,
    userIds,
    startDate
  }) {
    return await this.prismaClient.host.findMany({
      where: {
        userId: {
          in: userIds
        },
        eventTypeId,
        isFixed: false,
        createdAt: {
          gte: startDate
        }
      }
    });
  }
}
class PrismaOOORepository {
  constructor(prismaClient) {
    this.prismaClient = prismaClient;
  }
  async findManyOOO({
    startTimeDate,
    endTimeDate,
    allUserIds
  }) {
    return this.prismaClient.outOfOfficeEntry.findMany({
      where: {
        userId: {
          in: allUserIds
        },
        OR: [
          // outside of range
          // (start <= 'dateTo' AND end >= 'dateFrom')
          {
            start: {
              lte: endTimeDate
            },
            end: {
              gte: startTimeDate
            }
          },
          // start is between dateFrom and dateTo but end is outside of range
          // (start <= 'dateTo' AND end >= 'dateTo')
          {
            start: {
              lte: endTimeDate
            },
            end: {
              gte: endTimeDate
            }
          },
          // end is between dateFrom and dateTo but start is outside of range
          // (start <= 'dateFrom' OR end <= 'dateTo')
          {
            start: {
              lte: startTimeDate
            },
            end: {
              lte: endTimeDate
            }
          }
        ]
      },
      select: {
        id: true,
        start: true,
        end: true,
        user: {
          select: {
            id: true,
            name: true
          }
        },
        toUser: {
          select: {
            id: true,
            username: true,
            name: true
          }
        },
        reason: {
          select: {
            id: true,
            emoji: true,
            reason: true
          }
        }
      }
    });
  }
  async findUserOOODays({ userId, dateTo, dateFrom }) {
    return this.prismaClient.outOfOfficeEntry.findMany({
      where: {
        userId,
        OR: [
          // outside of range
          // (start <= 'dateTo' AND end >= 'dateFrom')
          {
            start: {
              lte: dateTo
            },
            end: {
              gte: dateFrom
            }
          },
          // start is between dateFrom and dateTo but end is outside of range
          // (start <= 'dateTo' AND end >= 'dateTo')
          {
            start: {
              lte: dateTo
            },
            end: {
              gte: dateTo
            }
          },
          // end is between dateFrom and dateTo but start is outside of range
          // (start <= 'dateFrom' OR end <= 'dateTo')
          {
            start: {
              lte: dateFrom
            },
            end: {
              lte: dateTo
            }
          }
        ]
      },
      select: {
        id: true,
        start: true,
        end: true,
        user: {
          select: {
            id: true,
            name: true
          }
        },
        toUser: {
          select: {
            id: true,
            username: true,
            name: true
          }
        },
        reason: {
          select: {
            id: true,
            emoji: true,
            reason: true
          }
        }
      }
    });
  }
  async findOOOEntriesInInterval({
    userIds,
    startDate,
    endDate
  }) {
    return this.prismaClient.outOfOfficeEntry.findMany({
      where: {
        userId: {
          in: userIds
        },
        start: {
          lte: endDate
        },
        end: {
          gte: startDate
        }
      },
      select: {
        start: true,
        end: true,
        userId: true
      }
    });
  }
}
exports.HostRepository = HostRepository;
exports.PrismaAttributeRepository = PrismaAttributeRepository;
exports.PrismaOOORepository = PrismaOOORepository;
