import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { UpsertCostosOperacionCreditoDto } from './dto/costos-operacion-credito.dto';

@Injectable()
export class CostosOperacionCreditoService {
  constructor(private readonly prisma: PrismaService) {}

  async getOrCreate(empresa_id: string) {
    return this.prisma.costos_operacion_credito.upsert({
      where: { empresa_id },
      create: { empresa_id },
      update: {},
    });
  }

  async upsert(empresa_id: string, dto: UpsertCostosOperacionCreditoDto, usuario?: string) {
    return this.prisma.costos_operacion_credito.upsert({
      where: { empresa_id },
      create: {
        empresa_id,
        activo: dto.activo,
        comision_venta_pct: dto.comision_venta_pct,
        costo_cobranza_pct: dto.costo_cobranza_pct,
        costo_financiero_pct_mensual: dto.costo_financiero_pct_mensual,
        mora_esperada_pct: dto.mora_esperada_pct,
        usuario_actualizacion: usuario,
      },
      update: {
        activo: dto.activo,
        comision_venta_pct: dto.comision_venta_pct,
        costo_cobranza_pct: dto.costo_cobranza_pct,
        costo_financiero_pct_mensual: dto.costo_financiero_pct_mensual,
        mora_esperada_pct: dto.mora_esperada_pct,
        usuario_actualizacion: usuario,
        updated_at: new Date(),
      },
    });
  }
}
