import { DashboardShell } from '../../../components/dashboard-shell';
import { ContactsDirectoryBoard } from '../../../components/contacts-directory-board';
import { requireAuthToken } from '../../../lib/auth';
import { fetchContactsDirectory } from '../../../lib/api';
import { getLocale } from '../../../lib/i18n';

const contactLabels = {
  en: {
    title: 'Contacts Directory',
    copy: 'Manage your customer database and communication segments.',
    import: 'Import',
    export: 'Export',
    addContact: 'Add Contact',
    importedPrefix: 'Imported',
    importedSuffix: 'contacts successfully.',
    failedImport: 'Failed to import contacts',
    emptyCsv: 'CSV file is empty.',
    invalidCsv: 'CSV does not contain valid contact rows.',
    failedImportContact: 'Failed to import contact',
    filters: 'Filters',
    searchPlaceholder: 'Search contacts...',
    allStatuses: 'All Statuses',
    active: 'Active',
    inactive: 'Inactive',
    allTags: 'All Tags',
    clearFilters: 'Clear all filters',
    contactName: 'Contact Name',
    phoneNumber: 'Phone Number',
    status: 'Status',
    tags: 'Tags',
    lastMessage: 'Last Message',
    showing: 'Showing',
    of: 'of',
    contacts: 'contacts',
    rowsPerPage: 'Rows per page:',
    newContact: 'New Contact',
    addProfile: 'Add a new contact profile',
    failedCreate: 'Failed to create contact',
    fullName: 'Full Name',
    emailAddress: 'Email Address',
    company: 'Company',
    notes: 'Notes',
    cancel: 'Cancel',
    saving: 'Saving...',
    saveContact: 'Save Contact',
  },
  id: {
    title: 'Direktori Kontak',
    copy: 'Kelola database pelanggan dan segmentasi komunikasi Anda.',
    import: 'Import',
    export: 'Export',
    addContact: 'Tambah Kontak',
    importedPrefix: 'Berhasil import',
    importedSuffix: 'kontak.',
    failedImport: 'Gagal import kontak',
    emptyCsv: 'File CSV kosong.',
    invalidCsv: 'CSV tidak berisi baris kontak yang valid.',
    failedImportContact: 'Gagal import kontak',
    filters: 'Filter',
    searchPlaceholder: 'Cari kontak...',
    allStatuses: 'Semua Status',
    active: 'Aktif',
    inactive: 'Nonaktif',
    allTags: 'Semua Tag',
    clearFilters: 'Hapus semua filter',
    contactName: 'Nama Kontak',
    phoneNumber: 'Nomor Telepon',
    status: 'Status',
    tags: 'Tag',
    lastMessage: 'Pesan Terakhir',
    showing: 'Menampilkan',
    of: 'dari',
    contacts: 'kontak',
    rowsPerPage: 'Baris per halaman:',
    newContact: 'Kontak Baru',
    addProfile: 'Tambah profil kontak baru',
    failedCreate: 'Gagal membuat kontak',
    fullName: 'Nama Lengkap',
    emailAddress: 'Alamat Email',
    company: 'Perusahaan',
    notes: 'Catatan',
    cancel: 'Batal',
    saving: 'Menyimpan...',
    saveContact: 'Simpan Kontak',
  },
} as const;

export default async function ContactsPage({
  searchParams,
}: {
  searchParams?: Promise<{
    page?: string;
    limit?: string;
    search?: string;
    status?: string;
    tag?: string;
  }>;
}) {
  const token = await requireAuthToken();
  const locale = await getLocale();
  const labels = contactLabels[locale];
  const query = await searchParams;
  const page = Math.max(1, Number(query?.page || '1'));
  const limit = Math.max(1, Number(query?.limit || '10'));
  const search = query?.search || '';
  const status = query?.status || '';
  const tag = query?.tag || '';
  const data = await fetchContactsDirectory(token, { page, limit, search, status, tag });

  return (
    <DashboardShell currentPath="/dashboard/contacts" title={labels.title}>
      <ContactsDirectoryBoard labels={labels} data={data} filters={{ search, status, tag }} />
    </DashboardShell>
  );
}
