import {
  IsString,
  IsEmail,
  IsOptional,
  IsBoolean,
  IsArray,
  IsEnum,
  IsInt,
  Min,
  Max,
  MinLength,
} from 'class-validator';

export enum GlobalRole {
  PARENT = 'parent',
  GUEST = 'guest',
  ADMIN = 'admin',
}

export class ListUsersQueryDto {
  @IsOptional()
  @IsInt()
  @Min(1)
  page?: number = 1;

  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(100)
  limit?: number = 20;

  @IsOptional()
  @IsString()
  search?: string;

  @IsOptional()
  @IsEnum(GlobalRole)
  role?: GlobalRole;

  @IsOptional()
  @IsBoolean()
  isAdmin?: boolean;
}

export class CreateUserDto {
  @IsEmail()
  email: string;

  @IsString()
  @MinLength(8)
  password: string;

  @IsString()
  name: string;

  @IsOptional()
  @IsString()
  phone?: string;

  @IsOptional()
  @IsEnum(GlobalRole)
  globalRole?: GlobalRole = GlobalRole.PARENT;

  @IsOptional()
  @IsBoolean()
  isAdmin?: boolean = false;

  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  adminPermissions?: string[] = [];
}

export class UpdateUserDto {
  @IsOptional()
  @IsString()
  name?: string;

  @IsOptional()
  @IsString()
  phone?: string;

  @IsOptional()
  @IsString()
  photoUrl?: string;

  @IsOptional()
  @IsEnum(GlobalRole)
  globalRole?: GlobalRole;

  @IsOptional()
  @IsBoolean()
  isAdmin?: boolean;

  @IsOptional()
  @IsArray()
  @IsString({ each: true })
  adminPermissions?: string[];

  @IsOptional()
  @IsBoolean()
  emailVerified?: boolean;
}

export class UserResponseDto {
  id: string;
  email: string;
  name: string;
  phone?: string;
  photoUrl?: string;
  globalRole: 'parent' | 'guest' | 'admin';
  isAdmin: boolean;
  adminPermissions: string[];
  emailVerified: boolean;
  createdAt: Date;
  updatedAt: Date;
  familyCount?: number;
  childrenCount?: number;
  deviceCount?: number;
}

export class PaginatedUsersResponseDto {
  users: UserResponseDto[];
  total: number;
  page: number;
  limit: number;
  totalPages: number;
}
