import { BaseDto } from 'src/core/modules/infrastructure/dto/base.dto';
import {
  AdditionalFormula,
  SalesPriceFormulaEntity,
  TransactionSetting,
} from '../../domain/entities/sales-price-formula.entity';
import { ApiProperty } from '@nestjs/swagger';
import { ValidateIf, ValidateNested } from 'class-validator';
import { Exclude } from 'class-transformer';
import { Any } from 'typeorm';
import { FormulaType } from '../../constants';

export class AdditionalFormulaDto implements AdditionalFormula {
  @ApiProperty({
    type: Any,
    required: false,
  })
  @ValidateIf((body) => body.formula_render)
  formula_render: any;

  @ApiProperty({
    type: String,
    required: false,
  })
  @ValidateIf((body) => body.formula_string)
  formula_string: string;

  @ApiProperty({
    type: String,
    required: false,
  })
  @ValidateIf((body) => body.value_for)
  value_for: string;
}

export class TransactionSettingDto implements TransactionSetting {
  @ApiProperty({
    type: String,
    required: true,
  })
  @ValidateIf((body) => body.id)
  id?: string;

  @ApiProperty({
    type: Number,
    required: true,
  })
  value: number;
}

export class SalesPriceFormulaDto
  extends BaseDto
  implements SalesPriceFormulaEntity
{
  @ApiProperty({
    type: Any,
    required: false,
  })
  @ValidateIf((body) => body.formula_render)
  formula_render: any;

  @ApiProperty({
    type: String,
    required: false,
  })
  @ValidateIf((body) => body.formula_string)
  formula_string: string;

  @Exclude()
  example_formula: string;

  @Exclude()
  example_result: number;

  @Exclude()
  value_for: string;

  @Exclude()
  type: FormulaType;

  @ApiProperty({
    type: [AdditionalFormulaDto],
    default: AdditionalFormulaDto,
  })
  @ValidateIf(({ additional }) => {
    return additional != null;
  })
  @ValidateNested({ each: true })
  additional: AdditionalFormulaDto[];
}
