'use client';

import { useState, useEffect } from 'react';
import Link from 'next/link';
import { useRouter, useSearchParams } from 'next/navigation';
import DeleteButton from './DeleteButton';
import Pagination from '@/lib/components/Pagination';

interface EmailTemplate {
    id: string;
    title: string;
    content: string;
    createdAt: string;
    modifiedAt: string;
}

interface PaginationInfo {
    page: number;
    pageSize: number;
    totalCount: number;
    totalPages: number;
}

interface EmailTemplatesResponse {
    data: EmailTemplate[];
    pagination: PaginationInfo;
}

export default function AdminEmailTemplates() {
    const router = useRouter();
    const searchParams = useSearchParams();

    // Get pagination values from URL params
    const initialPage = parseInt(searchParams.get('page') || '1');
    const initialPageSize = parseInt(searchParams.get('pageSize') || '10');

    // State for pagination
    const [page, setPage] = useState(initialPage);
    const [pageSize, setPageSize] = useState(initialPageSize);

    // State for data
    const [emailTemplates, setEmailTemplates] = useState<EmailTemplate[]>([]);
    const [pagination, setPagination] = useState<PaginationInfo>({
        page: initialPage,
        pageSize: initialPageSize,
        totalCount: 0,
        totalPages: 0
    });
    const [isLoading, setIsLoading] = useState(true);
    const [error, setError] = useState<string | null>(null);

    // Fetch email templates with pagination
    useEffect(() => {
        const fetchEmailTemplates = async () => {
            setIsLoading(true);
            setError(null);

            try {
                // Build query string with pagination
                const params = new URLSearchParams();
                params.append('page', initialPage.toString());
                params.append('pageSize', initialPageSize.toString());

                const response = await fetch(`/api/email-templates?${params.toString()}`);

                if (!response.ok) {
                    throw new Error('Failed to fetch email templates');
                }

                const responseData: EmailTemplatesResponse = await response.json();
                setEmailTemplates(responseData.data);
                setPagination(responseData.pagination);
            } catch (err) {
                console.error('Error fetching email templates:', err);
                setError(err instanceof Error ? err.message : 'An error occurred');
            } finally {
                setIsLoading(false);
            }
        };

        fetchEmailTemplates();
    }, [initialPage, initialPageSize]);

    // Handle page change
    const handlePageChange = (newPage: number) => {
        setPage(newPage);

        // Build query string with new page
        const params = new URLSearchParams(searchParams.toString());
        params.set('page', newPage.toString());

        // Update URL with new page
        router.push(`/admin/email-templates?${params.toString()}`);
    };

    return (
        <div>
            <div className="flex justify-between items-center">
                <h1 className="text-2xl font-semibold text-gray-900 dark:text-gray-100">Email Templates</h1>
                <Link
                    href="/admin/email-templates/new"
                    className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
                >
                    Add New Template
                </Link>
            </div>

            {/* Error Message */}
            {error && (
                <div className="bg-red-50 border-l-4 border-red-400 p-4 my-4 dark:bg-red-900/20 dark:border-red-500">
                    <div className="flex">
                        <div className="flex-shrink-0">
                            <svg className="h-5 w-5 text-red-400 dark:text-red-300" viewBox="0 0 20 20" fill="currentColor">
                                <path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clipRule="evenodd" />
                            </svg>
                        </div>
                        <div className="ml-3">
                            <p className="text-sm text-red-700 dark:text-red-300">{error}</p>
                        </div>
                    </div>
                </div>
            )}

            {/* Loading State */}
            {isLoading ? (
                <div className="text-center py-8">
                    <p className="text-gray-500 dark:text-gray-400">Loading email templates...</p>
                </div>
            ) : (
                <div className="mt-8 flex flex-col">
                    <div className="-my-2 -mx-4 overflow-x-auto sm:-mx-6 lg:-mx-8">
                        <div className="inline-block min-w-full py-2 align-middle md:px-6 lg:px-8">
                            <div className="overflow-hidden shadow ring-1 ring-black ring-opacity-5 md:rounded-lg">
                                <table className="min-w-full divide-y divide-gray-300 dark:divide-gray-700">
                                    <thead className="bg-gray-50 dark:bg-gray-700">
                                        <tr>
                                            <th
                                                scope="col"
                                                className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
                                                style={{ width: "60%" }}
                                            >
                                                Title
                                            </th>
                                            <th
                                                scope="col"
                                                className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
                                                style={{ width: "10%" }}
                                            >
                                                Created
                                            </th>
                                            <th
                                                scope="col"
                                                className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
                                                style={{ width: "10%" }}
                                            >
                                                Modified
                                            </th>
                                            <th scope="col" className="relative py-3.5 pl-3 pr-4 sm:pr-6" style={{ width: "15%" }}>
                                                <span className="sr-only">Actions</span>
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody className="divide-y divide-gray-200 bg-white dark:bg-gray-800 dark:divide-gray-700">
                                        {emailTemplates.length > 0 ? (
                                            emailTemplates.map((template) => (
                                                <tr key={template.id}>
                                                    <td className="py-4 px-3 text-sm text-gray-500 dark:text-gray-400">
                                                        <Link
                                                            href={`/admin/email-templates/detail/${template.id}`}
                                                            className="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-300"
                                                        >
                                                            {template.title}
                                                        </Link>
                                                    </td>
                                                    <td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
                                                        {new Date(template.createdAt).toLocaleDateString()}
                                                        {' '}
                                                        {new Date(template.createdAt).toLocaleTimeString()}
                                                    </td>
                                                    <td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
                                                        {new Date(template.modifiedAt).toLocaleDateString()}
                                                        {' '}
                                                        {new Date(template.modifiedAt).toLocaleTimeString()}
                                                    </td>
                                                    <td className="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6">
                                                        <Link
                                                            href={`/admin/email-templates/edit/${template.id}`}
                                                            className="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-300 mr-4"
                                                        >
                                                            Edit
                                                        </Link>
                                                        <DeleteButton templateId={template.id} />
                                                    </td>
                                                </tr>
                                            ))
                                        ) : (
                                            <tr>
                                                <td colSpan={5} className="py-4 pl-4 pr-3 text-sm text-gray-500 dark:text-gray-400 text-center">
                                                    No email templates found. Create your first template!
                                                </td>
                                            </tr>
                                        )}
                                    </tbody>
                                </table>

                                {/* Pagination */}
                                <Pagination
                                    currentPage={pagination.page}
                                    totalPages={pagination.totalPages}
                                    totalItems={pagination.totalCount}
                                    pageSize={pagination.pageSize}
                                    onPageChange={handlePageChange}
                                />
                            </div>
                        </div>
                    </div>
                </div>
            )}
        </div>
    );
}
