"use client"

import React from "react"
import { Skeleton } from "@/components/ui/skeleton"
import { Card } from "@/components/ui/card"

/**
 * Skeleton loader for individual Repository Card
 * Mimics the structure of the RepositoryCard component
 */
const RepositoryCardSkeleton: React.FC = () => (
  <Card className="bg-card bg-muted/5 rounded-xl border border-border overflow-hidden">
    <div className="p-5">
      <div className="flex items-start">
        {/* Avatar skeleton */}
        <div className="mr-3 flex-shrink-0">
          <Skeleton className="w-9 h-9 sm:w-11 sm:h-11 rounded-full" />
        </div>

        <div className="flex-1 min-w-0">
          {/* Repository name and badge skeleton */}
          <div className="flex items-center flex-wrap gap-1 mb-2">
            <Skeleton className="h-5 w-40 sm:w-48" />
            <Skeleton className="h-5 w-14 rounded-full" />
          </div>

          {/* Organization/full name skeleton */}
          <Skeleton className="h-4 w-32 sm:w-40 mb-2" />

          {/* Repository stats skeleton */}
          <div className="flex items-center flex-wrap gap-1.5 sm:gap-2">
            <Skeleton className="h-6 w-16 rounded-full" />
            <Skeleton className="h-6 w-14 rounded-full" />
            <Skeleton className="h-6 w-12 rounded-full" />
          </div>
        </div>
      </div>

      {/* Last optimized date skeleton */}
      <div className="mt-3 sm:mt-4">
        <Skeleton className="h-3 w-36" />
      </div>
    </div>
  </Card>
)

/**
 * Complete Repositories Page Skeleton Loader
 * Displays skeleton placeholders for the repositories page
 * Used while repository data is being fetched
 */
export const RepositoriesSkeleton: React.FC<{ message?: string }> = ({
  message = "Loading repositories...",
}) => {
  return (
    <div className="space-y-4 sm:space-y-6">
      {/* Header skeleton */}
      <div className="flex items-center justify-between mb-4 sm:mb-6">
        <div>
          <Skeleton className="h-8 w-32 sm:w-40 mb-2" />
          <Skeleton className="h-4 w-48 sm:w-64" />
        </div>
        <Skeleton className="h-9 w-20 sm:w-24 rounded-lg" />
      </div>

      {/* Optional loading message */}
      {message && (
        <div className="text-center mb-4">
          <p className="text-sm text-muted-foreground animate-pulse">{message}</p>
        </div>
      )}

      {/* Repository cards grid skeleton */}
      <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 sm:gap-5">
        {[...Array(6)].map((_, index) => (
          <RepositoryCardSkeleton key={index} />
        ))}
      </div>
    </div>
  )
}

/**
 * Compact loading state for refreshing repositories
 * Shows fewer skeleton cards with a subtle animation
 */
export const RepositoriesRefreshingSkeleton: React.FC = () => {
  return (
    <div className="space-y-4">
      <div className="text-center py-2">
        <p className="text-sm text-muted-foreground animate-pulse">Refreshing repositories...</p>
      </div>
      <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 sm:gap-5">
        {[...Array(3)].map((_, index) => (
          <RepositoryCardSkeleton key={index} />
        ))}
      </div>
    </div>
  )
}
