'use client';

import React from 'react';
import Link from 'next/link';
import { formatScore, countdown } from '../lib/format';

function TeamOrb({ code, tone, logoUrl }: { code: string; tone: 'a' | 'b'; logoUrl?: string }) {
  const bg =
    tone === 'a'
      ? 'bg-gradient-to-br from-sky-600 to-blue-800'
      : 'bg-gradient-to-br from-orange-400 to-orange-600';
  if (logoUrl) {
    // eslint-disable-next-line @next/next/no-img-element
    return (
      <img
        src={logoUrl}
        alt=""
        className="h-8 w-8 shrink-0 rounded-full object-cover ring-2 ring-white shadow-sm"
      />
    );
  }
  return (
    <div
      className={`flex h-8 w-8 shrink-0 items-center justify-center rounded-full text-[9px] font-black text-white shadow-sm ring-2 ring-white ${bg}`}
      aria-hidden
    >
      {(code || 'T').slice(0, 3).toUpperCase()}
    </div>
  );
}

export function MatchCard({ match }: { match: any }) {
  const a = match.teamA?.shortName || match.teamA?.name || 'T1';
  const b = match.teamB?.shortName || match.teamB?.name || 'T2';
  const isLive = match.status === 'LIVE';
  const isDone = match.status === 'COMPLETED';
  const isUpcoming = match.status === 'SCHEDULED';
  const fantasyOn = match.fantasyEnabled !== false;

  const href =
    isUpcoming && fantasyOn ? `/fantasy?matchId=${match._id}` : `/matches/${match._id}`;

  const cta = isUpcoming ? (fantasyOn ? 'Join' : 'View') : isLive ? 'Live' : 'Score';

  const when = new Date(match.startTime).toLocaleString(undefined, {
    day: 'numeric',
    month: 'short',
    hour: '2-digit',
    minute: '2-digit',
  });

  return (
    <Link
      href={href}
      className="d11-card group block cursor-pointer overflow-hidden hover:shadow-lift focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-d11-red"
    >
      {/* Compact meta row */}
      <div className="flex items-center justify-between gap-2 px-3 pt-2">
        <p className="min-w-0 truncate text-[10px] font-semibold uppercase tracking-wide text-d11-muted">
          {match.tournamentId?.title || match.venue || 'Cricket'}
        </p>
        {isLive ? (
          <span className="inline-flex shrink-0 items-center gap-1 rounded-full bg-d11-red px-1.5 py-0.5 text-[9px] font-extrabold uppercase tracking-wide text-white">
            <span className="live-dot inline-block h-1.5 w-1.5 rounded-full bg-white" />
            Live
          </span>
        ) : isDone ? (
          <span className="shrink-0 rounded-full bg-emerald-50 px-1.5 py-0.5 text-[9px] font-extrabold uppercase tracking-wide text-emerald-700">
            Done
          </span>
        ) : (
          <span className="shrink-0 text-[10px] font-extrabold text-d11-red">
            {countdown(match.startTime)}
          </span>
        )}
      </div>

      {/* Teams — single dense row */}
      <div className="grid grid-cols-[1fr_auto_1fr] items-center gap-1.5 px-3 py-2.5">
        <div className="flex min-w-0 items-center gap-2">
          <TeamOrb code={a} tone="a" logoUrl={match.teamA?.logoUrl} />
          <div className="min-w-0">
            <div className="truncate text-[13px] font-extrabold leading-tight text-d11-ink">{a}</div>
            {(isLive || isDone) && (
              <div className="font-mono text-[11px] font-bold leading-tight text-gray-600">
                {formatScore(match.teamA)}
              </div>
            )}
          </div>
        </div>

        <span className="px-0.5 text-[9px] font-extrabold uppercase tracking-wider text-gray-300">
          vs
        </span>

        <div className="flex min-w-0 items-center justify-end gap-2">
          <div className="min-w-0 text-right">
            <div className="truncate text-[13px] font-extrabold leading-tight text-d11-ink">{b}</div>
            {(isLive || isDone) && (
              <div className="font-mono text-[11px] font-bold leading-tight text-gray-600">
                {formatScore(match.teamB)}
              </div>
            )}
          </div>
          <TeamOrb code={b} tone="b" logoUrl={match.teamB?.logoUrl} />
        </div>
      </div>

      {/* Footer CTA strip */}
      <div className="flex items-center justify-between gap-2 border-t border-gray-100 bg-gray-50/80 px-3 py-1.5">
        <span className="truncate text-[10px] font-medium text-d11-muted">{when}</span>
        <span
          className={`shrink-0 rounded-full px-2.5 py-1 text-[10px] font-extrabold transition group-active:scale-95 ${
            isUpcoming && fantasyOn
              ? 'bg-d11-red text-white shadow-sm shadow-d11-red/20'
              : isLive
                ? 'bg-emerald-100 text-emerald-800'
                : 'bg-white text-d11-ink ring-1 ring-gray-200'
          }`}
        >
          {cta}
        </span>
      </div>
    </Link>
  );
}
