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

function Dashboard() {
  const { authUser, authRole, authId, books, borrows, members, getBooks, getBorrows, getMembers } = useStore()

  useEffect(() => {
    getBooks()
    getBorrows()
    getMembers()
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])

  const isLibrarian = authRole === 'librarian'

  // Calculate stats
  const availableBooks = books.length
  const totalBorrows = borrows.length

  // For students, filter their borrowed books
  const myBorrows = isLibrarian
    ? totalBorrows
    : borrows.filter(borrow => {
        const clientId = borrow.clientId?._id || borrow.clientId
        return clientId === authId
      }).length

  // For students, filter active borrows count
  const activeBorrowsForStudent = borrows.filter(borrow => {
    const clientId = borrow.clientId?._id || borrow.clientId
    return clientId === authId
  })

  return (
    <div className="min-h-screen bg-gray-50">
      {/* Header */}
      <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">
          <div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
            <div>
              <p className="text-primary-200 text-sm font-medium uppercase tracking-wider">Dashboard</p>
              <h1 className="text-3xl sm:text-4xl font-bold mt-1">Welcome, {authUser}</h1>
              <p className="text-primary-100 mt-1 capitalize">{authRole} Portal</p>
            </div>
            <div className="flex gap-3">
              <Link
                to="/books"
                className="flex items-center gap-2 px-5 py-2.5 bg-white/15 hover:bg-white/25 backdrop-blur-sm text-white rounded-xl font-medium transition-all duration-200"
              >
                <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253" />
                </svg>
                Browse Books
              </Link>
              {isLibrarian && (
                <Link
                  to="/borrows"
                  className="flex items-center gap-2 px-5 py-2.5 bg-white text-primary-700 hover:bg-primary-50 rounded-xl font-medium transition-all duration-200"
                >
                  <svg className="w-5 h-5" 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>
                  Manage Borrows
                </Link>
              )}
            </div>
          </div>
        </div>
      </div>

      {/* Stats */}
      <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
        <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-5">
          {/* Books Stat */}
          <div className="stat-card group">
            <div className="flex items-start justify-between">
              <div>
                <p className="text-sm font-medium text-gray-500">Total Books</p>
                <p className="text-4xl font-bold text-gray-900 mt-2">{availableBooks}</p>
                <p className="text-sm text-gray-400 mt-1">in library</p>
              </div>
              <div className="w-12 h-12 bg-blue-50 rounded-xl flex items-center justify-center group-hover:bg-blue-100 transition-colors">
                <svg className="w-6 h-6 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253" />
                </svg>
              </div>
            </div>
            <div className="mt-4 pt-4 border-t border-gray-100">
              <Link to="/books" className="text-sm font-medium text-primary-600 hover:text-primary-700 inline-flex items-center gap-1 transition-colors">
                View all books
                <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
                </svg>
              </Link>
            </div>
          </div>

          {/* Borrows Stat */}
          <div className="stat-card group">
            <div className="flex items-start justify-between">
              <div>
                <p className="text-sm font-medium text-gray-500">
                  {isLibrarian ? 'Active Borrows' : 'My Borrows'}
                </p>
                <p className="text-4xl font-bold text-gray-900 mt-2">{myBorrows}</p>
                <p className="text-sm text-gray-400 mt-1">{isLibrarian ? 'outstanding' : 'borrowed'}</p>
              </div>
              <div className="w-12 h-12 bg-green-50 rounded-xl flex items-center justify-center group-hover:bg-green-100 transition-colors">
                <svg className="w-6 h-6 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 2m-6 9l2 2 4-4" />
                </svg>
              </div>
            </div>
            <div className="mt-4 pt-4 border-t border-gray-100">
              <Link to="/borrows" className="text-sm font-medium text-primary-600 hover:text-primary-700 inline-flex items-center gap-1 transition-colors">
                {isLibrarian ? 'View all borrows' : 'View my borrows'}
                <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
                </svg>
              </Link>
            </div>
          </div>

          {/* Members Stat - Only for Librarians */}
          {isLibrarian ? (
            <div className="stat-card group">
              <div className="flex items-start justify-between">
                <div>
                  <p className="text-sm font-medium text-gray-500">Total Members</p>
                  <p className="text-4xl font-bold text-gray-900 mt-2">{members.length}</p>
                  <p className="text-sm text-gray-400 mt-1">registered</p>
                </div>
                <div className="w-12 h-12 bg-purple-50 rounded-xl flex items-center justify-center group-hover:bg-purple-100 transition-colors">
                  <svg className="w-6 h-6 text-purple-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>
              <div className="mt-4 pt-4 border-t border-gray-100">
                <Link to="/members" className="text-sm font-medium text-primary-600 hover:text-primary-700 inline-flex items-center gap-1 transition-colors">
                  View all members
                  <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
                  </svg>
                </Link>
              </div>
            </div>
          ) : (
            /* Student: Show their status card */
            <div className="stat-card group">
              <div className="flex items-start justify-between">
                <div>
                  <p className="text-sm font-medium text-gray-500">Your Status</p>
                  <p className="text-4xl font-bold text-gray-900 mt-2 capitalize">{authRole}</p>
                  <p className="text-sm text-gray-400 mt-1">Library Member</p>
                </div>
                <div className="w-12 h-12 bg-amber-50 rounded-xl flex items-center justify-center group-hover:bg-amber-100 transition-colors">
                  <svg className="w-6 h-6 text-amber-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z" />
                  </svg>
                </div>
              </div>
              <div className="mt-4 pt-4 border-t border-gray-100">
                <p className="text-xs text-gray-400">
                  You can browse books and manage your borrowed books.
                </p>
              </div>
            </div>
          )}

          {/* Role Info Card */}
          <div className="stat-card group">
            <div className="flex items-start justify-between">
              <div>
                <p className="text-sm font-medium text-gray-500">Your Role</p>
                <p className="text-4xl font-bold text-gray-900 mt-2 capitalize">{authRole}</p>
                <p className="text-sm text-gray-400 mt-1">
                  {isLibrarian ? 'full access' : 'view & borrow'}
                </p>
              </div>
              <div className="w-12 h-12 bg-amber-50 rounded-xl flex items-center justify-center group-hover:bg-amber-100 transition-colors">
                <svg className="w-6 h-6 text-amber-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z" />
                </svg>
              </div>
            </div>
            <div className="mt-4 pt-4 border-t border-gray-100">
              <p className="text-xs text-gray-400">
                {isLibrarian
                  ? 'You can add, edit, and delete books and manage borrow records.'
                  : 'You can browse the catalog and borrow books.'}
              </p>
            </div>
          </div>
        </div>

        {/* Quick Actions - Only for Librarians */}
        {isLibrarian && (
          <div className="mt-8">
            <h2 className="text-lg font-semibold text-gray-900 mb-4">Quick Actions</h2>
            <div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
              <Link
                to="/books"
                className="card p-5 flex items-center gap-4 hover:border-primary-200 group"
              >
                <div className="w-11 h-11 bg-blue-50 rounded-xl flex items-center justify-center group-hover:bg-blue-100 transition-colors">
                  <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="M12 6v6m0 0v6m0-6h6m-6 0H6" />
                  </svg>
                </div>
                <div>
                  <p className="font-semibold text-gray-900">Add New Book</p>
                  <p className="text-sm text-gray-500">Add a book to the library</p>
                </div>
              </Link>
              <Link
                to="/borrows"
                className="card p-5 flex items-center gap-4 hover:border-primary-200 group"
              >
                <div className="w-11 h-11 bg-green-50 rounded-xl flex items-center justify-center group-hover:bg-green-100 transition-colors">
                  <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 Borrows</p>
                  <p className="text-sm text-gray-500">Track book lending</p>
                </div>
              </Link>
            </div>
          </div>
        )}

        {/* Student: My Recent Borrows */}
        {!isLibrarian && activeBorrowsForStudent.length > 0 && (
          <div className="mt-8">
            <h2 className="text-lg font-semibold text-gray-900 mb-4">My Recent Borrows</h2>
            <div className="card">
              <div className="divide-y divide-gray-100">
                {activeBorrowsForStudent.slice(0, 5).map((borrow) => (
                  <div key={borrow._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="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253" />
                        </svg>
                      </div>
                      <div>
                        <p className="font-medium text-gray-900">{borrow.bookId?.title || 'N/A'}</p>
                        <p className="text-sm text-gray-500">
                          Due: {borrow.dueDate ? new Date(borrow.dueDate).toLocaleDateString() : 'N/A'}
                        </p>
                      </div>
                    </div>
                    <Link
                      to="/borrows"
                      className="text-sm font-medium text-primary-600 hover:text-primary-700"
                    >
                      View
                    </Link>
                  </div>
                ))}
              </div>
              <div className="p-4 border-t border-gray-100">
                <Link
                  to="/borrows"
                  className="text-sm font-medium text-primary-600 hover:text-primary-700 inline-flex items-center gap-1"
                >
                  View all my borrowed books
                  <svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
                  </svg>
                </Link>
              </div>
            </div>
          </div>
        )}
      </div>
    </div>
  )
}

export default Dashboard