'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 Customer {
    id: string;
    name: string;
    url: string;
    email: string;
    createdAt: string;
    modifiedAt: string;
}

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

interface CustomersResponse {
    data: Customer[];
    pagination: PaginationInfo;
}

export default function AdminCustomers() {
    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);

    // Search filters state
    const [filters, setFilters] = useState({
        name: '',
        email: '',
        url: '',
        hasEmail: false
    });
    const [debouncedFilters, setDebouncedFilters] = useState(filters);

    // State for data
    const [customers, setCustomers] = useState<Customer[]>([]);
    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 customers with pagination
    useEffect(() => {
        const fetchCustomers = async () => {
            setIsLoading(true);
            setError(null);

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

                if (debouncedFilters.name) params.append('name', debouncedFilters.name);
                if (debouncedFilters.email) params.append('email', debouncedFilters.email);
                if (debouncedFilters.url) params.append('url', debouncedFilters.url);
                if (debouncedFilters.hasEmail) params.append('hasEmail', 'true');

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

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

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

        fetchCustomers();
    }, [initialPage, initialPageSize, debouncedFilters]);

    // Debounce filter changes
    useEffect(() => {
        const timer = setTimeout(() => {
            setDebouncedFilters(filters);
        }, 300);

        return () => clearTimeout(timer);
    }, [filters]);

    // Handle filter changes
    const handleFilterChange = (key: keyof typeof filters, value: string | boolean) => {
        setFilters(prev => ({ ...prev, [key]: value }));
    };

    // Clear all filters
    const handleClearFilters = () => {
        setFilters({
            name: '',
            email: '',
            url: '',
            hasEmail: false
        });
    };

    // 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/customers?${params.toString()}`);
    };

    return (
        <div>
            <div className="flex justify-between items-center mb-4">
                <div>
                    <h1 className="text-2xl font-semibold text-gray-900 dark:text-gray-100">Customers</h1>
                    {!isLoading && (
                        <p className="text-sm text-gray-500 dark:text-gray-400 mt-1">
                            Total: {pagination.totalCount} customer{pagination.totalCount !== 1 ? 's' : ''}
                        </p>
                    )}
                </div>
                <Link
                    href="/admin/customers/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 Customer
                </Link>
            </div>

            {/* Search Filters */}
            <div className="mb-6 bg-white dark:bg-gray-800 shadow rounded-lg p-6">
                <div className="flex justify-between items-center mb-4">
                    <h2 className="text-lg font-medium text-gray-900 dark:text-gray-100">Filters</h2>
                    <button
                        onClick={handleClearFilters}
                        className="text-sm text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200"
                    >
                        Clear all filters
                    </button>
                </div>
                <div className="grid grid-cols-1 gap-6 sm:grid-cols-2 lg:grid-cols-4">
                    <div>
                        <label htmlFor="name-filter" className="block text-sm font-medium text-gray-700 dark:text-gray-300">
                            Search by Name
                        </label>
                        <div className="mt-1 relative rounded-md shadow-sm">
                            <input
                                type="text"
                                id="name-filter"
                                value={filters.name}
                                onChange={(e) => handleFilterChange('name', e.target.value)}
                                className="block w-full rounded-md border-gray-300 pr-10 focus:border-indigo-500 focus:ring-indigo-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white sm:text-sm"
                                placeholder="Enter name..."
                            />
                            {filters.name && (
                                <button
                                    onClick={() => handleFilterChange('name', '')}
                                    className="absolute inset-y-0 right-0 pr-3 flex items-center"
                                >
                                    <svg className="h-4 w-4 text-gray-400 hover:text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
                                    </svg>
                                </button>
                            )}
                        </div>
                    </div>
                    <div>
                        <label htmlFor="email-filter" className="block text-sm font-medium text-gray-700 dark:text-gray-300">
                            Search by Email
                        </label>
                        <div className="mt-1 relative rounded-md shadow-sm">
                            <input
                                type="text"
                                id="email-filter"
                                value={filters.email}
                                onChange={(e) => handleFilterChange('email', e.target.value)}
                                className="block w-full rounded-md border-gray-300 pr-10 focus:border-indigo-500 focus:ring-indigo-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white sm:text-sm"
                                placeholder="Enter email..."
                            />
                            {filters.email && (
                                <button
                                    onClick={() => handleFilterChange('email', '')}
                                    className="absolute inset-y-0 right-0 pr-3 flex items-center"
                                >
                                    <svg className="h-4 w-4 text-gray-400 hover:text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
                                    </svg>
                                </button>
                            )}
                        </div>
                    </div>
                    <div>
                        <label htmlFor="url-filter" className="block text-sm font-medium text-gray-700 dark:text-gray-300">
                            Search by URL
                        </label>
                        <div className="mt-1 relative rounded-md shadow-sm">
                            <input
                                type="text"
                                id="url-filter"
                                value={filters.url}
                                onChange={(e) => handleFilterChange('url', e.target.value)}
                                className="block w-full rounded-md border-gray-300 pr-10 focus:border-indigo-500 focus:ring-indigo-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white sm:text-sm"
                                placeholder="Enter URL..."
                            />
                            {filters.url && (
                                <button
                                    onClick={() => handleFilterChange('url', '')}
                                    className="absolute inset-y-0 right-0 pr-3 flex items-center"
                                >
                                    <svg className="h-4 w-4 text-gray-400 hover:text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
                                    </svg>
                                </button>
                            )}
                        </div>
                    </div>
                    <div>
                        <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-3">
                            Email Filter
                        </label>
                        <div className="flex items-center">
                            <input
                                type="checkbox"
                                id="has-email"
                                checked={filters.hasEmail}
                                onChange={(e) => handleFilterChange('hasEmail', e.target.checked)}
                                className="h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500 dark:bg-gray-700 dark:border-gray-600"
                            />
                            <label htmlFor="has-email" className="ml-2 block text-sm text-gray-700 dark:text-gray-300">
                                Has Email
                            </label>
                        </div>
                    </div>
                </div>
                {(filters.name || filters.email || filters.url || filters.hasEmail) && (
                    <div className="mt-4 text-sm text-gray-500 dark:text-gray-400">
                        Active filters: {[
                            filters.name && 'Name',
                            filters.email && 'Email',
                            filters.url && 'URL',
                            filters.hasEmail && 'Has Email'
                        ].filter(Boolean).join(', ')}
                    </div>
                )}
            </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 customers...</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="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 dark:text-gray-200 sm:pl-6"
                                            >
                                                ID
                                            </th>
                                            <th
                                                scope="col"
                                                className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
                                            >
                                                Name
                                            </th>
                                            <th
                                                scope="col"
                                                className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
                                            >
                                                URL
                                            </th>
                                            <th
                                                scope="col"
                                                className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
                                            >
                                                Email
                                            </th>
                                            <th
                                                scope="col"
                                                className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
                                            >
                                                Created
                                            </th>
                                            <th
                                                scope="col"
                                                className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
                                            >
                                                Modified
                                            </th>
                                            <th scope="col" className="relative py-3.5 pl-3 pr-4 sm:pr-6">
                                                <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">
                                        {customers.length > 0 ? (
                                            customers.map((customer) => (
                                                <tr key={customer.id}>
                                                    <td className="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 dark:text-gray-200 sm:pl-6">
                                                        <Link
                                                            href={`/admin/customers/detail/${customer.id}`}
                                                            className="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-300"
                                                        >
                                                            {customer.id.substring(0, 8)}...
                                                        </Link>
                                                    </td>
                                                    <td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
                                                        <Link
                                                            href={`/admin/customers/detail/${customer.id}`}
                                                            className="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-300"
                                                        >
                                                            {customer.name}
                                                        </Link>
                                                    </td>
                                                    <td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
                                                        {customer.url ? (
                                                            <a
                                                                href={customer.url.startsWith('http') ? customer.url : `https://${customer.url}`}
                                                                target="_blank"
                                                                rel="noopener noreferrer"
                                                                className="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-300"
                                                            >
                                                                {customer.url}
                                                            </a>
                                                        ) : (
                                                            <span className="text-gray-400 dark:text-gray-500">-</span>
                                                        )}
                                                    </td>
                                                    <td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
                                                        <a
                                                            href={`mailto:${customer.email}`}
                                                            className="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-300"
                                                        >
                                                            {customer.email}
                                                        </a>
                                                    </td>
                                                    <td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
                                                        {new Date(customer.createdAt).toLocaleDateString()}
                                                    </td>
                                                    <td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
                                                        {new Date(customer.modifiedAt).toLocaleDateString()}
                                                    </td>
                                                    <td className="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6">
                                                        <Link
                                                            href={`/admin/customers/edit/${customer.id}`}
                                                            className="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-300 mr-4"
                                                        >
                                                            Edit
                                                        </Link>
                                                        <DeleteButton customerId={customer.id} customerName={customer.name} />
                                                    </td>
                                                </tr>
                                            ))
                                        ) : (
                                            <tr>
                                                <td colSpan={7} className="py-4 pl-4 pr-3 text-sm text-gray-500 dark:text-gray-400 text-center">
                                                    No customers found. Create your first customer!
                                                </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>
    );
}
