import { ApiProperty } from '@nestjs/swagger';
import { IsEmail, IsOptional, IsString, MinLength } from 'class-validator';

export class AuthenticatedUserDTO {
  @ApiProperty()
  userId!: string;
  @ApiProperty()
  email!: string;
  @ApiProperty()
  displayName!: string;
  @ApiProperty({ required: false })
  primaryDriverId?: string;
  @ApiProperty({ required: false, nullable: true })
  avatarUrl?: string | null;
  @ApiProperty({ required: false })
  companyId?: string;
  @ApiProperty({ required: false, enum: ['driver', 'sponsor', 'league-owner', 'league-steward', 'league-admin', 'system-owner', 'super-admin'] })
  role?: 'driver' | 'sponsor' | 'league-owner' | 'league-steward' | 'league-admin' | 'system-owner' | 'super-admin';
}

export class AuthSessionDTO {
  @ApiProperty()
  token!: string;
  @ApiProperty()
  user!: AuthenticatedUserDTO;
}

export class SignupParamsDTO {
  @ApiProperty()
  @IsEmail()
  email!: string;
  
  @ApiProperty()
  @IsString()
  @MinLength(8)
  password!: string;
  
  @ApiProperty()
  @IsString()
  @MinLength(2)
  displayName!: string;
  
  @ApiProperty({ required: false })
  @IsOptional()
  username?: string; // Ignored, for compatibility with tests
  
  @ApiProperty({ required: false })
  iracingCustomerId?: string;
  
  @ApiProperty({ required: false })
  primaryDriverId?: string;
  
  @ApiProperty({ required: false, nullable: true })
  avatarUrl?: string | null;
}

export class SignupSponsorParamsDTO {
  @ApiProperty()
  @IsEmail()
  email!: string;
  
  @ApiProperty()
  @IsString()
  @MinLength(8)
  password!: string;
  
  @ApiProperty()
  @IsString()
  @MinLength(2)
  displayName!: string;
  
  @ApiProperty()
  @IsString()
  @MinLength(2)
  companyName!: string;
}

export class LoginParamsDTO {
  @ApiProperty()
  @IsEmail()
  email!: string;
  
  @ApiProperty()
  @IsString()
  password!: string;
  
  @ApiProperty({ required: false, default: false })
  @IsOptional()
  rememberMe?: boolean;
}

export class IracingAuthRedirectResultDTO {
  @ApiProperty()
  redirectUrl!: string;
  @ApiProperty()
  state!: string;
}

export class LoginWithIracingCallbackParamsDTO {
  @ApiProperty()
  @IsString()
  code!: string;
  
  @ApiProperty()
  @IsString()
  state!: string;
  
  @ApiProperty({ required: false })
  @IsString()
  returnTo?: string;
}

export class ForgotPasswordDTO {
  @ApiProperty()
  @IsEmail()
  email!: string;
}

export class ResetPasswordDTO {
  @ApiProperty()
  @IsString()
  token!: string;

  @ApiProperty()
  @IsString()
  @MinLength(8)
  newPassword!: string;
}