'use client';

import { X } from 'lucide-react';
import Image from 'next/image';

export default function PatientModal({ patient, onClose }) {
  if (!patient) return null;

  const fullName = `${patient.firstName} ${patient.middleName || ''} ${patient.lastName}`.trim();
  const avatarUrl = patient?.avatar
    ? patient.avatar
    : '/images/patient1.jpg';

  return (
    <div
      className="fixed inset-0 bg-white/30 backdrop-blur-sm flex items-center justify-center z-50 px-4"
      onClick={onClose}
    >
      <div
        onClick={(e) => e.stopPropagation()}
        className="bg-white rounded-xl w-full max-w-5xl shadow-2xl relative overflow-y-auto max-h-[90vh] animate-fade-in"
      >
        <button
          onClick={onClose}
          className="absolute top-4 right-4 text-gray-500 hover:text-black"
        >
          <X size={24} />
        </button>

        <div className="p-6">
          <div className="mb-4">
            <h2 className="text-2xl font-bold text-gray-800">Patient Profile</h2>
            <p className="text-gray-500">View and manage personal health information.</p>
          </div>

          {/* Banner */}
          <div className="bg-gray-100 rounded-lg overflow-hidden mb-6">
            <div className="relative h-48">
              <Image
                src="/images/patient-banner.jpg"
                alt="Banner"
                width={1200}
                height={192}
                className="object-cover w-full h-full"
              />
              <div className="absolute bottom-4 left-6 text-white drop-shadow">
                <h3 className="text-xl font-bold">{fullName}</h3>
                <p className="text-sm">Patient ID: #{patient.id}</p>
              </div>
            </div>
            {/* <div className="flex justify-end gap-8 px-6 py-4 bg-white">
              <div className="text-center">
                <p className="text-gray-500 text-sm">Appointments</p>
                <p className="text-lg font-semibold">15</p>
              </div>
              <div className="text-center">
                <p className="text-gray-500 text-sm">Reports</p>
                <p className="text-lg font-semibold">8</p>
              </div>
              <div className="text-center">
                <p className="text-gray-500 text-sm">Tests</p>
                <p className="text-lg font-semibold">4</p>
              </div>
            </div> */}
          </div>

          <div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
            {/* Sidebar Info */}
            <div className="space-y-6">
              <div className="bg-white rounded-xl shadow p-4">
                <h4 className="text-lg font-semibold text-gray-800 mb-2">Health Conditions</h4>
                <ul className="list-disc list-inside text-sm text-gray-700 space-y-1">
                  {patient.medicalHistory ? (
                    patient.medicalHistory.split(',').map((item, index) => (
                      <li key={index}>{item.trim()}</li>
                    ))
                  ) : (
                    <li>No medical history provided</li>
                  )}
                  {patient.allergies && (
                    <li className="text-red-600">Allergies: {patient.allergies}</li>
                  )}
                </ul>
              </div>

              <div className="bg-white rounded-xl shadow p-4">
                <h4 className="text-lg font-semibold text-gray-800 mb-2">Emergency Contact</h4>
                <div className="text-sm text-gray-700 space-y-1">
                  <p><span className="font-medium">Name:</span> {patient.emergencyContactName || 'N/A'}</p>
                  <p><span className="font-medium">Relationship:</span> {patient.emergencyContactRelationship || 'N/A'}</p>
                  <p><span className="font-medium">Phone:</span> {patient.emergencyContactPhone || 'N/A'}</p>
                </div>
              </div>

              <div className="bg-white rounded-xl shadow p-4">
                <h4 className="text-lg font-semibold text-gray-800 mb-2">Insurance Information</h4>
                <div className="text-sm text-gray-700 space-y-1">
                  <p><span className="font-medium">Provider:</span> {patient.insuranceProvider || 'N/A'}</p>
                  <p><span className="font-medium">Number:</span> {patient.insuranceNumber || 'N/A'}</p>
                </div>
              </div>

              <div className="bg-white rounded-xl shadow p-4">
                <h4 className="text-lg font-semibold text-gray-800 mb-2">Social History</h4>
                <ul className="text-sm text-gray-700 space-y-1 list-disc list-inside">
                  <li>Smokes: {patient.smokes ? 'Yes' : 'No'}</li>
                  <li>Drinks Alcohol: {patient.drinksAlcohol ? 'Yes' : 'No'}</li>
                  <li>Uses Drugs: {patient.usesDrugs ? 'Yes' : 'No'}</li>
                </ul>
              </div>
            </div>

            {/* Main Info */}
            <div className="lg:col-span-2 bg-white rounded-xl shadow p-6">
              <h4 className="text-lg font-semibold text-gray-800 mb-4">Personal Information</h4>
              <div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4 text-sm text-gray-700">
                <div className="col-span-full mb-4 flex justify-center">
                  <img
                    src={avatarUrl}
                    alt="Avatar"
                    width={112}
                    height={112}
                    className="w-28 h-28 rounded-full object-cover shadow-md"
                  />
                </div>
                <div>
                  <p className="font-medium text-gray-600">Full Name</p>
                  <p>{fullName}</p>
                </div>
                <div>
                  <p className="font-medium text-gray-600">Mobile</p>
                  <p>{patient.phone || 'N/A'}</p>
                </div>
                <div>
                  <p className="font-medium text-gray-600">Email</p>
                  <p>{patient.email || 'N/A'}</p>
                </div>
                <div>
                  <p className="font-medium text-gray-600">Location</p>
                  <p>{patient.address || 'Not specified'}</p>
                </div>
                <div>
                  <p className="font-medium text-gray-600">Gender</p>
                  <p>{patient.gender || 'Not specified'}</p>
                </div>
                <div>
                  <p className="font-medium text-gray-600">Date of Birth</p>
                  <p>{patient.dateOfBirth ? new Date(patient.dateOfBirth).toLocaleDateString() : 'Not specified'}</p>
                </div>
                <div>
                  <p className="font-medium text-gray-600">Blood Type</p>
                  <p>{patient.bloodType || 'N/A'}</p>
                </div>
                <div>
                  <p className="font-medium text-gray-600">Height (cm)</p>
                  <p>{patient.height || 'N/A'}</p>
                </div>
                <div>
                  <p className="font-medium text-gray-600">Weight (kg)</p>
                  <p>{patient.weight || 'N/A'}</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}
