'use client';

import { useEffect, useState } from 'react';
import { useRouter } from 'next/navigation';
import { usersService, User, getErrorMessage, uploadService } from '@/services';

type RoleFilter = 'ALL' | 'ADMIN' | 'USER' | 'AGENT';

const roleFilters: { label: string; value: RoleFilter }[] = [
  { label: 'All Users', value: 'ALL' },
  { label: 'Admin', value: 'ADMIN' },
  { label: 'Users', value: 'USER' },
  { label: 'Agents', value: 'AGENT' },
];

export default function UsersPage() {
  const router = useRouter();
  const [users, setUsers] = useState<User[]>([]);
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState('');
  const [currentPage, setCurrentPage] = useState(1);
  const [totalUsers, setTotalUsers] = useState(0);
  const [activeFilter, setActiveFilter] = useState<RoleFilter>('ALL');
  const [avatarUrls, setAvatarUrls] = useState<Record<string, string>>({});
  const limit = 10;

  // Helper function to check if avatar is an S3 key
  const isS3Key = (avatar: string | null | undefined): boolean => {
    if (!avatar) return false;
    return !avatar.startsWith('http') && !avatar.startsWith('/');
  };

  // Fetch presigned URLs for user avatars
  const fetchAvatarUrls = async (userList: User[]) => {
    const urls: Record<string, string> = {};

    await Promise.all(
      userList.map(async (user) => {
        const avatar = user.profile?.avatar;
        if (avatar) {
          if (isS3Key(avatar)) {
            try {
              const presignedUrl = await uploadService.getPresignedDownloadUrl(avatar);
              urls[user.id] = presignedUrl;
            } catch (err) {
              console.error(`Failed to get avatar URL for user ${user.id}:`, err);
            }
          } else {
            urls[user.id] = avatar;
          }
        }
      })
    );

    setAvatarUrls(urls);
  };

  useEffect(() => {
    fetchUsers();
  }, [currentPage, activeFilter]);

  const fetchUsers = async () => {
    setIsLoading(true);
    setError('');
    try {
      const filters: { page: number; limit: number; role?: string } = { page: currentPage, limit };
      if (activeFilter !== 'ALL') {
        filters.role = activeFilter;
      }
      const response = await usersService.getUsers(filters);
      const userList = response.users || [];
      setUsers(userList);
      setTotalUsers(response.total || 0);

      // Fetch presigned URLs for avatars
      await fetchAvatarUrls(userList);
    } catch (err) {
      const errorMessage = getErrorMessage(err);
      setError(errorMessage);
      if (errorMessage.includes('Unauthorized')) {
        router.push('/login');
      }
    } finally {
      setIsLoading(false);
    }
  };

  const handleFilterChange = (filter: RoleFilter) => {
    setActiveFilter(filter);
    setCurrentPage(1); // Reset to first page when filter changes
  };

  const totalPages = Math.ceil(totalUsers / limit);

  return (
    <div>
      {/* Page Title */}
      <div className="mb-6">
        <h1 className="text-2xl font-bold text-[#00293d] font-fractul">Users</h1>
        <p className="text-[#666666] font-serif">Manage all registered users</p>
      </div>

      {/* Users Table */}
      <div className="bg-white rounded-xl shadow-sm border border-[#e5e7eb]">
        <div className="px-6 py-4 border-b border-[#e5e7eb]">
          <div className="flex justify-between items-center">
            {/* Filter Tabs */}
            <div className="flex space-x-1 bg-[#f5f9f8] rounded-lg p-1">
              {roleFilters.map((filter) => (
                <button
                  key={filter.value}
                  onClick={() => handleFilterChange(filter.value)}
                  className={`px-4 py-2 text-sm font-medium rounded-md transition-colors ${
                    activeFilter === filter.value
                      ? 'bg-white text-[#00293d] shadow-sm'
                      : 'text-[#666666] hover:text-[#00293d]'
                  }`}
                >
                  {filter.label}
                </button>
              ))}
            </div>
            <button
              onClick={fetchUsers}
              className="px-4 py-2 bg-[#f5a623] hover:bg-[#e09620] text-white text-sm font-medium rounded-lg transition-colors flex items-center"
            >
              <svg
                className="w-4 h-4 mr-2"
                fill="none"
                stroke="currentColor"
                viewBox="0 0 24 24"
              >
                <path
                  strokeLinecap="round"
                  strokeLinejoin="round"
                  strokeWidth={2}
                  d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"
                />
              </svg>
              Refresh
            </button>
          </div>
        </div>

        {error && (
          <div className="px-6 py-4 bg-red-50 border-b border-red-200">
            <p className="text-red-700 text-sm">{error}</p>
          </div>
        )}

        <div className="overflow-x-auto">
          {isLoading ? (
            <div className="flex items-center justify-center py-12">
              <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-[#f5a623]"></div>
            </div>
          ) : users.length === 0 ? (
            <div className="text-center py-12">
              <svg
                className="mx-auto h-12 w-12 text-[#9ca3af]"
                fill="none"
                stroke="currentColor"
                viewBox="0 0 24 24"
              >
                <path
                  strokeLinecap="round"
                  strokeLinejoin="round"
                  strokeWidth={2}
                  d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z"
                />
              </svg>
              <p className="mt-2 text-[#666666]">No users found</p>
            </div>
          ) : (
            <table className="min-w-full divide-y divide-[#e5e7eb]">
              <thead className="bg-[#f5f9f8]">
                <tr>
                  <th className="px-6 py-3 text-left text-xs font-medium text-[#666666] uppercase tracking-wider">
                    User
                  </th>
                  <th className="px-6 py-3 text-left text-xs font-medium text-[#666666] uppercase tracking-wider">
                    Role
                  </th>
                  <th className="px-6 py-3 text-left text-xs font-medium text-[#666666] uppercase tracking-wider">
                    Status
                  </th>
                  <th className="px-6 py-3 text-left text-xs font-medium text-[#666666] uppercase tracking-wider">
                    Provider
                  </th>
                  <th className="px-6 py-3 text-left text-xs font-medium text-[#666666] uppercase tracking-wider">
                    Verified
                  </th>
                  <th className="px-6 py-3 text-left text-xs font-medium text-[#666666] uppercase tracking-wider">
                    Joined
                  </th>
                  <th className="px-6 py-3 text-left text-xs font-medium text-[#666666] uppercase tracking-wider">
                    Last Login
                  </th>
                </tr>
              </thead>
              <tbody className="bg-white divide-y divide-[#e5e7eb]">
                {users.map((user) => (
                  <tr
                    key={user.id}
                    className="hover:bg-[#f5f9f8] cursor-pointer"
                    onClick={() => router.push(`/dashboard/users/${user.id}`)}
                  >
                    <td className="px-6 py-4 whitespace-nowrap">
                      <div className="flex items-center">
                        <div className="flex-shrink-0 h-10 w-10">
                          <div className="h-10 w-10 rounded-full bg-[#e8f0ee] flex items-center justify-center relative overflow-hidden">
                            <span className="text-[#666666] font-medium text-sm">
                              {user.profile?.firstName?.[0] || user.email[0].toUpperCase()}
                            </span>
                            {avatarUrls[user.id] && (
                              <img
                                className="absolute inset-0 h-10 w-10 rounded-full object-cover"
                                src={avatarUrls[user.id]}
                                alt=""
                                onError={(e) => { (e.target as HTMLImageElement).style.display = 'none'; }}
                              />
                            )}
                          </div>
                        </div>
                        <div className="ml-4">
                          <div className="text-sm font-medium text-[#00293d]">
                            {user.profile?.firstName} {user.profile?.lastName}
                          </div>
                          <div className="text-sm text-[#666666]">{user.email}</div>
                        </div>
                      </div>
                    </td>
                    <td className="px-6 py-4 whitespace-nowrap">
                      <span
                        className={`inline-flex px-2 py-1 text-xs font-semibold rounded-full ${
                          user.role === 'ADMIN'
                            ? 'bg-red-100 text-red-800'
                            : user.role === 'AGENT'
                            ? 'bg-[#fff7ed] text-[#f5a623]'
                            : 'bg-[#e8f0ee] text-[#5ba4a4]'
                        }`}
                      >
                        {user.role}
                      </span>
                    </td>
                    <td className="px-6 py-4 whitespace-nowrap">
                      <span
                        className={`inline-flex px-2 py-1 text-xs font-semibold rounded-full ${
                          user.status === 'ACTIVE'
                            ? 'bg-green-100 text-green-800'
                            : user.status === 'PENDING_VERIFICATION'
                            ? 'bg-yellow-100 text-yellow-800'
                            : user.status === 'SUSPENDED'
                            ? 'bg-red-100 text-red-800'
                            : 'bg-gray-100 text-gray-800'
                        }`}
                      >
                        {user.status === 'PENDING_VERIFICATION' ? 'PENDING' : user.status}
                      </span>
                    </td>
                    <td className="px-6 py-4 whitespace-nowrap text-sm text-[#666666]">
                      {user.authProvider === 'LOCAL' ? 'Email' : user.authProvider}
                    </td>
                    <td className="px-6 py-4 whitespace-nowrap">
                      {user.emailVerified ? (
                        <svg
                          className="w-5 h-5 text-green-500"
                          fill="currentColor"
                          viewBox="0 0 20 20"
                        >
                          <path
                            fillRule="evenodd"
                            d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z"
                            clipRule="evenodd"
                          />
                        </svg>
                      ) : (
                        <svg
                          className="w-5 h-5 text-[#9ca3af]"
                          fill="currentColor"
                          viewBox="0 0 20 20"
                        >
                          <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>
                      )}
                    </td>
                    <td className="px-6 py-4 whitespace-nowrap text-sm text-[#666666]">
                      {new Date(user.createdAt).toLocaleDateString()}
                    </td>
                    <td className="px-6 py-4 whitespace-nowrap text-sm text-[#666666]">
                      {user.lastLoginAt
                        ? new Date(user.lastLoginAt).toLocaleDateString()
                        : 'Never'}
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          )}
        </div>

        {/* Pagination */}
        {totalPages > 1 && (
          <div className="px-6 py-4 border-t border-[#e5e7eb] flex items-center justify-between">
            <p className="text-sm text-[#666666]">
              Showing {(currentPage - 1) * limit + 1} to{' '}
              {Math.min(currentPage * limit, totalUsers)} of {totalUsers} users
            </p>
            <div className="flex space-x-2">
              <button
                onClick={() => setCurrentPage((p) => Math.max(1, p - 1))}
                disabled={currentPage === 1}
                className="px-3 py-1 border border-[#e5e7eb] rounded-md text-sm font-medium text-[#00293d] hover:bg-[#f5f9f8] disabled:opacity-50 disabled:cursor-not-allowed"
              >
                Previous
              </button>
              <button
                onClick={() => setCurrentPage((p) => Math.min(totalPages, p + 1))}
                disabled={currentPage === totalPages}
                className="px-3 py-1 border border-[#e5e7eb] rounded-md text-sm font-medium text-[#00293d] hover:bg-[#f5f9f8] disabled:opacity-50 disabled:cursor-not-allowed"
              >
                Next
              </button>
            </div>
          </div>
        )}
      </div>
    </div>
  );
}
