import { CampaignsManagementBoard } from '../../../components/campaigns-management-board';
import { DashboardShell } from '../../../components/dashboard-shell';
import { requireAuthToken } from '../../../lib/auth';
import { fetchCampaigns } from '../../../lib/api';
import { getLocale } from '../../../lib/i18n';

const campaignLabels = {
  en: {
    title: 'Campaign Management',
    copy: 'Design, schedule, and track your WhatsApp broadcast performance.',
    newCampaign: 'New Campaign',
    createCampaign: 'Create Campaign',
    launchBroadcast: 'Launch a new WhatsApp broadcast',
    campaignName: 'Campaign Name',
    audienceLabel: 'Audience Label',
    audienceGroup: 'Audience Group',
    totalRecipients: 'Total Recipients',
    status: 'Status',
    scheduledAt: 'Scheduled At',
    templateName: 'Template Name',
    language: 'Language',
    messageTitle: 'Message Title',
    messageBody: 'Message Body',
    primaryButton: 'Primary Button',
    secondaryButton: 'Secondary Button',
    bannerImageUrl: 'Banner Image URL',
    cancel: 'Cancel',
    creating: 'Creating...',
    failedCreate: 'Failed to create campaign',
    totalMessages: 'Total Messages',
    deliveryRate: 'Delivery Rate',
    scheduled: 'Scheduled',
    failedDelivery: 'Failed Delivery',
    allCampaigns: 'All Campaigns',
    sent: 'Sent',
    draft: 'Draft',
    failed: 'Failed',
    searchPlaceholder: 'Search campaigns...',
    sortNewest: 'Sort by: Newest',
    sortOldest: 'Sort by: Oldest',
    sortDelivery: 'Sort by: Best Delivery',
    audience: 'Audience',
    date: 'Date',
    actions: 'Actions',
    pendingSend: 'Pending send...',
    retry: 'Retry',
    actionsFor: 'Actions for',
    previousPage: 'Previous page',
    nextPage: 'Next page',
    showing: 'Showing',
    of: 'of',
    campaigns: 'campaigns',
    optimizationTitle: 'Campaign Performance Optimization',
    optimizationCopyA: 'Based on your recent broadcasts, campaigns sent between',
    optimizationCopyB: 'on Tuesdays have a',
    optimizationCopyC: 'read rate. Consider scheduling your next template for this window.',
    higherRate: '15% higher',
    viewDetails: 'View Details',
    templateHealth: 'Template Health',
    healthCopy: 'High quality rating across all approved templates.',
    namePlaceholder: 'Summer Launch Blast',
    audiencePlaceholder: 'VIP Customer List',
    groupPlaceholder: 'High-value segment',
    templatePlaceholder: 'summer_promo_v3',
    titlePlaceholder: 'Hi {{name}}, your private offer is ready',
    bodyPlaceholder: 'Write the campaign body that will appear in the WhatsApp template preview.',
    primaryPlaceholder: 'Shop Now',
    secondaryPlaceholder: 'View Catalog',
  },
  id: {
    title: 'Manajemen Kampanye',
    copy: 'Rancang, jadwalkan, dan pantau performa broadcast WhatsApp Anda.',
    newCampaign: 'Kampanye Baru',
    createCampaign: 'Buat Kampanye',
    launchBroadcast: 'Jalankan broadcast WhatsApp baru',
    campaignName: 'Nama Kampanye',
    audienceLabel: 'Label Audiens',
    audienceGroup: 'Grup Audiens',
    totalRecipients: 'Total Penerima',
    status: 'Status',
    scheduledAt: 'Dijadwalkan Pada',
    templateName: 'Nama Template',
    language: 'Bahasa',
    messageTitle: 'Judul Pesan',
    messageBody: 'Isi Pesan',
    primaryButton: 'Tombol Utama',
    secondaryButton: 'Tombol Kedua',
    bannerImageUrl: 'URL Gambar Banner',
    cancel: 'Batal',
    creating: 'Membuat...',
    failedCreate: 'Gagal membuat kampanye',
    totalMessages: 'Total Pesan',
    deliveryRate: 'Rasio Terkirim',
    scheduled: 'Terjadwal',
    failedDelivery: 'Gagal Terkirim',
    allCampaigns: 'Semua Kampanye',
    sent: 'Terkirim',
    draft: 'Draft',
    failed: 'Gagal',
    searchPlaceholder: 'Cari kampanye...',
    sortNewest: 'Urutkan: Terbaru',
    sortOldest: 'Urutkan: Terlama',
    sortDelivery: 'Urutkan: Delivery Terbaik',
    audience: 'Audiens',
    date: 'Tanggal',
    actions: 'Aksi',
    pendingSend: 'Menunggu dikirim...',
    retry: 'Coba ulang',
    actionsFor: 'Aksi untuk',
    previousPage: 'Halaman sebelumnya',
    nextPage: 'Halaman berikutnya',
    showing: 'Menampilkan',
    of: 'dari',
    campaigns: 'kampanye',
    optimizationTitle: 'Optimasi Performa Kampanye',
    optimizationCopyA: 'Berdasarkan broadcast terbaru, kampanye yang dikirim antara',
    optimizationCopyB: 'pada hari Selasa memiliki',
    optimizationCopyC: 'rasio dibaca. Pertimbangkan menjadwalkan template berikutnya pada waktu ini.',
    higherRate: '15% lebih tinggi',
    viewDetails: 'Lihat Detail',
    templateHealth: 'Kesehatan Template',
    healthCopy: 'Rating kualitas tinggi di seluruh template yang sudah disetujui.',
    namePlaceholder: 'Promo Launching Musim Panas',
    audiencePlaceholder: 'Daftar Pelanggan VIP',
    groupPlaceholder: 'Segmen bernilai tinggi',
    templatePlaceholder: 'promo_musim_panas_v3',
    titlePlaceholder: 'Hai {{name}}, penawaran khusus Anda sudah siap',
    bodyPlaceholder: 'Tulis isi kampanye yang akan muncul di preview template WhatsApp.',
    primaryPlaceholder: 'Belanja Sekarang',
    secondaryPlaceholder: 'Lihat Katalog',
  },
} as const;

export default async function CampaignsPage() {
  const token = await requireAuthToken();
  const locale = await getLocale();
  const labels = campaignLabels[locale];
  const campaignsData = await fetchCampaigns(token);

  return (
    <DashboardShell currentPath="/dashboard/campaigns" title={labels.title}>
      <CampaignsManagementBoard labels={labels} campaigns={campaignsData.items} metrics={campaignsData.metrics} />
    </DashboardShell>
  );
}
