import { useMyDashboard, useStructures } from '@/hooks/useMlm';

export default function MLMPage() {
  const { data: dashboard, isLoading } = useMyDashboard();
  const { data: structures } = useStructures({ isActive: true });

  if (isLoading) {
    return (
      <div className="flex items-center justify-center min-h-[400px]">
        <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600"></div>
      </div>
    );
  }

  return (
    <div className="space-y-6">
      <div className="flex justify-between items-center">
        <h1 className="text-2xl font-semibold text-gray-900">MLM Dashboard</h1>
      </div>

      {/* Summary Cards */}
      <div className="grid grid-cols-1 gap-5 sm:grid-cols-2 lg:grid-cols-4">
        <div className="bg-white overflow-hidden shadow rounded-lg">
          <div className="p-5">
            <div className="flex items-center">
              <div className="flex-shrink-0">
                <svg className="h-6 w-6 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z" />
                </svg>
              </div>
              <div className="ml-5 w-0 flex-1">
                <dl>
                  <dt className="text-sm font-medium text-gray-500 truncate">Total Downline</dt>
                  <dd className="text-lg font-semibold text-gray-900">{dashboard?.totalDownline ?? 0}</dd>
                </dl>
              </div>
            </div>
          </div>
        </div>

        <div className="bg-white overflow-hidden shadow rounded-lg">
          <div className="p-5">
            <div className="flex items-center">
              <div className="flex-shrink-0">
                <svg className="h-6 w-6 text-blue-500" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M18 9v3m0 0v3m0-3h3m-3 0h-3m-2-5a4 4 0 11-8 0 4 4 0 018 0zM3 20a6 6 0 0112 0v1H3v-1z" />
                </svg>
              </div>
              <div className="ml-5 w-0 flex-1">
                <dl>
                  <dt className="text-sm font-medium text-gray-500 truncate">Direct Referrals</dt>
                  <dd className="text-lg font-semibold text-gray-900">{dashboard?.directReferrals ?? 0}</dd>
                </dl>
              </div>
            </div>
          </div>
        </div>

        <div className="bg-white overflow-hidden shadow rounded-lg">
          <div className="p-5">
            <div className="flex items-center">
              <div className="flex-shrink-0">
                <svg className="h-6 w-6 text-purple-500" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z" />
                </svg>
              </div>
              <div className="ml-5 w-0 flex-1">
                <dl>
                  <dt className="text-sm font-medium text-gray-500 truncate">Group Volume</dt>
                  <dd className="text-lg font-semibold text-gray-900">
                    {(dashboard?.groupVolume ?? 0).toLocaleString()}
                  </dd>
                </dl>
              </div>
            </div>
          </div>
        </div>

        <div className="bg-white overflow-hidden shadow rounded-lg">
          <div className="p-5">
            <div className="flex items-center">
              <div className="flex-shrink-0">
                <svg className="h-6 w-6 text-green-500" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                </svg>
              </div>
              <div className="ml-5 w-0 flex-1">
                <dl>
                  <dt className="text-sm font-medium text-gray-500 truncate">Total Earnings</dt>
                  <dd className="text-lg font-semibold text-gray-900">
                    ${(dashboard?.totalEarnings ?? 0).toLocaleString()}
                  </dd>
                </dl>
              </div>
            </div>
          </div>
        </div>
      </div>

      {/* Stats Grid */}
      <div className="grid grid-cols-1 gap-5 lg:grid-cols-2">
        {/* Current Rank */}
        <div className="bg-white shadow rounded-lg p-6">
          <h3 className="text-lg font-medium text-gray-900 mb-4">Current Rank</h3>
          {dashboard?.currentRank ? (
            <div className="flex items-center space-x-4">
              <div className="w-16 h-16 rounded-full bg-gradient-to-br from-yellow-400 to-yellow-600 flex items-center justify-center">
                <span className="text-2xl font-bold text-white">{dashboard.currentRank.level}</span>
              </div>
              <div>
                <p className="text-xl font-semibold text-gray-900">{dashboard.currentRank.name}</p>
                <p className="text-sm text-gray-500">Level {dashboard.currentRank.level}</p>
              </div>
            </div>
          ) : (
            <p className="text-sm text-gray-500">No rank assigned yet</p>
          )}
        </div>

        {/* Next Rank Progress */}
        <div className="bg-white shadow rounded-lg p-6">
          <h3 className="text-lg font-medium text-gray-900 mb-4">Next Rank Progress</h3>
          {dashboard?.nextRank ? (
            <div className="space-y-3">
              <div className="flex justify-between items-center">
                <span className="text-sm font-medium text-gray-900">{dashboard.nextRank.name}</span>
                <span className="text-xs text-gray-500">Level {dashboard.nextRank.level}</span>
              </div>
              {Object.entries(dashboard.nextRank.progress || {}).map(([key, value]) => (
                <div key={key}>
                  <div className="flex justify-between text-xs text-gray-500 mb-1">
                    <span className="capitalize">{key.replace(/([A-Z])/g, ' $1').trim()}</span>
                    <span>{Math.min(100, Math.round(Number(value)))}%</span>
                  </div>
                  <div className="w-full bg-gray-200 rounded-full h-2">
                    <div
                      className="bg-blue-600 h-2 rounded-full"
                      style={{ width: `${Math.min(100, Number(value))}%` }}
                    ></div>
                  </div>
                </div>
              ))}
            </div>
          ) : (
            <p className="text-sm text-gray-500">You've reached the highest rank!</p>
          )}
        </div>
      </div>

      {/* Volume Stats */}
      <div className="bg-white shadow rounded-lg p-6">
        <h3 className="text-lg font-medium text-gray-900 mb-4">Volume Summary</h3>
        <div className="grid grid-cols-1 gap-4 sm:grid-cols-3">
          <div className="flex justify-between p-3 bg-gray-50 rounded-lg">
            <span className="text-sm text-gray-500">Personal Volume</span>
            <span className="text-sm font-medium text-gray-900">
              {(dashboard?.personalVolume ?? 0).toLocaleString()}
            </span>
          </div>
          <div className="flex justify-between p-3 bg-gray-50 rounded-lg">
            <span className="text-sm text-gray-500">Group Volume</span>
            <span className="text-sm font-medium text-gray-900">
              {(dashboard?.groupVolume ?? 0).toLocaleString()}
            </span>
          </div>
          <div className="flex justify-between p-3 bg-gray-50 rounded-lg">
            <span className="text-sm text-gray-500">Active Downline</span>
            <span className="text-sm font-medium text-gray-900">{dashboard?.activeDownline ?? 0}</span>
          </div>
        </div>
      </div>

      {/* Active Structures */}
      <div className="bg-white shadow rounded-lg p-6">
        <h3 className="text-lg font-medium text-gray-900 mb-4">Active Structures</h3>
        <div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
          {structures?.map((structure) => (
            <div key={structure.id} className="border border-gray-200 rounded-lg p-4 hover:shadow-md transition-shadow">
              <div className="flex items-start justify-between">
                <div>
                  <h4 className="font-medium text-gray-900">{structure.name}</h4>
                  <p className="text-sm text-gray-500 capitalize">{structure.type}</p>
                </div>
                <span className="px-2 py-1 text-xs font-semibold rounded-full bg-green-100 text-green-800">
                  Active
                </span>
              </div>
              {structure.description && (
                <p className="mt-2 text-sm text-gray-600 line-clamp-2">{structure.description}</p>
              )}
            </div>
          ))}
          {(!structures || structures.length === 0) && (
            <p className="text-sm text-gray-500 col-span-full">No active structures</p>
          )}
        </div>
      </div>

      {/* Quick Links */}
      <div className="bg-white shadow rounded-lg p-6">
        <h3 className="text-lg font-medium text-gray-900 mb-4">Quick Actions</h3>
        <div className="grid grid-cols-2 gap-4 sm:grid-cols-5">
          <a
            href="/dashboard/mlm/my-network"
            className="flex items-center p-4 border border-gray-200 rounded-lg hover:bg-gray-50"
          >
            <span className="text-sm font-medium text-gray-900">My Network</span>
          </a>
          <a
            href="/dashboard/mlm/structures"
            className="flex items-center p-4 border border-gray-200 rounded-lg hover:bg-gray-50"
          >
            <span className="text-sm font-medium text-gray-900">Structures</span>
          </a>
          <a
            href="/dashboard/mlm/ranks"
            className="flex items-center p-4 border border-gray-200 rounded-lg hover:bg-gray-50"
          >
            <span className="text-sm font-medium text-gray-900">Ranks</span>
          </a>
          <a
            href="/dashboard/commissions/my-earnings"
            className="flex items-center p-4 border border-gray-200 rounded-lg hover:bg-gray-50"
          >
            <span className="text-sm font-medium text-gray-900">My Earnings</span>
          </a>
          <a
            href="/dashboard/mlm/my-network"
            className="flex items-center p-4 border border-blue-200 bg-blue-50 rounded-lg hover:bg-blue-100"
          >
            <span className="text-sm font-medium text-blue-900">+ Invite</span>
          </a>
        </div>
      </div>
    </div>
  );
}
