import React, { useEffect, useState } from "react";
import { useParams, Link } from "react-router-dom";
import axios from "../../app/api";
import { ArrowLeft, Mail, Shield, User as UserIcon, Activity, Clock } from "lucide-react";
import StatusBadge from "./StatusBadge";
import { formatDate } from "../../utils/formatDate";

const UserDetails = () => {
  const { id } = useParams();
  const [user, setUser] = useState(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    axios
      .get(`/admin/users/${id}`)
      .then((res) => {
        setUser(res.data.data.user);
        setLoading(false);
      })
      .catch((err) => {
        console.error(err);
        setLoading(false);
      });
  }, [id]);

  if (loading)
    return (
      <div className="flex justify-center items-center h-64 text-gray-600">
        Loading user details...
      </div>
    );

  if (!user)
    return (
      <div className="text-center mt-10 text-gray-600">User not found!</div>
    );

  return (
    <div className="max-w-5xl mx-auto space-y-8">
      {/* Back Button */}
      <div className="flex items-center space-x-2 text-blue-600 hover:text-blue-800">
        <ArrowLeft size={18} />
        <Link to="/users" className="font-medium">
          Back to Users
        </Link>
      </div>

      {/* Header Card */}
      <div className="flex flex-col md:flex-row items-start md:items-center justify-between bg-white shadow-md rounded-2xl p-6 border border-gray-100">
        <div className="flex items-center gap-4">
          <div className="w-16 h-16 rounded-full bg-gradient-to-br from-blue-500 to-indigo-600 flex items-center justify-center text-white text-2xl font-semibold uppercase">
            {user.firstName?.[0] || "U"}
          </div>
          <div>
            <h2 className="text-2xl font-bold text-gray-800">
              {user.firstName} {user.lastName}
            </h2>
            <div className="flex items-center text-gray-500 text-sm space-x-2 mt-1">
              <Mail size={16} className="text-blue-500" />
              <span>{user.email}</span>
            </div>
          </div>
        </div>

        <div className="mt-4 md:mt-0">
          <StatusBadge
            value={user.isActive}
            trueText="Active"
            falseText="Inactive"
          />
        </div>
      </div>

      {/* Details Section */}
      <div className="grid md:grid-cols-2 gap-6">
        {/* Left - User Info */}
        <div className="bg-white shadow-sm rounded-xl p-6 border border-gray-100 space-y-3">
          <h3 className="text-lg font-semibold text-gray-700 border-b pb-2">
            User Information
          </h3>
          <InfoRow icon={<UserIcon />} label="Username" value={user.username} />
          <InfoRow icon={<Shield />} label="Role" value={user.role} />
          <InfoRow
            label="Verified"
            value={
              <StatusBadge
                value={user.isVerified}
                trueText="Yes"
                falseText="No"
              />
            }
          />
          <InfoRow
            label="Email Verified"
            value={user.isVerified ? "Yes" : "No"}
          />
          <InfoRow
            label="Created At"
            value={formatDate(user.createdAt)}
          />
        </div>

        {/* Right - Activity Info */}
        <div className="bg-white shadow-sm rounded-xl p-6 border border-gray-100 space-y-3">
          <h3 className="text-lg font-semibold text-gray-700 border-b pb-2">
            Account Activity
          </h3>
          <InfoRow
            icon={<Activity />}
            label="Last Login"
            value={formatDate(user.lastLoginAt)}
          />
          <InfoRow
            icon={<Clock />}
            label="Last Updated"
            value={formatDate(user.updatedAt || user.lastLoginAt)}
          />
          <InfoRow label="Account Status" value={user.isActive ? "Active" : "Inactive"} />
          <InfoRow label="Role" value={user.role} />
        </div>
      </div>

      {/* Action Buttons */}
      <div className="flex justify-end gap-4 pt-6">
        <button className="px-5 py-2.5 bg-gradient-to-r from-blue-500 to-indigo-600 text-white rounded-lg font-medium shadow-md hover:shadow-lg transition-all">
          Edit User
        </button>
        <button className="px-5 py-2.5 bg-gray-100 text-gray-700 rounded-lg font-medium hover:bg-red-50 hover:text-red-600 transition-all">
          Delete
        </button>
      </div>
    </div>
  );
};

// Reusable Info Row
const InfoRow = ({ icon, label, value }) => (
  <div className="flex justify-between items-center text-sm text-gray-700">
    <div className="flex items-center gap-2 text-gray-600">
      {icon && <span className="text-blue-500">{icon}</span>}
      <span className="font-medium">{label}:</span>
    </div>
    <span className="text-gray-800 font-medium">{value || "—"}</span>
  </div>
);

export default UserDetails;
