/**
 * Library authoring — generic demo constants for the new-item composer.
 *
 * Storage in `lib/mock/library.ts` keeps the simple `LibraryItemType` enum
 * (`multiple_choice` | `true_false` | `short_answer`). The authoring surface
 * exposes a richer item-writer model and maps back to that base enum on save.
 *
 * All copy is intentionally generic so the demo applies to any product domain.
 */

import type { LibraryItemType, LibraryTierLevel } from "@/lib/mock/library"

// ─────────────────────────────────────────────────────────────────────────────
// Item type
// ─────────────────────────────────────────────────────────────────────────────

/** Authoring-time item type — richer than the storage enum. */
export type AuthoringQuestionType =
  | "mcq_single"
  | "mcq_multiple"
  | "true_false"
  | "short_answer"
  | "numeric"
  | "essay"
  | "fill_blank"
  | "matching"
  | "ordering"
  | "hotspot"

export interface AuthoringQuestionTypeOption {
  value: AuthoringQuestionType
  label: string
  shortLabel: string
  icon: string
  /** Long, sentence-form description for the inspector caption. */
  description: string
  /** Compact one-liner shown under the title in `SelectionTileGrid` tiles. */
  tileSummary: string
  /** "Single best answer", "Multiple response", etc. — appears under the title. */
  badge?: string
  /** Maps to the storage type when persisting. */
  storageType: LibraryItemType
}

export const AUTHORING_QUESTION_TYPES: readonly AuthoringQuestionTypeOption[] = [
  {
    value: "mcq_single",
    label: "Single choice",
    shortLabel: "Single choice",
    icon: "fa-list-radio",
    description:
      "One correct answer from a set of options. Default 5 distractors.",
    tileSummary: "Single correct answer from a set of options",
    badge: "Single best answer",
    storageType: "multiple_choice",
  },
  {
    value: "mcq_multiple",
    label: "Multi-select",
    shortLabel: "Multi-select",
    icon: "fa-list-check",
    description:
      "Select every correct option from the list. Optional partial credit.",
    tileSummary: "One or more correct answers — partial credit",
    badge: "Multiple response",
    storageType: "multiple_choice",
  },
  {
    value: "true_false",
    label: "True / False",
    shortLabel: "True / False",
    icon: "fa-circle-half-stroke",
    description:
      "Use for simple, unambiguous statements.",
    tileSummary: "Binary statement — quick recall",
    storageType: "true_false",
  },
  {
    value: "short_answer",
    label: "Short answer",
    shortLabel: "Short answer",
    icon: "fa-input-text",
    description:
      "Free-text response with an expected answer + acceptable variants.",
    tileSummary: "Single word or short phrase, exact-match",
    storageType: "short_answer",
  },
  {
    value: "numeric",
    label: "Numeric",
    shortLabel: "Numeric",
    icon: "fa-input-numeric",
    description:
      "Number-only response with optional units and a ± tolerance band.",
    tileSummary: "Number with units and tolerance band",
    badge: "Auto-graded",
    storageType: "short_answer",
  },
  {
    value: "essay",
    label: "Essay",
    shortLabel: "Essay",
    icon: "fa-paragraph",
    description:
      "Long-form response graded against a rubric.",
    tileSummary: "Long response — rubric-graded",
    badge: "Rubric",
    storageType: "short_answer",
  },
  {
    value: "fill_blank",
    label: "Fill in the blank",
    shortLabel: "Fill in the blank",
    icon: "fa-text-size",
    description:
      "Sentence with one or more {{blanks}}; respondents type the missing word(s).",
    tileSummary: "Sentence with one or more blanks to fill",
    storageType: "short_answer",
  },
  {
    value: "matching",
    label: "Matching",
    shortLabel: "Matching",
    icon: "fa-arrow-right-arrow-left",
    description:
      "Two columns — pair each prompt on the left to its correct match on the right.",
    tileSummary: "Pair entries on the left to entries on the right",
    storageType: "multiple_choice",
  },
  {
    value: "ordering",
    label: "Ordering",
    shortLabel: "Ordering",
    icon: "fa-list-ol",
    description:
      "Show steps; respondents re-order them into the correct sequence.",
    tileSummary: "Place items in the correct sequence",
    storageType: "multiple_choice",
  },
  {
    value: "hotspot",
    label: "Hotspot",
    shortLabel: "Hotspot",
    icon: "fa-bullseye-pointer",
    description:
      "Image-based item — respondents click the correct region.",
    tileSummary: "Click the correct region of an image",
    badge: "Image-based",
    storageType: "multiple_choice",
  },
] as const

