import { Conversation, ConversationDetails } from './types';

// Base de données mock des conversations
export const mockConversations: Conversation[] = [
  {
    id: 1,
    name: "Groupe Développement",
    avatar: "👨‍💻",
    isGroup: true,
    participants: [1, 2, 4, 5], // Vous, Marie, Sophie, Pierre
    lastMessage: "Salut tout le monde ! Comment ça va ?",
    lastMessageTime: "14:30",
    lastMessageTimestamp: Date.now() - 30 * 60 * 1000, // Il y a 30 min
    unreadCount: 3,
    isPinned: true,
    isMuted: false
  },
  {
    id: 2,
    name: "Marie Dubois",
    avatar: null, // Pas d'avatar, utilisera "MD"
    isGroup: false,
    participants: [1, 2], // Vous et Marie
    lastMessage: "Merci pour le code !",
    lastMessageTime: "13:45",
    lastMessageTimestamp: Date.now() - 75 * 60 * 1000, // Il y a 75 min
    unreadCount: 0,
    isPinned: false,
    isMuted: false
  },
  {
    id: 3,
    name: "Équipe Marketing",
    avatar: null, // Pas d'avatar, utilisera "EM"
    isGroup: true,
    participants: [1, 3, 6, 7], // Vous, Jean, Emma, Lucas
    lastMessage: "Réunion demain à 10h",
    lastMessageTime: "12:20",
    lastMessageTimestamp: Date.now() - 2 * 60 * 60 * 1000, // Il y a 2h
    unreadCount: 1,
    isPinned: false,
    isMuted: false
  },
  {
    id: 4,
    name: "Jean Martin",
    avatar: "👨",
    isGroup: false,
    participants: [1, 3], // Vous et Jean
    lastMessage: "Parfait, on se voit demain",
    lastMessageTime: "11:15",
    lastMessageTimestamp: Date.now() - 3 * 60 * 60 * 1000, // Il y a 3h
    unreadCount: 0,
    isPinned: false,
    isMuted: false
  },
  {
    id: 5,
    name: "Groupe Projet OwaHub",
    avatar: "🚀",
    isGroup: true,
    participants: [1, 2, 4, 5, 6], // Vous, Marie, Sophie, Pierre, Emma
    lastMessage: "L'interface est superbe !",
    lastMessageTime: "10:30",
    lastMessageTimestamp: Date.now() - 4 * 60 * 60 * 1000, // Il y a 4h
    unreadCount: 5,
    isPinned: true,
    isMuted: false
  },
  {
    id: 6,
    name: "Sophie Laurent",
    avatar: null, // Pas d'avatar, utilisera "SL"
    isGroup: false,
    participants: [1, 4], // Vous et Sophie
    lastMessage: "À plus tard !",
    lastMessageTime: "Hier",
    lastMessageTimestamp: Date.now() - 24 * 60 * 60 * 1000, // Il y a 1 jour
    unreadCount: 0,
    isPinned: false,
    isMuted: false
  }
];

// Détails complets des conversations
export const mockConversationDetails: ConversationDetails[] = [
  {
    ...mockConversations[0],
    description: "Discussion pour l'équipe de développement",
    createdAt: "2024-01-15",
    createdBy: 5
  },
  {
    ...mockConversations[2],
    description: "Équipe marketing et communication",
    createdAt: "2024-02-01",
    createdBy: 6
  },
  {
    ...mockConversations[4],
    description: "Projet principal de développement OwaHub",
    createdAt: "2024-03-10",
    createdBy: 2
  }
];

// Fonction API mock pour récupérer toutes les conversations
export const getConversations = async (): Promise<Conversation[]> => {
  await new Promise(resolve => setTimeout(resolve, 500));
  // Trier par dernière activité (pinned d'abord, puis par timestamp)
  return [...mockConversations].sort((a, b) => {
    if (a.isPinned && !b.isPinned) return -1;
    if (!a.isPinned && b.isPinned) return 1;
    return b.lastMessageTimestamp - a.lastMessageTimestamp;
  });
};

// Fonction API mock pour récupérer une conversation par ID
export const getConversationById = async (conversationId: number): Promise<ConversationDetails | undefined> => {
  await new Promise(resolve => setTimeout(resolve, 300));
  const conversation = mockConversations.find(c => c.id === conversationId);
  if (!conversation) return undefined;
  
  const details = mockConversationDetails.find(d => d.id === conversationId);
  if (details) return details;
  
  // Si pas de détails, créer un objet par défaut
  return {
    ...conversation,
    createdAt: "2024-01-01",
    createdBy: 1
  };
};

// Fonction API mock pour rechercher des conversations
export const searchConversations = async (query: string): Promise<Conversation[]> => {
  await new Promise(resolve => setTimeout(resolve, 300));
  const lowerQuery = query.toLowerCase();
  return mockConversations.filter(conv => 
    conv.name.toLowerCase().includes(lowerQuery) ||
    conv.lastMessage.toLowerCase().includes(lowerQuery)
  );
};

// Fonction API mock pour créer une nouvelle conversation
export const createConversation = async (
  name: string,
  participants: number[],
  isGroup: boolean
): Promise<Conversation> => {
  await new Promise(resolve => setTimeout(resolve, 500));
  
  const newConversation: Conversation = {
    id: Math.max(...mockConversations.map(c => c.id)) + 1,
    name,
    avatar: isGroup ? "👥" : "👤",
    isGroup,
    participants,
    lastMessage: "",
    lastMessageTime: "Maintenant",
    lastMessageTimestamp: Date.now(),
    unreadCount: 0,
    isPinned: false,
    isMuted: false
  };
  
  mockConversations.push(newConversation);
  return newConversation;
};

// Fonction API mock pour mettre à jour une conversation
export const updateConversation = async (
  conversationId: number,
  updates: Partial<Conversation>
): Promise<Conversation | undefined> => {
  await new Promise(resolve => setTimeout(resolve, 300));
  
  const index = mockConversations.findIndex(c => c.id === conversationId);
  if (index === -1) return undefined;
  
  mockConversations[index] = { ...mockConversations[index], ...updates };
  return mockConversations[index];
};

