'use client';

import React from 'react';
import {
  X,
  User,
  Building,
  Phone,
  Mail,
  MapPin,
  ShieldCheck,
  Coins,
  KeyRound,
  Clock,
  Calendar,
  CheckCircle2,
} from 'lucide-react';

interface UserProfileDrawerProps {
  user: any;
  onClose: () => void;
}

export default function UserProfileDrawer({ user, onClose }: UserProfileDrawerProps) {
  if (!user) return null;

  return (
    <div className="fixed inset-0 bg-slate-900/50 backdrop-blur-sm z-50 flex items-center justify-center p-4">
      <div className="bg-white rounded-2xl shadow-2xl border border-slate-200 max-w-2xl w-full p-6 sm:p-7 space-y-6 animate-in fade-in zoom-in-95 duration-150 max-h-[90vh] overflow-y-auto">
        <div className="flex items-center justify-between border-b border-slate-100 pb-4">
          <div>
            <span className="text-[10.5px] font-mono font-bold text-[#00875a] uppercase tracking-wider block">
              INDIVIDUAL USER PROFILE
            </span>
            <h3 className="text-lg font-bold text-slate-900 mt-0.5">
              {user.name} ({user.userCode})
            </h3>
          </div>
          <button
            onClick={onClose}
            className="text-slate-400 hover:text-slate-600 p-1.5 rounded-lg hover:bg-slate-100"
          >
            <X className="w-5 h-5" />
          </button>
        </div>

        {/* Identity & Account Grid */}
        <div className="grid grid-cols-1 sm:grid-cols-2 gap-4 p-4 rounded-xl bg-slate-50 border border-slate-200/80 text-xs">
          <div>
            <span className="text-slate-500 font-mono block">Full Name</span>
            <span className="text-slate-900 font-bold text-sm">{user.name}</span>
          </div>
          <div>
            <span className="text-slate-500 font-mono block">User ID (Permanent)</span>
            <span className="text-[#00875a] font-mono font-bold text-sm">{user.userCode}</span>
          </div>
          <div>
            <span className="text-slate-500 font-mono block">Email Address</span>
            <span className="text-slate-800 font-mono">{user.email}</span>
          </div>
          <div>
            <span className="text-slate-500 font-mono block">Phone Number</span>
            <span className="text-slate-800 font-mono">{user.phone || 'N/A'}</span>
          </div>
          <div>
            <span className="text-slate-500 font-mono block">National ID (NIN Masked)</span>
            <span className="text-emerald-700 font-mono font-bold">{user.nin || 'Not Provided'}</span>
          </div>
          <div>
            <span className="text-slate-500 font-mono block">Account Status</span>
            <span
              className={`inline-block px-2 py-0.5 rounded-full text-[10px] font-bold ${
                user.status === 'ACTIVE'
                  ? 'bg-emerald-100 text-emerald-800'
                  : 'bg-rose-100 text-rose-800'
              }`}
            >
              {user.status}
            </span>
          </div>
          <div>
            <span className="text-slate-500 font-mono block">Role</span>
            <span className="font-mono font-bold text-slate-800">
              {user.role === 'STANDARD_USER' ? 'User (Standard)' : user.role.replace('_', ' ')}
            </span>
          </div>
          <div>
            <span className="text-slate-500 font-mono block">Registration Date</span>
            <span className="text-slate-700 font-mono">{new Date(user.createdAt).toLocaleDateString()}</span>
          </div>
          <div className="sm:col-span-2">
            <span className="text-slate-500 font-mono block">Address</span>
            <span className="text-slate-700">{user.address || 'N/A'}</span>
          </div>
        </div>

        {/* Units Account Statistics */}
        <div className="space-y-2">
          <h4 className="text-xs font-mono font-bold text-slate-700 uppercase tracking-wider flex items-center gap-1.5">
            <Coins className="w-4 h-4 text-amber-600" />
            <span>Units Accounting Statistics</span>
          </h4>
          <div className="grid grid-cols-3 gap-3 text-center">
            <div className="p-3.5 bg-amber-50/60 rounded-xl border border-amber-200">
              <span className="text-[10px] text-amber-800 font-bold uppercase">Available Units</span>
              <div className="text-xl font-extrabold text-amber-900 font-mono mt-0.5">
                {Number(user.wallet?.unitsBalance ?? 0).toLocaleString()}
              </div>
            </div>
            <div className="p-3.5 bg-slate-50 rounded-xl border border-slate-200">
              <span className="text-[10px] text-slate-500 font-semibold uppercase">Purchased Units</span>
              <div className="text-xl font-bold text-slate-900 font-mono mt-0.5">
                {Number(user.wallet?.totalUnitsPurchased ?? 0).toLocaleString()}
              </div>
            </div>
            <div className="p-3.5 bg-slate-50 rounded-xl border border-slate-200">
              <span className="text-[10px] text-slate-500 font-semibold uppercase">Consumed Units</span>
              <div className="text-xl font-bold text-slate-700 font-mono mt-0.5">
                {Number(user.wallet?.totalUnitsUsed ?? 0).toLocaleString()}
              </div>
            </div>
          </div>
        </div>

        {/* Verification & API Usage History */}
        <div className="space-y-2">
          <h4 className="text-xs font-mono font-bold text-slate-700 uppercase tracking-wider flex items-center gap-1.5">
            <ShieldCheck className="w-4 h-4 text-[#00875a]" />
            <span>Recent Identity Verification Queries ({user.verificationRequests?.length || 0})</span>
          </h4>
          <div className="bg-slate-50 rounded-xl border border-slate-200 p-3 max-h-40 overflow-y-auto space-y-1.5 text-xs">
            {(user.verificationRequests || []).length === 0 ? (
              <div className="text-slate-400 text-center py-2 text-xs">No verification requests logged.</div>
            ) : (
              user.verificationRequests.map((vr: any) => (
                <div key={vr.id} className="flex justify-between items-center text-[11px] font-mono p-1.5 bg-white rounded-lg border border-slate-100">
                  <span className="text-slate-700 font-bold">{vr.requestId}</span>
                  <span className="text-[#00875a]">{vr.verificationType} ({vr.status})</span>
                  <span className="text-slate-400">{new Date(vr.createdAt).toLocaleTimeString()}</span>
                </div>
              ))
            )}
          </div>
        </div>

        <div className="flex justify-end pt-2 border-t border-slate-100">
          <button
            onClick={onClose}
            className="px-5 py-2.5 bg-slate-800 hover:bg-slate-700 text-white font-bold text-xs rounded-xl transition-all"
          >
            Close Inspector
          </button>
        </div>
      </div>
    </div>
  );
}
