import {
  historicalAccuracySchema,
  type HistoricalAccuracy,
} from "@/lib/schemas";

export function HistoricalAccuracyView({ data }: { data: unknown }) {
  const result = historicalAccuracySchema.safeParse(data);
  if (!result.success) {
    return (
      <div className="rounded-2xl border border-stone-200 bg-white p-6 text-sm text-stone-700">
        Could not display this insight (data format issue). Try another card or refresh later.
      </div>
    );
  }
  const parsed = result.data as HistoricalAccuracy;

  return (
    <div className="space-y-5 rounded-2xl border border-stone-200 bg-white p-6">
      <div className="flex items-end gap-4">
        <div>
          <p className="text-xs uppercase tracking-wide text-stone-500">
            Accuracy score
          </p>
          <p className="text-4xl font-semibold text-amber-700">
            {parsed.accuracyScore}
            <span className="text-lg text-stone-500">/100</span>
          </p>
        </div>
        <p className="pb-1 text-sm capitalize text-stone-600">
          Confidence: {parsed.confidence}
        </p>
      </div>

      <p className="text-stone-700">{parsed.summary}</p>

      {parsed.accurateElements.length > 0 && (
        <div>
          <p className="mb-2 text-sm font-medium text-stone-950">What holds up</p>
          <ul className="space-y-2">
            {parsed.accurateElements.map((item) => (
              <li
                key={item}
                className="rounded-lg border border-emerald-200 bg-emerald-50 px-3 py-2 text-sm text-stone-700"
              >
                {item}
              </li>
            ))}
          </ul>
        </div>
      )}

      {parsed.inventions.length > 0 && (
        <div>
          <p className="mb-2 text-sm font-medium text-stone-950">
            Dramatizations & inventions
          </p>
          <div className="space-y-3">
            {parsed.inventions.map((item, index) => (
              <div
                key={`${item.claim}-${index}`}
                className="rounded-xl border border-stone-200 bg-stone-100 p-4"
              >
                <p className="text-sm text-stone-700">
                  <span className="text-stone-500">Film claim:</span> {item.claim}
                </p>
                <p className="mt-2 text-sm text-stone-700">
                  <span className="text-stone-500">Reality:</span> {item.reality}
                </p>
              </div>
            ))}
          </div>
        </div>
      )}
    </div>
  );
}