export function authoringQuestionType(value: AuthoringQuestionType): AuthoringQuestionTypeOption {
  return AUTHORING_QUESTION_TYPES.find(t => t.value === value) ?? AUTHORING_QUESTION_TYPES[0]
}

// ─────────────────────────────────────────────────────────────────────────────
// Level + tier + cognitive level
// ─────────────────────────────────────────────────────────────────────────────

export const AUTHORING_DIFFICULTY_OPTIONS = [
  { value: "easy", label: "Low", description: "Quick recall — most respondents complete" },
  { value: "medium", label: "Normal", description: "Standard application — mixed completion" },
  { value: "hard", label: "High", description: "Deeper analysis — fewer respondents complete" },
] as const

export const AUTHORING_BLOOM_OPTIONS: readonly { value: LibraryTierLevel; label: string; hint: string }[] = [
  { value: "Tier 1", label: "Tier 1", hint: "Recall basic facts" },
  { value: "Tier 2", label: "Tier 2", hint: "Explain, summarise, classify" },
  { value: "Tier 3", label: "Tier 3", hint: "Use knowledge in a new situation" },
  { value: "Tier 4", label: "Tier 4", hint: "Break information apart, examine relationships" },
  { value: "Tier 5", label: "Tier 5", hint: "Justify a stand, defend a decision" },
  { value: "Tier 6", label: "Tier 6", hint: "Produce new or original work" },
] as const

/** Cognitive level — broader bucketing for analytics. */
export const AUTHORING_COG_LEVEL_OPTIONS = [
  { value: "recall", label: "Recall", hint: "Definitions and core concepts" },
  { value: "application", label: "Application", hint: "Apply concept to a new scenario" },
  { value: "analysis", label: "Analysis", hint: "Compare, evaluate, synthesise" },
] as const
export type AuthoringCogLevel = typeof AUTHORING_COG_LEVEL_OPTIONS[number]["value"]

// ─────────────────────────────────────────────────────────────────────────────
// Category / subject / track / phase
// ─────────────────────────────────────────────────────────────────────────────

export const AUTHORING_SUBJECT_AREAS = [
  "Category 1",
  "Category 2",
  "Category 3",
  "Category 4",
  "Category 5",
  "Category 6",
  "Category 7",
  "Category 8",
  "Category 9",
  "Category 10",
] as const
export type AuthoringSubjectArea = typeof AUTHORING_SUBJECT_AREAS[number]

export const AUTHORING_BODY_SYSTEMS = [
  { value: "topic-a", label: "Topic A", icon: "fa-circle" },
  { value: "topic-b", label: "Topic B", icon: "fa-circle" },
  { value: "topic-c", label: "Topic C", icon: "fa-circle" },
  { value: "topic-d", label: "Topic D", icon: "fa-circle" },
  { value: "topic-e", label: "Topic E", icon: "fa-circle" },
  { value: "topic-f", label: "Topic F", icon: "fa-circle" },
  { value: "topic-g", label: "Topic G", icon: "fa-circle" },
  { value: "topic-h", label: "Topic H", icon: "fa-circle" },
  { value: "topic-i", label: "Topic I", icon: "fa-circle" },
  { value: "topic-j", label: "Topic J", icon: "fa-circle" },
  { value: "topic-k", label: "Topic K", icon: "fa-circle" },
  { value: "topic-l", label: "Topic L", icon: "fa-circle" },
  { value: "topic-multi", label: "Multi-topic", icon: "fa-circle-nodes" },
] as const
export type AuthoringBodySystem = typeof AUTHORING_BODY_SYSTEMS[number]["value"]

