import { prisma } from '../../lib/prisma'

export async function findMany(where: any, skip: number, take: number) {
  return Promise.all([
    prisma.vehicle.findMany({ where, skip, take, orderBy: { createdAt: 'desc' } }),
    prisma.vehicle.count({ where }),
  ])
}

export async function findById(id: string, companyId: string) {
  return prisma.vehicle.findFirst({ where: { id, companyId } })
}

export async function findFirst(id: string, companyId: string) {
  return prisma.vehicle.findFirst({ where: { id, companyId } })
}

export async function create(data: any) {
  return prisma.vehicle.create({ data })
}

export async function updateById(id: string, data: any) {
  return prisma.vehicle.update({ where: { id }, data })
}

export async function softDelete(id: string, companyId: string) {
  return prisma.vehicle.updateMany({ where: { id, companyId }, data: { status: 'OUT_OF_SERVICE', isPublished: false } })
}

export async function setPublished(id: string, companyId: string, isPublished: boolean) {
  return prisma.vehicle.updateMany({ where: { id, companyId }, data: { isPublished } })
}

export async function findReservationConflicts(vehicleId: string, companyId: string, start: Date, end: Date) {
  return prisma.reservation.findMany({
    where: {
      vehicleId,
      companyId,
      status: { in: ['CONFIRMED', 'ACTIVE'] },
      startDate: { lt: end },
      endDate:   { gt: start },
    },
    select: { id: true, startDate: true, endDate: true, status: true },
  })
}

export async function findCalendarBlockConflicts(vehicleId: string, start: Date, end: Date) {
  return prisma.vehicleCalendarBlock.findMany({
    where: { vehicleId, startDate: { lt: end }, endDate: { gt: start } },
    select: { id: true, startDate: true, endDate: true, type: true, reason: true },
  })
}

export async function findCalendarEvents(vehicleId: string, companyId: string, rangeStart: Date, rangeEnd: Date) {
  return Promise.all([
    prisma.reservation.findMany({
      where: {
        vehicleId,
        companyId,
        status: { in: ['DRAFT', 'CONFIRMED', 'ACTIVE'] },
        startDate: { lt: rangeEnd },
        endDate:   { gt: rangeStart },
      },
      select: {
        id: true, startDate: true, endDate: true, status: true,
        customer: { select: { firstName: true, lastName: true } },
      },
      orderBy: { startDate: 'asc' },
    }),
    prisma.vehicleCalendarBlock.findMany({
      where: { vehicleId, startDate: { lt: rangeEnd }, endDate: { gt: rangeStart } },
      orderBy: { startDate: 'asc' },
    }),
  ])
}

export async function createCalendarBlock(data: any) {
  return prisma.vehicleCalendarBlock.create({ data })
}

export async function deleteCalendarBlock(id: string, vehicleId: string) {
  return prisma.vehicleCalendarBlock.deleteMany({ where: { id, vehicleId } })
}

export async function findMaintenanceLogs(vehicleId: string) {
  return prisma.maintenanceLog.findMany({ where: { vehicleId }, orderBy: { performedAt: 'desc' } })
}

export async function createMaintenanceLog(data: any) {
  return prisma.maintenanceLog.create({ data })
}

export async function findPricingConfiguration(vehicleId: string) {
  return prisma.vehiclePricingConfiguration.findUnique({
    where: { vehicleId },
    include: {
      rules: { orderBy: [{ sortOrder: 'asc' }, { startDate: 'asc' }, { createdAt: 'asc' }] },
      history: { orderBy: { createdAt: 'desc' }, take: 20 },
    },
  })
}

export async function createPricingConfiguration(data: any) {
  return prisma.vehiclePricingConfiguration.create({
    data,
    include: {
      rules: { orderBy: [{ sortOrder: 'asc' }, { startDate: 'asc' }, { createdAt: 'asc' }] },
      history: { orderBy: { createdAt: 'desc' }, take: 20 },
    },
  })
}

export async function updatePricingConfiguration(id: string, data: any) {
  return prisma.vehiclePricingConfiguration.update({
    where: { id },
    data,
    include: {
      rules: { orderBy: [{ sortOrder: 'asc' }, { startDate: 'asc' }, { createdAt: 'asc' }] },
      history: { orderBy: { createdAt: 'desc' }, take: 20 },
    },
  })
}

export async function findPricingRule(id: string, configurationId: string) {
  return prisma.vehiclePricingRule.findFirst({ where: { id, configurationId } })
}

export async function createPricingRule(data: any) {
  return prisma.vehiclePricingRule.create({ data })
}

export async function updatePricingRule(id: string, data: any) {
  return prisma.vehiclePricingRule.update({ where: { id }, data })
}

export async function deletePricingRule(id: string, configurationId: string) {
  return prisma.vehiclePricingRule.deleteMany({ where: { id, configurationId } })
}

export async function createPriceHistory(data: any) {
  return prisma.vehiclePriceHistory.create({ data })
}
