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

/**
 * User roles enum
 */
export enum UserRole {
  ADMIN = 'ADMIN',
  MANAGER = 'MANAGER',
  USER = 'USER',
  VIEWER = 'VIEWER',
}

/**
 * Create User DTO (for admin/manager inviting users)
 */
export class CreateUserDto {
  @ApiProperty({
    example: 'jane.doe@acme.com',
    description: 'User email address',
  })
  @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: UserRole.USER,
    description: 'User role',
    enum: UserRole,
  })
  @IsEnum(UserRole)
  role: UserRole;

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

  @ApiPropertyOptional({
    example: 'TempPassword123!',
    description:
      'Temporary password (min 12 characters). If not provided, a random one will be generated.',
    minLength: 12,
  })
  @IsString()
  @IsOptional()
  @MinLength(12, { message: 'Password must be at least 12 characters' })
  password?: string;
}

/**
 * Update User DTO
 */
export class UpdateUserDto {
  @ApiPropertyOptional({
    example: 'Jane',
    description: 'First name',
    minLength: 2,
  })
  @IsString()
  @IsOptional()
  @MinLength(2)
  firstName?: string;

  @ApiPropertyOptional({
    example: 'Doe',
    description: 'Last name',
    minLength: 2,
  })
  @IsString()
  @IsOptional()
  @MinLength(2)
  lastName?: string;

  @ApiPropertyOptional({
    example: UserRole.MANAGER,
    description: 'User role',
    enum: UserRole,
  })
  @IsEnum(UserRole)
  @IsOptional()
  role?: UserRole;

  @ApiPropertyOptional({
    example: true,
    description: 'Active status',
  })
  @IsBoolean()
  @IsOptional()
  isActive?: boolean;
}

/**
 * Update Password DTO
 */
export class UpdatePasswordDto {
  @ApiProperty({
    example: 'OldPassword123!',
    description: 'Current password',
  })
  @IsString()
  @IsNotEmpty()
  currentPassword: string;

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

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

  @ApiProperty({
    example: 'john.doe@acme.com',
    description: 'User email',
  })
  email: string;

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

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

  @ApiProperty({
    example: UserRole.USER,
    description: 'User role',
    enum: UserRole,
  })
  role: UserRole;

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

  @ApiProperty({
    example: true,
    description: 'Active status',
  })
  isActive: boolean;

  @ApiProperty({
    example: '2025-01-01T00:00:00Z',
    description: 'Creation timestamp',
  })
  createdAt: Date;

  @ApiProperty({
    example: '2025-01-15T10:00:00Z',
    description: 'Last update timestamp',
  })
  updatedAt: Date;
}

/**
 * User List Response DTO
 */
export class UserListResponseDto {
  @ApiProperty({
    description: 'List of users',
    type: [UserResponseDto],
  })
  users: UserResponseDto[];

  @ApiProperty({
    example: 15,
    description: 'Total number of users',
  })
  total: number;

  @ApiProperty({
    example: 1,
    description: 'Current page number',
  })
  page: number;

  @ApiProperty({
    example: 20,
    description: 'Page size',
  })
  pageSize: number;

  @ApiProperty({
    example: 1,
    description: 'Total number of pages',
  })
  totalPages: number;
}