export const AUTHORING_DISCIPLINES = [
  "Track 1",
  "Track 2",
  "Track 3",
  "Track 4",
  "Track 5",
  "Track 6",
  "Track 7",
  "Track 8",
  "Track 9",
  "Track 10",
  "Track 11",
] as const
export type AuthoringDiscipline = typeof AUTHORING_DISCIPLINES[number]

export const AUTHORING_PHASES = [
  { value: "preclinical", label: "Phase 1", hint: "Early stage" },
  { value: "clinical", label: "Phase 2", hint: "Practice stage" },
  { value: "residency", label: "Phase 3", hint: "Advanced stage" },
  { value: "ce", label: "Phase 4", hint: "Ongoing stage" },
] as const
export type AuthoringPhase = typeof AUTHORING_PHASES[number]["value"]

// ─────────────────────────────────────────────────────────────────────────────
// Workflow status
// ─────────────────────────────────────────────────────────────────────────────

export type AuthoringStatus = "draft" | "in_review" | "approved" | "published" | "retired"

export const AUTHORING_STATUS_OPTIONS: readonly { value: AuthoringStatus; label: string; description: string; icon: string }[] = [
  {
    value: "draft",
    label: "Draft",
    description: "Visible only to owners. Not eligible for use.",
    icon: "fa-file-pen",
  },
  {
    value: "in_review",
    label: "In review",
    description: "Awaiting peer review.",
    icon: "fa-magnifying-glass",
  },
  {
    value: "approved",
    label: "Approved",
    description: "Reviewer signed off. Ready to publish.",
    icon: "fa-circle-check",
  },
  {
    value: "published",
    label: "Published",
    description: "Live in the library. Available for use.",
    icon: "fa-broadcast-tower",
  },
  {
    value: "retired",
    label: "Retired",
    description: "Removed from active use. Kept for history.",
    icon: "fa-box-archive",
  },
] as const

// ─────────────────────────────────────────────────────────────────────────────
// Defaults
// ─────────────────────────────────────────────────────────────────────────────

export const AUTHORING_DEFAULT_OPTION_COUNT = 5
export const AUTHORING_MIN_OPTION_COUNT = 2
export const AUTHORING_MAX_OPTION_COUNT = 8

/** Name placeholder — generic primary line for the demo composer. */
export const AUTHORING_STEM_PLACEHOLDER = `Item 13 — sample primary line for a new library entry. Describe the item in one or two sentences so respondents understand the prompt without needing extra context. Use placeholder content that demonstrates the layout without committing to a specific product domain.`

export const AUTHORING_LEAD_IN_PLACEHOLDER =
  "Which option fits best?"

/** Best-practice notes prompt — owner-side, not respondent-facing. */
export const AUTHORING_RATIONALE_PLACEHOLDER =
  "Explain why this option is correct (or why this distractor is plausible). Cite the source or reference where possible."

/**
 * Draft item handle assigned when the composer route loads.
 * Format: `LIB-YYMM-XXXX` (e.g. `LIB-2605-A3F2`). Generate on the server and pass
 * into the composer so SSR and hydration share one value.
 */
export function generateDraftQuestionId(now = new Date()): string {
  const yy = String(now.getFullYear()).slice(-2)
  const mm = String(now.getMonth() + 1).padStart(2, "0")
  const rand = Math.random().toString(16).slice(2, 6).toUpperCase().padEnd(4, "0")
  return `LIB-${yy}${mm}-${rand}`
}
