import Link from 'next/link';
import { getDataSource, EmailTemplate } from '@/lib/database';
import DeleteButton from './DeleteButton';

export default async function AdminEmailTemplates() {
    // Fetch email templates from the database
    const dataSource = await getDataSource();
    const emailTemplateRepository = dataSource.getRepository(EmailTemplate);

    const emailTemplates = await emailTemplateRepository.find({
        order: { createdAt: 'DESC' }
    });

    return (
        <div>
            <div className="flex justify-between items-center">
                <h1 className="text-2xl font-semibold text-gray-900">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>

            <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">
                                <thead className="bg-gray-50">
                                    <tr>
                                        <th
                                            scope="col"
                                            className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
                                            style={{ width: "60%" }}
                                        >
                                            Title
                                        </th>
                                        <th
                                            scope="col"
                                            className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
                                            style={{ width: "10%" }}
                                        >
                                            Created
                                        </th>
                                        <th
                                            scope="col"
                                            className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
                                            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">
                                    {emailTemplates.length > 0 ? (
                                        emailTemplates.map((template) => (
                                            <tr key={template.id}>

                                                <td className="py-4 px-3 text-sm text-gray-500">
                                                    <Link
                                                        href={`/admin/email-templates/detail/${template.id}`}
                                                        className="text-indigo-600 hover:text-indigo-900"
                                                    >
                                                        {template.title}
                                                    </Link>
                                                </td>
                                                <td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
                                                    {new Date(template.createdAt).toLocaleDateString()}
                                                    {new Date(template.createdAt).toLocaleTimeString()}
                                                </td>
                                                <td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
                                                    {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 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 text-center">
                                                No email templates found. Create your first template!
                                            </td>
                                        </tr>
                                    )}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    );
}
