import { DashboardShell } from '../../../components/dashboard-shell';
import { ConversationsInbox } from '../../../components/conversations-inbox';
import { requireAuthToken } from '../../../lib/auth';
import { fetchConversationDetail, fetchConversations } from '../../../lib/api';
import { getLocale } from '../../../lib/i18n';

const conversationLabels = {
  en: {
    title: 'Conversations',
    searchPlaceholder: 'Search conversations...',
    all: 'All',
    active: 'Active',
    pending: 'Pending',
    unread: 'unread',
    noMatch: 'No conversations match this filter.',
    noSelected: 'No conversation selected',
    online: 'Online',
    offline: 'Offline',
    loading: 'Loading conversation...',
    quickReplies: 'Quick Replies',
    templates: 'Templates',
    messagePlaceholder: 'Type a message...',
    unavailable: 'Unavailable',
    assignedTo: 'Assigned to',
    unassigned: 'Unassigned conversation',
    contactDetails: 'Contact Details',
    tags: 'Tags',
    addTag: '+ Add Tag',
    recentActivity: 'Recent Activity',
    viewCrm: 'View Full CRM Profile',
    assignConversation: 'Assign conversation',
    editContact: 'Edit contact',
    favoriteContact: 'Favorite contact',
    blockContact: 'Block contact',
    startVideoCall: 'Start video call',
    startCall: 'Start call',
    moreActions: 'More actions',
    emoji: 'Emoji',
    attachFile: 'Attach file',
    sendMessage: 'Send message',
    failedSend: 'Failed to send message',
    justNow: 'Just now',
    fallbackMessagePrefix: 'No active thread loaded for',
    fallbackMessageSuffix: 'yet.',
    quickReply1: 'Checking this for you now.',
    quickReply2: 'Could you share your order ID?',
    quickReply3: 'I have escalated this to our support team.',
    template1Label: 'Activation Follow-up',
    template1Body: 'Your activation request is being processed. We will share the update shortly.',
    template2Label: 'Shipping Update',
    template2Body: 'Your shipment is in transit and the tracking page will refresh within a few minutes.',
    template3Label: 'Discount Clarification',
    template3Body: 'The enterprise discount is still available for qualifying annual plans.',
  },
  id: {
    title: 'Percakapan',
    searchPlaceholder: 'Cari percakapan...',
    all: 'Semua',
    active: 'Aktif',
    pending: 'Tertunda',
    unread: 'belum dibaca',
    noMatch: 'Tidak ada percakapan yang cocok dengan filter ini.',
    noSelected: 'Belum ada percakapan dipilih',
    online: 'Online',
    offline: 'Offline',
    loading: 'Memuat percakapan...',
    quickReplies: 'Balasan Cepat',
    templates: 'Template',
    messagePlaceholder: 'Ketik pesan...',
    unavailable: 'Tidak tersedia',
    assignedTo: 'Ditangani oleh',
    unassigned: 'Percakapan belum ditugaskan',
    contactDetails: 'Detail Kontak',
    tags: 'Tag',
    addTag: '+ Tambah Tag',
    recentActivity: 'Aktivitas Terbaru',
    viewCrm: 'Lihat Profil CRM Lengkap',
    assignConversation: 'Assign percakapan',
    editContact: 'Edit kontak',
    favoriteContact: 'Favoritkan kontak',
    blockContact: 'Blokir kontak',
    startVideoCall: 'Mulai video call',
    startCall: 'Mulai panggilan',
    moreActions: 'Aksi lainnya',
    emoji: 'Emoji',
    attachFile: 'Lampirkan file',
    sendMessage: 'Kirim pesan',
    failedSend: 'Gagal mengirim pesan',
    justNow: 'Baru saja',
    fallbackMessagePrefix: 'Belum ada thread aktif untuk',
    fallbackMessageSuffix: 'saat ini.',
    quickReply1: 'Saya cek dulu untuk Anda.',
    quickReply2: 'Bisa kirimkan ID order Anda?',
    quickReply3: 'Saya sudah eskalasikan ini ke tim support.',
    template1Label: 'Follow-up Aktivasi',
    template1Body: 'Permintaan aktivasi Anda sedang diproses. Kami akan segera membagikan pembaruannya.',
    template2Label: 'Update Pengiriman',
    template2Body: 'Pengiriman Anda sedang berjalan dan halaman tracking akan diperbarui dalam beberapa menit.',
    template3Label: 'Klarifikasi Diskon',
    template3Body: 'Diskon enterprise masih tersedia untuk paket tahunan yang memenuhi syarat.',
  },
} as const;

export default async function ConversationsPage({
  searchParams,
}: {
  searchParams?: Promise<{ q?: string }>;
}) {
  const token = await requireAuthToken();
  const locale = await getLocale();
  const labels = conversationLabels[locale];
  const resolvedSearchParams = (await searchParams) || {};
  const search = resolvedSearchParams.q?.trim() || '';
  const conversations = await fetchConversations(token, { filter: 'all', search });
  const initialConversationDetail = conversations[0]
    ? await fetchConversationDetail(token, conversations[0].id)
    : null;

  return (
    <DashboardShell
      currentPath="/dashboard/conversations"
      title={labels.title}
      searchPlaceholder={labels.searchPlaceholder}
      searchValue={search}
      searchActionPath="/dashboard/conversations"
      searchQueryName="q"
    >
      <ConversationsInbox
        labels={labels}
        initialConversations={conversations}
        initialConversationDetail={initialConversationDetail}
        initialSearch={search}
      />
    </DashboardShell>
  );
}
