// Types Supabase pour le chat (UUID-based)

export type SupabaseUser = {
  id: string; // UUID
  main_id: number; // ID Laravel
  username?: string;
  name: string;
  email?: string;
  phone?: string;
  is_affiliate: boolean;
  avatar_url?: string;
  status: 'online' | 'offline' | 'away';
  last_seen: string;
  created_at: string;
  updated_at: string;
};

export type SupabaseConversation = {
  id: string; // UUID
  type: 'direct' | 'group';
  name?: string;
  created_at: string;
  updated_at: string;
};

export type SupabaseMessage = {
  id: string; // UUID
  conversation_id: string; // UUID
  sender_id: string; // UUID
  content: string;
  message_type: 'text' | 'image' | 'video' | 'file';
  status: 'sent' | 'delivered' | 'read';
  created_at: string;
  updated_at: string;
};

export type SupabaseConversationParticipant = {
  id: string; // UUID
  conversation_id: string; // UUID
  user_id: string; // UUID
  joined_at: string;
};

export type SupabaseMessageRead = {
  id: string; // UUID
  message_id: string; // UUID
  user_id: string; // UUID
  read_at: string;
};

// Types enrichis pour l'UI
export type ChatUser = SupabaseUser & {
  avatar?: string;
  lastSeen?: string;
};

export type ChatMessage = SupabaseMessage & {
  text: string; // alias pour content
  time: string; // formaté depuis created_at
  timestamp: number; // created_at en timestamp
  isRead: boolean; // calculé depuis message_reads
  isReadByOthers?: boolean; // lu par le(s) destinataire(s) (pour affichage double-check)
  attachments?: Attachment[];
};

export type ChatConversation = SupabaseConversation & {
  avatar?: string;
  isGroup: boolean; // calculé depuis type
  participants: string[]; // UUIDs des participants
  lastMessage?: string;
  lastMessageTime?: string;
  lastMessageTimestamp?: number;
  unreadCount: number; // calculé depuis message_reads
  isPinned?: boolean;
  isMuted?: boolean;
};

export type ChatConversationDetails = ChatConversation & {
  description?: string;
  createdBy?: string; // UUID du créateur
};

export type Attachment = {
  id: string;
  type: 'image' | 'file' | 'video' | 'audio';
  url: string;
  name: string;
  size?: number;
};

// Fonctions utilitaires
export const getInitials = (name: string): string => {
  return name
    .split(' ')
    .map(word => word.charAt(0).toUpperCase())
    .slice(0, 2)
    .join('');
};

export const getAvatarOrInitials = (avatar: string | null, name: string): string => {
  if (avatar) {
    return avatar;
  }
  return getInitials(name);
};

// Fonctions de conversion
export const convertSupabaseUserToChatUser = (user: SupabaseUser): ChatUser => ({
  ...user,
  avatar: user.avatar_url || undefined,
  lastSeen: user.last_seen,
});

export const convertSupabaseMessageToChatMessage = (
  message: SupabaseMessage,
  isRead: boolean = false,
  isReadByOthers: boolean = false
): ChatMessage => ({
  ...message,
  text: message.content,
  time: new Date(message.created_at).toLocaleTimeString(),
  timestamp: new Date(message.created_at).getTime(),
  isRead,
  isReadByOthers,
});

export const convertSupabaseConversationToChatConversation = (
  conversation: SupabaseConversation,
  participants: string[] = [],
  lastMessage?: string,
  lastMessageTime?: string,
  lastMessageTimestamp?: number,
  unreadCount: number = 0
): ChatConversation => ({
  ...conversation,
  isGroup: conversation.type === 'group',
  participants,
  lastMessage,
  lastMessageTime,
  lastMessageTimestamp,
  unreadCount,
});
