import { prisma } from '../../lib/prisma'
import type { output } from 'zod'
import { renterUpdateSchema } from './auth.renter.schemas'

type RenterUpdateInput = output<typeof renterUpdateSchema>

export function findRenterProfile(id: string) {
  return prisma.renter.findUniqueOrThrow({
    where: { id },
    select: {
      id: true,
      firstName: true,
      lastName: true,
      email: true,
      phone: true,
      preferredLocale: true,
      preferredCurrency: true,
      emailVerified: true,
      savedCompanies: { select: { companyId: true } },
    },
  })
}

export function findSavedCompanies(companyIds: string[]) {
  return companyIds.length === 0
    ? Promise.resolve([])
    : prisma.company.findMany({
        where: { id: { in: companyIds } },
        select: {
          id: true,
          brand: {
            select: { displayName: true, subdomain: true, logoUrl: true },
          },
        },
      })
}

export function updateRenterProfile(id: string, data: RenterUpdateInput) {
  return prisma.renter.update({
    where: { id },
    data,
  })
}

export function updateRenterFcmToken(id: string, fcmToken: string) {
  return prisma.renter.update({
    where: { id },
    data: { fcmToken },
  })
}
