import prisma from '../lib/prisma.js';

export async function findRideById(id: string) {
  return prisma.rides.findUnique({ where: { id } });
}

export async function createRide(data: {
  id: string;
  user_id: string;
  company: string;
  km: any;
  cost_per_km: any;
  total: any;
  ride_date: string;
  synced?: number;
}) {
  return prisma.rides.create({ data });
}

export async function updateRide(
  id: string,
  data: Partial<{
    user_id: string;
    company: string;
    km: any;
    cost_per_km: any;
    total: any;
    ride_date: string;
    synced: number;
  }>
) {
  return prisma.rides.update({ where: { id }, data });
}

export async function deleteRide(id: string) {
  return prisma.rides.delete({ where: { id } });
}

export async function listRidesByUserId(user_id: string) {
  return prisma.rides.findMany({ where: { user_id } });
}

export async function listAllRides() {
  return prisma.rides.findMany();
}

export async function listRidesNotSynced(user_id: string) {
  return prisma.rides.findMany({
    where: {
      user_id,
      synced: 0,
    },
  });
}

export async function markRideAsSynced(id: string) {
  return prisma.rides.update({
    where: { id },
    data: { synced: 1 },
  });
}

export async function upsertRide(data: {
  id: string;
  user_id: string;
  company: string;
  km: any;
  cost_per_km: any;
  total: any;
  ride_date: string;
  synced?: number;
}) {
  return prisma.rides.upsert({
    where: { id: data.id },
    update: {
      user_id: data.user_id,
      company: data.company,
      km: data.km,
      cost_per_km: data.cost_per_km,
      total: data.total,
      ride_date: data.ride_date,
      synced: data.synced,
    },
    create: data,
  });
}
