/**
 * Canonical document tagging metadata. Mirrors web/app.py:DOC_TYPE_NAMES and
 * the validation lists in mcp-server/src/legal_mcp/tools/documents.py.
 *
 * If you add a doc_type or appraiser_side here, add the matching value on the
 * backend too — the API rejects anything not on its whitelist.
 */

export type DocType =
  | "appeal"
  | "response"
  | "protocol"
  | "plan"
  | "decision"
  | "court_decision"
  | "permit"
  | "appraisal"
  | "exhibit"
  | "objection"
  | "reference";

export const DOC_TYPE_LABELS: Record<DocType, string> = {
  appeal:         "כתב ערר",
  response:       "כתב תשובה",
  protocol:       "פרוטוקול",
  plan:           "תכנית",
  decision:       "החלטת ועדה מקומית",
  court_decision: "פסק דין",
  permit:         "היתר",
  appraisal:      "שומה",
  exhibit:        "נספח",
  objection:      "התנגדות",
  reference:      "חומר רקע",
};

/** Display order for editors and selects. */
export const DOC_TYPE_OPTIONS: { value: DocType; label: string }[] = [
  { value: "appeal",         label: DOC_TYPE_LABELS.appeal },
  { value: "response",       label: DOC_TYPE_LABELS.response },
  { value: "protocol",       label: DOC_TYPE_LABELS.protocol },
  { value: "plan",           label: DOC_TYPE_LABELS.plan },
  { value: "decision",       label: DOC_TYPE_LABELS.decision },
  { value: "court_decision", label: DOC_TYPE_LABELS.court_decision },
  { value: "permit",         label: DOC_TYPE_LABELS.permit },
  { value: "appraisal",      label: DOC_TYPE_LABELS.appraisal },
  { value: "exhibit",        label: DOC_TYPE_LABELS.exhibit },
  { value: "objection",      label: DOC_TYPE_LABELS.objection },
  { value: "reference",      label: DOC_TYPE_LABELS.reference },
];

export function doctypeLabel(value: string): string {
  return (DOC_TYPE_LABELS as Record<string, string>)[value] ?? value;
}

export function doctypeTone(value: string): string {
  switch (value) {
    case "appeal":         return "bg-info-bg text-info border-info/40";
    case "response":       return "bg-gold-wash text-gold-deep border-gold/40";
    case "decision":       return "bg-success-bg text-success border-success/40";
    case "protocol":       return "bg-warn-bg text-warn border-warn/40";
    case "appraisal":      return "bg-info-bg text-info border-info/40";
    case "court_decision": return "bg-success-bg text-success border-success/40";
    default:               return "bg-rule-soft text-ink-muted border-rule";
  }
}

// ── Overview groups (display-only convenience buckets) ─────────────
//
// The case-overview document list is split into four sections purely for
// the chair's convenience — grouping NEVER changes a document's stored
// doc_type. Classification is derived on the client from doc_type plus two
// metadata flags (is_post_hearing, protocol_scope).

export type DocGroup = "primary" | "ancillary" | "hearing_protocol" | "post_hearing";

export const DOC_GROUP_LABELS: Record<DocGroup, string> = {
  primary:          "מסמכים עיקריים",
  ancillary:        "מסמכים נלווים",
  hearing_protocol: "פרוטוקול דיון ועדת הערר",
  post_hearing:     "מסמכים לאחר הדיון",
};

export const DOC_GROUP_HINTS: Record<DocGroup, string> = {
  primary:          "כתבי ערר, תשובות ושומות שהוגשו לפני הדיון",
  ancillary:        "פרוטוקולי/החלטות ועדה מקומית-מחוזית, תכניות, וכל השאר",
  hearing_protocol: "הדיון בפני הוועדה",
  post_hearing:     "השלמות ותגובות שהתקבלו לאחר הדיון",
};

/** Fixed render order of the groups in the overview panel. */
export const DOC_GROUP_ORDER: DocGroup[] = [
  "primary",
  "ancillary",
  "hearing_protocol",
  "post_hearing",
];

/** doc_types that count as "primary" pre-hearing filings (group 1). */
const PRIMARY_DOC_TYPES = new Set<DocType>(["appeal", "response", "appraisal"]);

/**
 * Classify a document into one of the four overview groups.
 *
 * Priority order matters:
 *   1. is_post_hearing wins over everything — a post-hearing כתב תשובה
 *      belongs in group 4, not group 1.
 *   2. A protocol goes to group 3 (ועדת הערר) by default, or group 2 when
 *      explicitly tagged as a lower-committee protocol (protocol_scope==="lower").
 *   3. appeal/response/appraisal → group 1; everything else → group 2.
 */
export function documentGroup(
  docType: string,
  metadata?: Record<string, unknown> | null,
): DocGroup {
  if (metadata?.is_post_hearing === true) return "post_hearing";
  if (docType === "protocol") {
    return metadata?.protocol_scope === "lower" ? "ancillary" : "hearing_protocol";
  }
  if (PRIMARY_DOC_TYPES.has(docType as DocType)) return "primary";
  return "ancillary";
}

// ── Protocol scope (only relevant when doc_type === "protocol") ────
// Distinguishes the appeal-committee hearing protocol (group 3) from a
// lower (local/district) committee protocol (group 2). Stored in
// documents.metadata.protocol_scope; absent === "appeal" (the default).

export type ProtocolScope = "appeal" | "lower";

export const PROTOCOL_SCOPE_LABELS: Record<ProtocolScope, string> = {
  appeal: "ועדת הערר",
  lower:  "ועדה מקומית/מחוזית",
};

export const PROTOCOL_SCOPE_OPTIONS: { value: ProtocolScope; label: string }[] = [
  { value: "appeal", label: PROTOCOL_SCOPE_LABELS.appeal },
  { value: "lower",  label: PROTOCOL_SCOPE_LABELS.lower },
];

// ── Appraiser sides (only relevant when doc_type === "appraisal") ──

export type AppraiserSide = "committee" | "appellant" | "deciding";

export const APPRAISER_SIDE_LABELS: Record<AppraiserSide, string> = {
  committee: "שמאי הוועדה המקומית",
  appellant: "שמאי העורר",
  deciding:  "שמאי מכריע",
};

export const APPRAISER_SIDE_OPTIONS: { value: AppraiserSide; label: string }[] = [
  { value: "committee", label: APPRAISER_SIDE_LABELS.committee },
  { value: "appellant", label: APPRAISER_SIDE_LABELS.appellant },
  { value: "deciding",  label: APPRAISER_SIDE_LABELS.deciding },
];

export function appraiserSideLabel(value: string): string {
  return (APPRAISER_SIDE_LABELS as Record<string, string>)[value] ?? value;
}
