// === ENUMS COHÉRENTS ===
export enum UserType {
  HUB = 'HUB',
  MERCHANT_PARTNER = 'MERCHANT'
}

export enum UserRole {
  // Rôles Hub (sans merchantPartnerId)
  DCB_ADMIN = 'dcb-admin',
  DCB_SUPPORT = 'dcb-support',
  DCB_PARTNER = 'dcb-partner',
  
  // Rôles Merchant Partner (avec merchantPartnerId obligatoire)
  DCB_PARTNER_ADMIN = 'dcb-partner-admin',
  DCB_PARTNER_MANAGER = 'dcb-partner-manager',
  DCB_PARTNER_SUPPORT = 'dcb-partner-support'
}

// Enum pour le contexte Angular (identique à l'ancien)
export enum UserContext {
  HUB = 'HUB',
  MERCHANT = 'MERCHANT'
}

// Ajoutez cette interface dans vos modèles
export interface GlobalUsersOverview {
  hubUsers: User[];
  merchantUsers: User[];
  statistics: {
    totalHubUsers: number;
    totalMerchantUsers: number;
    totalUsers: number;
  };
}

export interface UsersStatistics {
  totalHubUsers: number;
  totalMerchantUsers: number;
  totalUsers: number;
}

// === MODÈLE USER PRINCIPAL ===
export interface User {
  id: string;
  username: string;
  email: string;
  firstName: string;
  lastName: string;
  enabled: boolean;
  emailVerified: boolean;
  userType: UserType; // HUB ou MERCHANT
  merchantPartnerId?: string;
  role: UserRole;
  createdBy?: string;
  createdByUsername?: string;
  createdTimestamp: number;
  lastLogin?: number;
  profileImage?: string | null
}

// === DTOs CRUD ===
export interface CreateUserDto {
  username: string;
  email: string;
  firstName: string;
  lastName: string;
  password: string;
  userType: UserType;
  role: UserRole;
  enabled?: boolean;
  emailVerified?: boolean;
  merchantPartnerId?: string;
}

export interface UpdateUserDto {
  firstName?: string;
  lastName?: string;
  email?: string;
  enabled?: boolean;
  role?: UserRole;
}

export interface ResetPasswordDto {
  userId?: string;
  newPassword: string;
  temporary?: boolean;
}

// === PAGINATION / STATS ===
export interface PaginatedUserResponse {
  users: User[];
  total: number;
  page: number;
  limit: number;
  totalPages: number;
}

export interface MerchantPartnerStatsResponse {
  totalAdmins: number;
  totalManagers: number;
  totalSupport: number;
  totalUsers: number;
  activeUsers: number;
  inactiveUsers: number;
}

// === ROLES ===
export interface AvailableRole {
  value: UserRole;
  label: string;
  description: string;
  allowedForCreation: boolean;
  userType: UserType;
}

export interface AvailableRolesResponse {
  roles: AvailableRole[];
}

export interface RoleOperationResponse {
  message: string;
  success: boolean;
}

// === SEARCH ===
export interface SearchUsersParams {
  query?: string;
  role?: UserRole;
  enabled?: boolean;
  userType?: UserType;
  merchantPartnerId?: string;
  page?: number;
  limit?: number;
}

// === UTILITAIRES ===
export class UserUtils {
  static isHubUser(user: User): boolean {
    return user.userType === UserType.HUB;
  }

  static isMerchantPartnerUser(user: User): boolean {
    return user.userType === UserType.MERCHANT_PARTNER;
  }

  static hasRole(user: User, role: UserRole): boolean {
    if (!user.role) return false;
    return user.role.includes(role);
  }

  static getRoleDisplayName(role: UserRole): string {
    const roleNames = {
      [UserRole.DCB_ADMIN]: 'DCB Admin',
      [UserRole.DCB_SUPPORT]: 'DCB Support',
      [UserRole.DCB_PARTNER]: 'DCB Partner',
      [UserRole.DCB_PARTNER_ADMIN]: 'Partner Admin',
      [UserRole.DCB_PARTNER_MANAGER]: 'Partner Manager',
      [UserRole.DCB_PARTNER_SUPPORT]: 'Partner Support'
    };
    return roleNames[role] || role;
  }

  static getUserTypeDisplayName(userType: UserType): string {
    const typeNames = {
      [UserType.HUB]: 'Hub',
      [UserType.MERCHANT_PARTNER]: 'Merchant Partner'
    };
    return typeNames[userType] || userType;
  }

  // Méthode pour convertir UserContext (Angular) en UserType (API)
  static contextToUserType(context: UserContext): UserType {
    return context === UserContext.HUB ? UserType.HUB : UserType.MERCHANT_PARTNER;
  }

  // Méthode pour convertir UserType (API) en UserContext (Angular)
  static userTypeToContext(userType: UserType): UserContext {
    return userType === UserType.HUB ? UserContext.HUB : UserContext.MERCHANT;
  }

  static validateUserCreation(user: CreateUserDto): string[] {
    const errors: string[] = [];
    
    // Validation merchantPartnerId
    if (user.userType === UserType.MERCHANT_PARTNER && !user.merchantPartnerId) {
      errors.push('merchantPartnerId est obligatoire pour les utilisateurs Merchant Partner');
    }
    
    if (user.userType === UserType.HUB && user.merchantPartnerId) {
      errors.push('merchantPartnerId ne doit pas être défini pour les utilisateurs Hub');
    }
    
    if (!user.role) {
      errors.push('Un rôle doit être assigné');
    }
    
    // Validation cohérence rôle/type
    const hubRoles = [UserRole.DCB_ADMIN, UserRole.DCB_SUPPORT, UserRole.DCB_PARTNER];
    const merchantRoles = [UserRole.DCB_PARTNER_ADMIN, UserRole.DCB_PARTNER_MANAGER, UserRole.DCB_PARTNER_SUPPORT];
    
    if (user.userType === UserType.HUB && user.role) {
      if (!hubRoles.includes(user.role)) {
        errors.push(`Rôle invalide pour un utilisateur Hub: ${user.role}`);
      }
    }
    
    if (user.userType === UserType.MERCHANT_PARTNER && user.role) {
      if (!merchantRoles.includes(user.role)) {
        errors.push(`Rôle invalide pour un utilisateur Merchant Partner: ${user.role}`);
      }
    }
    
    return errors;
  }
}