import React from 'react'

export const SkeletonPulse = ({ className = '' }: { className?: string }) => (
  <div
    className={`animate-pulse rounded bg-gray-200 dark:bg-gray-700 ${className}`}
    aria-hidden
  />
)

interface TableSkeletonProps {
  rows?: number
  cols?: number
  showCheckbox?: boolean
}

export const TableSkeleton = ({ rows = 8, cols = 6, showCheckbox = false }: TableSkeletonProps) => (
  <div className="rounded-2xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-white/[0.03]">
    <div className="overflow-x-auto">
      <table className="w-full">
        <thead>
          <tr className="border-b border-gray-200 dark:border-gray-800">
            {showCheckbox && (
              <th className="px-5 py-4">
                <SkeletonPulse className="h-4 w-4 rounded" />
              </th>
            )}
            {Array.from({ length: cols }).map((_, i) => (
              <th key={i} className="px-5 py-4 text-right">
                <SkeletonPulse className="h-4 w-24" />
              </th>
            ))}
          </tr>
        </thead>
        <tbody className="divide-y divide-gray-200 dark:divide-gray-800">
          {Array.from({ length: rows }).map((_, rowIndex) => (
            <tr key={rowIndex} className="hover:bg-transparent">
              {showCheckbox && (
                <td className="px-5 py-4">
                  <SkeletonPulse className="h-4 w-4 rounded" />
                </td>
              )}
              {Array.from({ length: cols }).map((_, colIndex) => (
                <td key={colIndex} className="px-5 py-4">
                  <SkeletonPulse
                    className={`h-4 ${
                      colIndex === 0 ? 'w-24' : colIndex === cols - 1 ? 'w-20' : 'w-32'
                    }`}
                  />
                </td>
              ))}
            </tr>
          ))}
        </tbody>
      </table>
    </div>
  </div>
)

interface ListSkeletonProps {
  rows?: number
}

export const ListSkeleton = ({ rows = 5 }: ListSkeletonProps) => (
  <div className="divide-y divide-gray-200 dark:divide-gray-800">
    {Array.from({ length: rows }).map((_, i) => (
      <div key={i} className="flex items-center justify-between px-6 py-3">
        <div className="space-y-2">
          <SkeletonPulse className="h-4 w-32" />
          <SkeletonPulse className="h-3 w-24" />
        </div>
        <SkeletonPulse className="h-6 w-16 rounded-full" />
      </div>
    ))}
  </div>
)

interface DetailPageSkeletonProps {
  showBackButton?: boolean
  cards?: number
}

export const DetailPageSkeleton = ({
  showBackButton = true,
  cards = 3,
}: DetailPageSkeletonProps) => (
  <div className="space-y-6">
    {showBackButton && (
      <SkeletonPulse className="h-5 w-32 rounded" />
    )}
    <div className="rounded-2xl border border-gray-200 bg-white p-6 dark:border-gray-800 dark:bg-white/[0.03]">
      <div className="flex flex-wrap items-start justify-between gap-4">
        <div className="space-y-2">
          <div className="flex items-center gap-3">
            <SkeletonPulse className="h-6 w-48" />
            <SkeletonPulse className="h-6 w-16 rounded-full" />
          </div>
          <div className="flex gap-4">
            <SkeletonPulse className="h-4 w-28" />
            <SkeletonPulse className="h-4 w-24" />
          </div>
        </div>
        <div className="flex items-center gap-2">
          {[1, 2, 3].map((i) => (
            <SkeletonPulse key={i} className="h-10 w-28 rounded-lg" />
          ))}
        </div>
      </div>
    </div>
    <div className="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3">
      {Array.from({ length: cards }, (_, i) => (
        <div
          key={i}
          className="rounded-2xl border border-gray-200 bg-white p-5 dark:border-gray-800 dark:bg-white/[0.03]"
        >
          <SkeletonPulse className="h-4 w-24 mb-4" />
          <div className="space-y-3">
            <SkeletonPulse className="h-6 w-full" />
            <SkeletonPulse className="h-6 w-[75%]" />
            <SkeletonPulse className="h-6 w-1/2" />
          </div>
        </div>
      ))}
    </div>
  </div>
)
