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

export enum InvitationRole {
  MANAGER = 'MANAGER',
  USER = 'USER',
  VIEWER = 'VIEWER',
}

/**
 * Create Invitation DTO
 */
export class CreateInvitationDto {
  @ApiProperty({
    example: 'jane.doe@acme.com',
    description: 'Email address of the person to invite',
  })
  @IsEmail({}, { message: 'Invalid email format' })
  email: string;

  @ApiProperty({
    example: 'Jane',
    description: 'First name',
    minLength: 2,
  })
  @IsString()
  @MinLength(2, { message: 'First name must be at least 2 characters' })
  firstName: string;

  @ApiProperty({
    example: 'Doe',
    description: 'Last name',
    minLength: 2,
  })
  @IsString()
  @MinLength(2, { message: 'Last name must be at least 2 characters' })
  lastName: string;

  @ApiProperty({
    example: InvitationRole.USER,
    description: 'Role to assign to the invited user',
    enum: InvitationRole,
  })
  @IsEnum(InvitationRole)
  role: InvitationRole;
}

/**
 * Invitation Response DTO
 */
export class InvitationResponseDto {
  @ApiProperty({
    example: '550e8400-e29b-41d4-a716-446655440000',
    description: 'Invitation ID',
  })
  id: string;

  @ApiProperty({
    example: 'abc123def456',
    description: 'Invitation token',
  })
  token: string;

  @ApiProperty({
    example: 'jane.doe@acme.com',
    description: 'Email address',
  })
  email: string;

  @ApiProperty({
    example: 'Jane',
    description: 'First name',
  })
  firstName: string;

  @ApiProperty({
    example: 'Doe',
    description: 'Last name',
  })
  lastName: string;

  @ApiProperty({
    example: InvitationRole.USER,
    description: 'Role',
    enum: InvitationRole,
  })
  role: string;

  @ApiProperty({
    example: '550e8400-e29b-41d4-a716-446655440000',
    description: 'Organization ID',
  })
  organizationId: string;

  @ApiProperty({
    example: '2025-12-01T00:00:00Z',
    description: 'Expiration date',
  })
  expiresAt: Date;

  @ApiProperty({
    example: false,
    description: 'Whether the invitation has been used',
  })
  isUsed: boolean;

  @ApiPropertyOptional({
    example: '2025-11-24T10:00:00Z',
    description: 'Date when invitation was used',
  })
  usedAt?: Date;

  @ApiProperty({
    example: '2025-11-20T10:00:00Z',
    description: 'Creation date',
  })
  createdAt: Date;
}

/**
 * Verify Invitation DTO
 */
export class VerifyInvitationDto {
  @ApiProperty({
    example: 'abc123def456',
    description: 'Invitation token',
  })
  @IsString()
  token: string;
}

/**
 * Accept Invitation DTO (for registration)
 */
export class AcceptInvitationDto {
  @ApiProperty({
    example: 'abc123def456',
    description: 'Invitation token',
  })
  @IsString()
  token: string;

  @ApiProperty({
    example: 'SecurePassword123!',
    description: 'Password (minimum 12 characters)',
    minLength: 12,
  })
  @IsString()
  @MinLength(12, { message: 'Password must be at least 12 characters' })
  password: string;

  @ApiPropertyOptional({
    example: '+33612345678',
    description: 'Phone number (optional)',
  })
  @IsString()
  @IsOptional()
  phoneNumber?: string;
}
