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

function ClientDashboard() {
  const { clientDashboardStats, getClientDashboardStats, authUser } = useStore()

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

  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">Client 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-3 gap-5 mb-8">
          <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">{clientDashboardStats?.availableCarsCount || 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">{clientDashboardStats?.activeRentalsCount || 0}</p>
          </div>
          <div className="stat-card">
            <p className="text-sm font-medium text-gray-500">Rental History</p>
            <p className="text-3xl font-bold text-purple-600 mt-2">{clientDashboardStats?.totalRentalsCount || 0}</p>
          </div>
        </div>

        {/* Quick Links */}
        <div className="grid grid-cols-1 sm:grid-cols-2 gap-4 mb-8">
          <Link to="/client/browse" 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">Browse Available Cars</p>
              <p className="text-sm text-gray-500">View and rent cars</p>
            </div>
          </Link>
          <Link to="/client/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">My Rentals</p>
              <p className="text-sm text-gray-500">View and manage your rentals</p>
            </div>
          </Link>
        </div>

        {/* Recent Rentals */}
        <div className="card">
          <div className="p-5 border-b border-gray-100">
            <h2 className="text-lg font-semibold text-gray-900">My Recent Rentals</h2>
          </div>
          <div className="divide-y divide-gray-100">
            {clientDashboardStats?.myRecentRentals?.length > 0 ? (
              clientDashboardStats.myRecentRentals.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">Due: {formatDate(rental.expectedReturnDate)}</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. Browse cars to get started!</div>
            )}
          </div>
        </div>
      </div>
    </div>
  )
}

export default ClientDashboard