"use client";

/**
 * SiteVerdictCard — bottom-grid «Site Verdict» plaque: the JTBD «стоит ли брать
 * участок» answer. The big verdict word comes from gate_verdict
 * (can_build_mkd → Приобретать/Нельзя/Нужна проверка); the checklist is derived
 * from REAL signals (рыночный спрос, потенциал застройки, инженерия) and the gate
 * blockers/warnings. Ports the prototype's `.verdict-card`/`.verdict-split`.
 */

import styles from "@/app/site-finder/analysis/[cad]/ptica/ptica.module.css";
import type { ParcelAnalysis } from "@/types/site-finder";
import type { ForecastReport } from "@/types/forecast";
import {
  adaptSiteVerdict,
  type PticaVerdictCheckItem,
} from "@/components/site-finder/ptica/ptica-adapt";

interface Props {
  analysis: ParcelAnalysis;
  forecastReport?: ForecastReport;
}

const WORD_TONE_CLASS = {
  good: styles.verdictWordGood,
  warn: styles.verdictWordWarn,
  bad: styles.verdictWordBad,
} as const;

const ITEM_TONE_CLASS: Record<PticaVerdictCheckItem["tone"], string> = {
  ok: styles.checkItemOk,
  warn: styles.checkItemWarn,
  bad: styles.checkItemBad,
};

function CheckIcon({ tone }: { tone: PticaVerdictCheckItem["tone"] }) {
  if (tone === "ok") {
    return (
      <svg
        className={styles.checkIc}
        viewBox="0 0 24 24"
        fill="none"
        aria-hidden="true"
      >
        <path
          d="M5 13l4 4L19 7"
          stroke="currentColor"
          strokeWidth="2"
          strokeLinecap="round"
          strokeLinejoin="round"
        />
      </svg>
    );
  }
  // warn / bad → triangle exclamation
  return (
    <svg
      className={styles.checkIc}
      viewBox="0 0 24 24"
      fill="none"
      aria-hidden="true"
    >
      <path
        d="M12 4l8 14H4L12 4z"
        stroke="currentColor"
        strokeWidth="1.8"
        strokeLinejoin="round"
      />
      <path
        d="M12 10v4M12 16.5v.4"
        stroke="currentColor"
        strokeWidth="1.8"
        strokeLinecap="round"
      />
    </svg>
  );
}

export function SiteVerdictCard({ analysis, forecastReport }: Props) {
  const { word, tone, checklist } = adaptSiteVerdict(analysis, forecastReport);

  return (
    <div className={`${styles.card} ${styles.verdictCard}`}>
      <div className={styles.verdictSplit}>
        <div className={styles.verdictLead}>
          <div className={styles.verdictLeadLabel}>Site Verdict</div>
          <div className={`${styles.verdictWord} ${WORD_TONE_CLASS[tone]}`}>
            {word}
          </div>
        </div>
        <div className={styles.checklist}>
          {checklist.map((item, i) => (
            <div
              key={`${item.text}-${i}`}
              className={`${styles.checkItem} ${ITEM_TONE_CLASS[item.tone]}`}
            >
              <CheckIcon tone={item.tone} />
              {item.text}
            </div>
          ))}
        </div>
      </div>
    </div>
  );
}
