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

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

export async function createCompany(data: {
  id: string;
  name: string;
  cost_per_km: any;
  notes?: string;
}) {
  return prisma.companies.create({ data });
}

export async function updateCompany(
  id: string,
  data: Partial<{
    name: string;
    cost_per_km: any;
    notes: string;
  }>
) {
  return prisma.companies.update({ where: { id }, data });
}

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

export async function listCompanies() {
  return prisma.companies.findMany();
}

export async function upsertCompany(data: {
  id: string;
  name: string;
  cost_per_km: any;
  notes?: string;
}) {
  return prisma.companies.upsert({
    where: { id: data.id },
    update: {
      name: data.name,
      cost_per_km: data.cost_per_km,
      notes: data.notes,
    },
    create: data,
  });
}
