import { z } from 'zod';

/**
 * EN: Identity Management DTOs
 * VI: DTOs cho Quản Lý Danh Tính
 */

// =============================================================================
// Organization DTOs
// =============================================================================

export const CreateOrganizationDto = z.object({
  name: z.string().min(1, 'Organization name is required / Tên tổ chức là bắt buộc').max(255),
  domain: z.string().email().optional().or(z.string().min(1).max(255).optional()),
  parentId: z.string().optional(),
  settings: z.record(z.string(), z.any()).optional(),
});

export type CreateOrganizationDto = z.infer<typeof CreateOrganizationDto>;

export const UpdateOrganizationDto = z.object({
  name: z.string().min(1).max(255).optional(),
  domain: z.string().email().optional().or(z.string().min(1).max(255).optional()),
  parentId: z.string().nullable().optional(),
  settings: z.record(z.string(), z.any()).optional(),
  isActive: z.boolean().optional(),
});

export type UpdateOrganizationDto = z.infer<typeof UpdateOrganizationDto>;

// =============================================================================
// Group DTOs
// =============================================================================

export const CreateGroupDto = z.object({
  name: z.string().min(1, 'Group name is required / Tên nhóm là bắt buộc').max(255),
  organizationId: z.string().optional(),
  description: z.string().max(1000).optional(),
});

export type CreateGroupDto = z.infer<typeof CreateGroupDto>;

export const UpdateGroupDto = z.object({
  name: z.string().min(1).max(255).optional(),
  description: z.string().max(1000).optional(),
  isActive: z.boolean().optional(),
});

export type UpdateGroupDto = z.infer<typeof UpdateGroupDto>;

export const AddGroupMemberDto = z.object({
  userId: z.string().min(1, 'User ID is required / User ID là bắt buộc'),
  role: z.enum(['member', 'admin']).default('member'),
  expiresAt: z.string().datetime().optional(),
});

export type AddGroupMemberDto = z.infer<typeof AddGroupMemberDto>;

// =============================================================================
// User Profile DTOs
// =============================================================================

export const CreateUserProfileDto = z.object({
  firstName: z.string().max(255).optional(),
  lastName: z.string().max(255).optional(),
  phone: z.string().max(20).optional(),
  avatarUrl: z.string().url().optional(),
  customFields: z.record(z.string(), z.any()).optional(),
  preferences: z.record(z.string(), z.any()).optional(),
  metadata: z.record(z.string(), z.any()).optional(),
});

export type CreateUserProfileDto = z.infer<typeof CreateUserProfileDto>;

export const UpdateUserProfileDto = z.object({
  firstName: z.string().max(255).optional(),
  lastName: z.string().max(255).optional(),
  phone: z.string().max(20).optional(),
  avatarUrl: z.string().url().optional(),
  customFields: z.record(z.string(), z.any()).optional(),
  preferences: z.record(z.string(), z.any()).optional(),
  metadata: z.record(z.string(), z.any()).optional(),
});

export type UpdateUserProfileDto = z.infer<typeof UpdateUserProfileDto>;

// =============================================================================
// Identity Verification DTOs
// =============================================================================

export const VerificationRequestDto = z.object({
  type: z.enum(['EMAIL', 'PHONE', 'DOCUMENT', 'BIOMETRIC']),
  method: z.string().optional(),
  metadata: z.record(z.string(), z.any()).optional(),
});

export type VerificationRequestDto = z.infer<typeof VerificationRequestDto>;

export const VerifyCodeDto = z.object({
  token: z.string().min(1, 'Verification token is required / Token xác thực là bắt buộc'),
  code: z.string().min(1, 'Verification code is required / Mã xác thực là bắt buộc').optional(),
});

export type VerifyCodeDto = z.infer<typeof VerifyCodeDto>;

export const VerifyEmailDto = z.object({
  token: z.string().min(1, 'Verification token is required / Token xác thực là bắt buộc'),
});

export type VerifyEmailDto = z.infer<typeof VerifyEmailDto>;

export const VerifyPhoneDto = z.object({
  token: z.string().min(1, 'Verification token is required / Token xác thực là bắt buộc'),
  code: z.string().min(4, 'Verification code must be at least 4 characters / Mã xác thực phải có ít nhất 4 ký tự'),
});

export type VerifyPhoneDto = z.infer<typeof VerifyPhoneDto>;

// =============================================================================
// User Management DTOs
// =============================================================================

export const UpdateUserDto = z.object({
  username: z.string().min(3).max(50).optional(),
  email: z.string().email().optional(),
  isActive: z.boolean().optional(),
  organizationId: z.string().nullable().optional(),
});

export type UpdateUserDto = z.infer<typeof UpdateUserDto>;

export const BulkImportUsersDto = z.object({
  organizationId: z.string().optional(),
  users: z.array(z.object({
    email: z.string().email(),
    username: z.string().optional(),
    firstName: z.string().optional(),
    lastName: z.string().optional(),
    phone: z.string().optional(),
  })),
});

export type BulkImportUsersDto = z.infer<typeof BulkImportUsersDto>;

export const UserFiltersDto = z.object({
  organizationId: z.string().optional(),
  isActive: z.boolean().optional(),
  emailVerified: z.boolean().optional(),
  search: z.string().optional(),
  skip: z.number().int().min(0).default(0),
  take: z.number().int().min(1).max(100).default(20),
});

export type UserFiltersDto = z.infer<typeof UserFiltersDto>;
