'use client';

import Link from 'next/link';
import { ArrowUpRight, Briefcase, Users } from 'lucide-react';
import { getCategoryTl } from '@/lib/categoryTl';
import { JOBS_FEATURE_ENABLED } from '@/lib/features';
import { useLanguage } from '@/contexts/LanguageContext';
import { cn } from '@/lib/utils';

export interface CategoryCardData {
  id: string;
  name: string;
  slug: string;
  icon?: string;
  description?: string;
  _count?: { workerSkills: number; jobs: number };
}

interface CategoryCardProps {
  category: CategoryCardData;
  /** Compact tiles for home browse grid; default for full categories page */
  variant?: 'compact' | 'default';
}

function CategoryStats({
  workers,
  jobs,
  compact,
}: {
  workers: number;
  jobs: number;
  compact?: boolean;
}) {
  const showJobs = JOBS_FEATURE_ENABLED && jobs > 0;
  if (workers === 0 && !showJobs) return null;

  return (
    <div className={cn('flex flex-wrap gap-1.5', compact ? 'mt-2' : 'mt-auto pt-2.5')}>
      {workers > 0 && (
        <span className="inline-flex items-center gap-1 text-[10px] font-medium text-slate-500 sm:text-[11px]">
          <Users className="h-3 w-3 shrink-0 text-[var(--home-amber)]" />
          {workers} worker{workers !== 1 ? 's' : ''}
        </span>
      )}
      {showJobs && (
        <span className="inline-flex items-center gap-1 text-[10px] font-medium text-slate-500 sm:text-[11px]">
          <Briefcase className="h-3 w-3 shrink-0" />
          {jobs} job{jobs !== 1 ? 's' : ''}
        </span>
      )}
    </div>
  );
}

export function CategoryCard({ category, variant = 'default' }: CategoryCardProps) {
  const { lang, t } = useLanguage();
  const workers = category._count?.workerSkills ?? 0;
  const jobs = category._count?.jobs ?? 0;
  const compact = variant === 'compact';
  const tl = getCategoryTl(category.slug);
  const name = lang === 'tl' && tl?.nameTl ? tl.nameTl : category.name;
  const description =
    lang === 'tl' && tl?.descriptionTl ? tl.descriptionTl : category.description;

  return (
    <Link href={`/category/${category.slug}`} className="group block h-full">
      <article
        className={cn(
          'relative flex h-full flex-col border border-[#e2ddd2] bg-[#fffdf9] transition-all duration-200',
          'hover:border-[#c4b89f] hover:bg-white hover:-translate-y-0.5',
          compact ? 'min-h-[6.5rem] p-3 sm:p-3.5' : 'p-4'
        )}
      >
        <div className="flex items-start justify-between gap-2">
          <div
            className={cn(
              'flex shrink-0 items-center justify-center border border-[#e2ddd2] bg-[var(--home-sand)]',
              compact ? 'h-10 w-10 text-xl sm:h-11 sm:w-11 sm:text-2xl' : 'h-11 w-11 text-2xl sm:h-12 sm:w-12'
            )}
          >
            {category.icon || '🔧'}
          </div>
          <ArrowUpRight
            className={cn(
              'shrink-0 text-slate-300 transition-all duration-200',
              'group-hover:text-[var(--home-amber)] group-hover:translate-x-0.5 group-hover:-translate-y-0.5',
              compact ? 'h-3.5 w-3.5 mt-0.5' : 'h-4 w-4 mt-1'
            )}
          />
        </div>

        <h3
          className={cn(
            'home-display mt-2.5 font-bold text-slate-900 leading-snug',
            compact ? 'text-sm' : 'text-sm sm:text-[0.9375rem]'
          )}
        >
          <span className="block line-clamp-2">{name}</span>
        </h3>

        {!compact && description && (
          <p className="mt-1 text-xs text-muted leading-relaxed line-clamp-3 hidden sm:block">
            {description}
          </p>
        )}

        <CategoryStats workers={workers} jobs={jobs} compact={compact} />

        {!compact && workers === 0 && jobs === 0 && (
          <p className="mt-auto pt-2 text-[11px] text-slate-400">
            {t('Browse this trade', 'Tingnan ang trabahong ito')}
          </p>
        )}
      </article>
    </Link>
  );
}

export function CategoryCardSkeleton({ compact }: { compact?: boolean }) {
  return (
    <div
      className={cn(
        'border border-[#e2ddd2] bg-[#fffdf9] animate-pulse',
        compact ? 'h-[7.5rem]' : 'h-[9rem] sm:h-[9.5rem]'
      )}
    >
      <div className="space-y-3 p-4">
        <div className="h-10 w-10 bg-slate-200" />
        <div className="h-4 w-3/4 bg-slate-200" />
        <div className="h-3 w-1/2 bg-slate-100" />
      </div>
    </div>
  );
}
