import { Transform } from 'class-transformer';
import { IsInt, Min } from 'class-validator';
import { TransformNumber } from '../../../shared/decorators/class-transformer/transform-number.decorator';
import { TryToTransformStringInNumber } from '../../../shared/decorators/class-transformer/try-to-transform-string-in-number.decorator';

export class RetrieveRecipeParamsDto {
  @TransformNumber()
  @IsInt()
  @Min(1)
  id: number;
}

export class RetrieveRecipe_Tag {
  id: number;
  name: string;
}

export class RetrieveRecipe_Ingredient {
  id: number;
  name: string;
  quantity: number;
}

export class RetrieveRecipe_StepByStep {
  lexoRank: string;
  description: string;
}

export class RetrieveRecipe_Recipe {
  id: number;
  creator: string;
  name: string;

  @TryToTransformStringInNumber()
  kcal: number | string;

  servings: number;
  pictureUrl: string;

  @Transform(({ value }) => !!+value)
  isLiked?: boolean;

  @Transform(({ value }) => Number(value))
  likes: number;

  tags: RetrieveRecipe_Tag[];
  ingredients: RetrieveRecipe_Ingredient[];

  @Transform(({ value }) =>
    value.sort((a: RetrieveRecipe_StepByStep, b: RetrieveRecipe_StepByStep) => {
      return a.lexoRank.localeCompare(b.lexoRank);
    }),
  )
  stepBySteps: RetrieveRecipe_StepByStep[];

  constructor(partial: Partial<RetrieveRecipe_Recipe>) {
    Object.assign(this, partial);
  }
}

export class RetrieveRecipeResponseDto {
  data: RetrieveRecipe_Recipe;
}
