"use client";

/**
 * ParcelPassportCard — passport KV-grid (hero col 2). Mono cad number; every
 * value flows from the adapter view-model, so placeholders render muted "—"
 * with their caption rather than fake live data.
 */

import styles from "@/app/site-finder/analysis/[cad]/ptica/ptica.module.css";
import type { ParcelAnalysis } from "@/types/site-finder";
import { adaptPassport } from "@/components/site-finder/ptica/ptica-adapt";
import type {
  PticaField,
  PticaPassport,
} from "@/components/site-finder/ptica/ptica-adapt";
import type { DrawerKey } from "@/components/site-finder/ptica/drawers/drawer-keys";

interface Props {
  analysis: ParcelAnalysis;
  onOpenDrawer: (key: DrawerKey) => void;
}

function KvCell({
  label,
  field,
  wide,
}: {
  label: string;
  field: PticaField;
  wide?: boolean;
}) {
  return (
    <div className={`${styles.kv} ${wide ? styles.kvWide : ""}`}>
      <span className={styles.kvK}>{label}</span>
      <span
        className={`${styles.kvV} ${field.isReal ? "" : styles.kvPlaceholder}`}
      >
        {field.value}
      </span>
      {field.caption && !field.isReal && (
        <span className={styles.kvCaption}>{field.caption}</span>
      )}
    </div>
  );
}

export function ParcelPassportCard({ analysis, onOpenDrawer }: Props) {
  const p: PticaPassport = adaptPassport(analysis);

  return (
    <div className={styles.panel}>
      <div className={styles.cardHead}>
        <h3 className={styles.cardTitle}>Паспорт участка</h3>
        <button
          type="button"
          className={styles.detailBtn}
          onClick={() => onOpenDrawer("passport")}
          title="Открыть детализацию"
        >
          Подробнее
        </button>
      </div>
      <div className={styles.kvGrid}>
        <div className={`${styles.kv} ${styles.kvWide}`}>
          <span className={styles.kvK}>Кадастровый номер</span>
          <span className={`${styles.kvV} ${styles.mono}`}>{p.cadNum}</span>
        </div>
        <KvCell label="Район" field={p.district} />
        <KvCell label="Площадь" field={p.area} />
        <KvCell label="Адрес" field={p.address} wide />
        <KvCell label="Категория земель" field={p.landCategory} />
        <KvCell label="ВРИ" field={p.vri} />
        <KvCell label="Статус" field={p.status} />
        <KvCell label="Обременения" field={p.encumbrances} />
      </div>
    </div>
  );
}
