import { useEffect } from 'react'
import useStore from '../../store.js'
import { Link } from 'react-router-dom'

function AdminDashboard() {
  const { dashboardStats, getDashboardStats, authUser } = useStore()

  useEffect(() => {
    getDashboardStats()
  }, [])

  const formatCurrency = (amount) => {
    return new Intl.NumberFormat('en-US', {
      style: 'currency',
      currency: 'USD'
    }).format(amount || 0)
  }

  const formatDate = (date) => {
    return new Date(date).toLocaleDateString()
  }

  return (
    <div className="min-h-screen bg-gray-50">
      <div className="bg-gradient-to-r from-primary-600 to-primary-800 text-white">
        <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-10">
          <h1 className="text-3xl font-bold">Welcome, {authUser}</h1>
          <p className="text-primary-100 mt-1">Admin Dashboard</p>
        </div>
      </div>

      <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
        {/* Stats Grid */}
        <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-5 mb-8">
          <div className="stat-card">
            <p className="text-sm font-medium text-gray-500">Total Clients</p>
            <p className="text-3xl font-bold text-gray-900 mt-2">{dashboardStats?.totalClients || 0}</p>
          </div>
          <div className="stat-card">
            <p className="text-sm font-medium text-gray-500">Total Cars</p>
            <p className="text-3xl font-bold text-gray-900 mt-2">{dashboardStats?.totalCars || 0}</p>
          </div>
          <div className="stat-card">
            <p className="text-sm font-medium text-gray-500">Available Cars</p>
            <p className="text-3xl font-bold text-green-600 mt-2">{dashboardStats?.availableCars || 0}</p>
          </div>
          <div className="stat-card">
            <p className="text-sm font-medium text-gray-500">Rented Cars</p>
            <p className="text-3xl font-bold text-amber-600 mt-2">{dashboardStats?.rentedCars || 0}</p>
          </div>
          <div className="stat-card">
            <p className="text-sm font-medium text-gray-500">Active Rentals</p>
            <p className="text-3xl font-bold text-blue-600 mt-2">{dashboardStats?.activeRentals || 0}</p>
          </div>
          <div className="stat-card">
            <p className="text-sm font-medium text-gray-500">Returned Rentals</p>
            <p className="text-3xl font-bold text-purple-600 mt-2">{dashboardStats?.returnedRentals || 0}</p>
          </div>
          <div className="stat-card">
            <p className="text-sm font-medium text-gray-500">Total Revenue</p>
            <p className="text-3xl font-bold text-gray-900 mt-2">{formatCurrency(dashboardStats?.totalRevenue)}</p>
          </div>
        </div>

        {/* Recent Activity */}
        <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
          {/* Recent Rentals */}
          <div className="card">
            <div className="p-5 border-b border-gray-100">
              <h2 className="text-lg font-semibold text-gray-900">Recent Rentals</h2>
            </div>
            <div className="divide-y divide-gray-100">
              {dashboardStats?.recentRentals?.length > 0 ? (
                dashboardStats.recentRentals.map((rental) => (
                  <div key={rental._id} className="p-4 flex items-center justify-between">
                    <div className="flex items-center gap-3">
                      <div className="w-10 h-10 bg-blue-50 rounded-lg flex items-center justify-center">
                        <svg className="w-5 h-5 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                          <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 7h12m0 0l-4-4m4 4l-4 4M9 17V7m0 10H5a2 2 0 01-2-2V5a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-5m-6 0h6" />
                        </svg>
                      </div>
                      <div>
                        <p className="font-medium text-gray-900">{rental.car?.brand} {rental.car?.model}</p>
                        <p className="text-sm text-gray-500">{rental.client?.fullName}</p>
                      </div>
                    </div>
                    <span className={`px-2.5 py-1 rounded-full text-xs font-medium ${
                      rental.status === 'active' ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-700'
                    }`}>
                      {rental.status}
                    </span>
                  </div>
                ))
              ) : (
                <div className="p-8 text-center text-gray-500">No rentals yet</div>
              )}
            </div>
          </div>

          {/* Recent Clients */}
          <div className="card">
            <div className="p-5 border-b border-gray-100">
              <h2 className="text-lg font-semibold text-gray-900">Recent Clients</h2>
            </div>
            <div className="divide-y divide-gray-100">
              {dashboardStats?.recentClients?.length > 0 ? (
                dashboardStats.recentClients.map((client) => (
                  <div key={client._id} className="p-4 flex items-center justify-between">
                    <div className="flex items-center gap-3">
                      <div className="w-10 h-10 bg-primary-100 rounded-full flex items-center justify-center">
                        <span className="text-sm font-semibold text-primary-700">
                          {client.fullName.charAt(0).toUpperCase()}
                        </span>
                      </div>
                      <div>
                        <p className="font-medium text-gray-900">{client.fullName}</p>
                        <p className="text-sm text-gray-500">{client.email}</p>
                      </div>
                    </div>
                    <span className="text-sm text-gray-400">{formatDate(client.createdAt)}</span>
                  </div>
                ))
              ) : (
                <div className="p-8 text-center text-gray-500">No clients yet</div>
              )}
            </div>
          </div>
        </div>

        {/* Quick Links */}
        <div className="grid grid-cols-1 sm:grid-cols-3 gap-4 mt-6">
          <Link to="/admin/clients" className="card p-5 flex items-center gap-4 hover:border-primary-300">
            <div className="w-11 h-11 bg-amber-50 rounded-xl flex items-center justify-center">
              <svg className="w-5 h-5 text-amber-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <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>
              <p className="font-semibold text-gray-900">Manage Clients</p>
              <p className="text-sm text-gray-500">View and edit clients</p>
            </div>
          </Link>
          <Link to="/admin/cars" className="card p-5 flex items-center gap-4 hover:border-primary-300">
            <div className="w-11 h-11 bg-blue-50 rounded-xl flex items-center justify-center">
              <svg className="w-5 h-5 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 7h12m0 0l-4-4m4 4l-4 4M9 17V7m0 10H5a2 2 0 01-2-2V5a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-5m-6 0h6" />
              </svg>
            </div>
            <div>
              <p className="font-semibold text-gray-900">Manage Cars</p>
              <p className="text-sm text-gray-500">Add, edit, or remove cars</p>
            </div>
          </Link>
          <Link to="/admin/rentals" className="card p-5 flex items-center gap-4 hover:border-primary-300">
            <div className="w-11 h-11 bg-green-50 rounded-xl flex items-center justify-center">
              <svg className="w-5 h-5 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" />
              </svg>
            </div>
            <div>
              <p className="font-semibold text-gray-900">Manage Rentals</p>
              <p className="text-sm text-gray-500">Track rentals and returns</p>
            </div>
          </Link>
        </div>
      </div>
    </div>
  )
}

export default AdminDashboard