import { TipoIVA } from '@win2win/shared';
import { Producto } from 'src/models';

const IVA_RATE = {
  [TipoIVA.GENERAL]: 0.21,
  [TipoIVA.REDUCIDO]: 0.1,
  [TipoIVA.SUPERREDUCIDO]: 0.04,
  [TipoIVA.EXENTO]: 0,
};

export class ProductPrice {
  private ivaRate: number;
  private netPrice: number;

  constructor(product: Partial<Pick<Producto, 'TIPO_IVA' | 'PRECIO'>>) {
    this.ivaRate = IVA_RATE[product.TIPO_IVA || TipoIVA.GENERAL];
    this.netPrice = Number(product.PRECIO || 0)
  }

  getNetPrice(): number {
    return this.roundToDecimals(this.netPrice);
  }

  getIVAPercent(): number {
    return this.ivaRate * 100;
  }

  getIVA(): number {
    return this.roundToDecimals(this.getNetPrice() * this.ivaRate);
  }

  getTotalPrice(): number {
    return this.getNetPrice() + this.getIVA();
  }

  getPVP(includeIVA: boolean = true): number {
    if (includeIVA) {
      return this.getTotalPrice();
    }
    return this.getNetPrice();
  }

  private roundToDecimals(value: number, decimals: number = 2): number {
    return Number(value.toFixed(decimals));
  }
}
