import {
  IsDate,
  IsEmail,
  IsEnum,
  IsInt,
  IsJWT,
  IsNotEmpty,
  IsNumber,
  IsString,
  Max,
  MaxLength,
  Min,
  ValidateIf,
} from 'class-validator';
import { TransformDate } from '../../../shared/decorators/class-transformer/transform-date.decorator';
import { IsUsername } from '../../../shared/decorators/class-validator/is-username.decorator';
import { MaxDate } from '../../../shared/decorators/class-validator/max-date.decorator';
import { MinDate } from '../../../shared/decorators/class-validator/min-date.decorator';
import { Gender } from '../../../shared/enums/user/gender.enum';
import { MatchesPersonNamesRegex } from '../../../shared/decorators/class-validator/matches-person-names-regex.decorator';
import { IsOptional } from '../../../shared/decorators/class-validator/is-optional.decorator';

export class CreateUserDto {
  @IsString()
  @IsNotEmpty()
  @MaxLength(80)
  @MatchesPersonNamesRegex()
  fullName: string;

  @IsString()
  @IsUsername()
  username: string;

  @IsString()
  @IsEmail()
  @MaxLength(60)
  email: string;

  @ValidateIf((obj: CreateUserDto, value: any) => {
    if (!obj.googleToken) return true;

    return typeof value !== 'undefined';
  })
  @IsString()
  @IsNotEmpty()
  @MaxLength(512)
  password: string;

  @IsNumber()
  @Min(1)
  @Max(9999.99)
  weight: number;

  @IsNumber()
  @Min(0.01)
  @Max(99.99)
  height: number;

  @TransformDate()
  @IsDate()
  @MinDate('1900-01-01')
  @MaxDate(() => new Date())
  birthDate: Date;

  @IsEnum(Gender)
  gender: Gender;

  @IsInt()
  @Min(1)
  goalId: number;

  @IsInt()
  @Min(1)
  physicalActivityId: number;

  @IsOptional()
  @IsJWT()
  googleToken?: string;
}

export class CreateUserResponseDto {
  data: {
    accessToken: string;
  };
}
