'use client';

import Link from 'next/link';
import { formatCurrencyShort, getTimeRemaining, getMatchStatus } from '@/lib/utils';
import type { Match } from '@/types';

interface MatchCardProps {
  match: Match;
  showCountdown?: boolean;
}

export function MatchCard({ match, showCountdown = true }: MatchCardProps) {
  const timeRemaining = getTimeRemaining(match.registrationDeadlineAt);
  const status = getMatchStatus(match);
  const isRegistrationOpen = new Date() < match.registrationDeadlineAt;

  return (
    <Link href={`/tournament/${match.id}`}>
      <div className="card group cursor-pointer animate-bounce-in h-full">
        {/* Cover Image */}
        <div className="relative mb-4 rounded-lg overflow-hidden bg-slate-800 aspect-video">
          {match.coverImage && (
            <img
              src={match.coverImage}
              alt={match.title}
              className="w-full h-full object-cover group-hover:scale-110 transition duration-300"
            />
          )}
          {!match.coverImage && (
            <div className="w-full h-full flex items-center justify-center bg-gradient-to-br from-orange-500/20 to-yellow-500/10">
              <div className="text-center">
                <div className="text-4xl mb-2">🎮</div>
                <p className="text-slate-400 text-sm">Gaming Tournament</p>
              </div>
            </div>
          )}

          {/* Status Badge */}
          <div className="absolute top-3 right-3 bg-slate-900/90 px-3 py-1 rounded-full text-xs font-bold">
            {isRegistrationOpen ? (
              <span className="text-green-400 flex items-center gap-1">
                <span className="w-2 h-2 bg-green-400 rounded-full"></span>
                খোলা
              </span>
            ) : (
              <span className="text-slate-400">বন্ধ</span>
            )}
          </div>

          {/* Featured Badge */}
          {match.isFeatured && (
            <div className="absolute top-3 left-3 bg-yellow-500 text-black px-2 py-1 rounded text-xs font-bold">
              ⭐ বৈশিষ্ট্যযুক্ত
            </div>
          )}
        </div>

        {/* Content */}
        <div className="space-y-3">
          {/* Title */}
          <h3 className="font-bold text-lg line-clamp-2 group-hover:text-orange-500 transition">
            {match.title}
          </h3>

          {/* Match Info */}
          <div className="flex flex-wrap gap-2 text-xs">
            <span className="bg-slate-800 px-2 py-1 rounded">{match.gameMode}</span>
            <span className="bg-slate-800 px-2 py-1 rounded">{match.teamType}</span>
          </div>

          {/* Pricing and Slots */}
          <div className="grid grid-cols-2 gap-2 pt-3 border-t border-slate-800">
            <div>
              <p className="text-slate-400 text-xs mb-1">এন্ট্রি ফি</p>
              <p className="text-lg font-bold text-orange-500">
                {formatCurrencyShort(match.entryFee)}
              </p>
            </div>
            <div>
              <p className="text-slate-400 text-xs mb-1">পুরস্কার পুল</p>
              <p className="text-lg font-bold text-yellow-400">
                {formatCurrencyShort(match.prizePool)}
              </p>
            </div>
          </div>

          {/* Slots */}
          <div className="space-y-2 pt-2">
            <p className="text-slate-400 text-xs">উপলব্ধ স্লট: {match.availableSlots}/{match.totalSlots}</p>
            <div className="w-full bg-slate-800 rounded-full h-2 overflow-hidden">
              <div
                className="bg-gradient-to-r from-orange-500 to-yellow-500 h-full transition-all"
                style={{
                  width: `${((match.totalSlots - match.availableSlots) / match.totalSlots) * 100}%`,
                }}
              ></div>
            </div>
          </div>

          {/* Countdown */}
          {showCountdown && isRegistrationOpen && (
            <div className="pt-3 border-t border-slate-800">
              <p className="text-slate-400 text-xs mb-2">নিবন্ধন শেষ হয়:</p>
              <div className="flex gap-2 text-xs font-bold">
                {timeRemaining.isExpired ? (
                  <span className="text-red-400">সমাপ্ত</span>
                ) : (
                  <>
                    {timeRemaining.days > 0 && (
                      <span className="bg-slate-800 px-2 py-1 rounded">
                        {timeRemaining.days}দ
                      </span>
                    )}
                    {timeRemaining.hours > 0 && (
                      <span className="bg-slate-800 px-2 py-1 rounded">
                        {timeRemaining.hours}ঘ
                      </span>
                    )}
                    <span className="bg-slate-800 px-2 py-1 rounded">
                      {timeRemaining.minutes}মি
                    </span>
                  </>
                )}
              </div>
            </div>
          )}

          {/* Join Button */}
          <button className="btn btn-primary w-full mt-4 group-hover:shadow-glow">
            যোগ দিন →
          </button>
        </div>
      </div>
    </Link>
  );
}
