import { useTranslation } from '@/hooks/use-translation';
import AppLayout from '@/layouts/app-layout';
import type { BreadcrumbItem, WhatsAppTemplate } from '@/types';
import { Head, usePage } from '@inertiajs/react';
import { useEffect } from 'react';
import { toast } from 'sonner';
import ChannelSwitch from './channel-switch';
import WhatsAppPanel from './whatsapp-panel';

interface WhatsAppContact {
    id: number;
    name: string;
    phone: string;
    opted_in: boolean;
}

interface PageProps {
    whatsappTemplates: WhatsAppTemplate[];
    whatsappClients: WhatsAppContact[];
    whatsappOwners: WhatsAppContact[];
    flash: {
        type: string;
        message: string;
    };
    [key: string]: unknown;
}

export default function WhatsAppNewsletter() {
    const { t } = useTranslation();
    const { whatsappTemplates, whatsappClients, whatsappOwners, flash } = usePage<PageProps>().props;

    const breadcrumbs: BreadcrumbItem[] = [
        { title: t('newsletters', 'newsletters'), href: '/newsletters' },
        { title: t('newsletters', 'whatsapp'), href: '/newsletters/whatsapp' },
    ];

    useEffect(() => {
        if (flash?.type === 'success') {
            toast.success(flash.message);
        } else if (flash?.type === 'error') {
            toast.error(flash.message);
        }
    }, [flash]);

    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title={t('newsletters', 'whatsapp')} />
            <div className="flex h-full flex-1 flex-col gap-4 rounded-xl p-4">
                <div className="container mx-auto max-w-[1200px] pb-4">
                    <ChannelSwitch active="whatsapp" />
                    <WhatsAppPanel
                        templates={whatsappTemplates}
                        clients={whatsappClients}
                        owners={whatsappOwners}
                    />
                </div>
            </div>
        </AppLayout>
    );
}
