/**
 * Types TypeScript pour l'authentification - COOP-CA AMIFOND
 *
 * Types correspondant aux modèles Django backend et aux endpoints API
 */

// Types des utilisateurs selon le backend Django
export type UserType =
  | "super_admin"
  | "ca_member"
  | "manager"
  | "account_manager"
  | "cashier"
  | "client"
  | "it_staff"
  | "other_staff"
  | "agency_agent"
  | "community_leader";

export interface UserProfile {
  avatar?: string;
  bio?: string;
  date_of_birth?: string;
  gender?: "M" | "F" | "O";
  nationality?: string;
  address?: string;
  city?: string;
  country?: string;
  postal_code?: string;
  preferred_language?: string;
  timezone?: string;
  notification_preferences?: Record<string, boolean>;
}

export interface User {
  id: number;
  email: string;
  username?: string;
  first_name: string;
  last_name: string;
  user_type: UserType;
  user_type_display: string; // Champ ajouté depuis le serializer
  phone_number: string;
  employee_id?: string;
  department?: string;
  is_active: boolean;
  is_verified: boolean;
  is_staff: boolean;
  is_superuser?: boolean;
  has_two_factor?: boolean;
  last_login?: string;
  date_joined: string;
  permissions_level: number; // Champ ajouté
  additional_roles?: string[]; // Champ ajouté
  profile?: UserProfile;
}

// Types pour les requêtes d'authentification
export interface LoginRequest {
  email: string;
  password: string;
  remember?: boolean;
}

export interface RegisterRequest {
  email: string;
  username: string;
  password: string;
  re_password: string;
  first_name: string;
  last_name: string;
  phone_number: string;
  user_type?: UserType;
}

export interface PasswordChangeRequest {
  old_password: string;
  new_password: string;
  re_new_password: string;
}

export interface PasswordResetRequest {
  email: string;
}

export interface PasswordResetConfirmRequest {
  uid: string;
  token: string;
  new_password: string;
  re_new_password: string;
}

// Types pour les réponses d'authentification
export interface AuthTokens {
  access: string;
  refresh: string;
  access_token_expiration?: string;
  refresh_token_expiration?: string;
}

export interface LoginResponse {
  user: User;
}

export interface RegisterResponse {
  user: User;
  detail: string;
}

// Types pour la gestion des erreurs
export interface AuthError {
  message: string;
  field?: string;
  code?: string;
  details?: Record<string, string[]>;
  data?: unknown; // Include raw error data from Django
}

export interface ApiError {
  message: string;
  status: number;
  errors?: Record<string, string[]>;
}

// Types pour le store d'authentification
export interface AuthState {
  // État d'authentification
  isAuthenticated: boolean;
  isLoading: boolean;
  user: User | null;

  // Actions
  login: (credentials: LoginRequest) => Promise<void>;
  register: (data: RegisterRequest) => Promise<void>;
  logout: () => Promise<void>;
  updateUser: (user: User) => void;
  clearError: () => void;

  // État des erreurs
  error: AuthError | null;
  loginError: AuthError | null;
  registerError: AuthError | null;
}

// Types pour la vérification 2FA
export interface TwoFactorSetupRequest {
  password: string;
}

export interface TwoFactorVerifyRequest {
  token: string;
  backup_token?: string;
}

export interface TwoFactorResponse {
  qr_code?: string;
  backup_tokens?: string[];
  verified?: boolean;
  enabled?: boolean;
}

// Types pour la vérification par email/SMS
export interface VerificationRequest {
  email?: string;
  phone_number?: string;
  verification_type: "email" | "sms";
}

export interface VerificationConfirmRequest {
  code: string;
  email?: string;
  phone_number?: string;
}

// Types pour les permissions
export interface Permission {
  id: number;
  name: string;
  content_type: number;
  codename: string;
}

export interface Group {
  id: number;
  name: string;
  permissions: Permission[];
}

// Types pour les statistiques utilisateur (admin)
export interface UserStats {
  total_users: number;
  active_users: number;
  new_users_today: number;
  new_users_this_week: number;
  new_users_this_month: number;
  users_by_type: Record<UserType, number>;
  verification_stats: {
    verified: number;
    unverified: number;
    pending: number;
  };
  two_factor_stats: {
    enabled: number;
    disabled: number;
  };
}

// Types pour les actions en lot (admin)
export interface BulkAction {
  action: "activate" | "deactivate" | "delete" | "verify" | "unverify";
  user_ids: number[];
  reason?: string;
}

export interface BulkActionResponse {
  success: boolean;
  affected_count: number;
  errors?: string[];
  details?: string;
}
