import { z } from 'zod';

// ---------------------------------------------------------------------------
// Micro learning types
// ---------------------------------------------------------------------------

export const MICRO_LEARNING_TYPES = [
  'concept_explainer',
  'scenario_quiz',
  'misconceptions',
  'how_to',
  'comparison_card',
  'reflection_prompt',
  'flashcard_set',
  'case_study',
  'glossary_anchor',
  'myth_vs_evidence',
] as const;

export type MicroLearningType = (typeof MICRO_LEARNING_TYPES)[number];

// ---------------------------------------------------------------------------
// Content schemas — validated against AI output before PocketBase write
// ---------------------------------------------------------------------------

export const ConceptExplainerSchema = z.object({
  paragraphs: z.array(z.string().min(10)).min(2).max(3),
  example: z.string().min(20),
});

export const ScenarioQuizSchema = z.object({
  scenario: z.string().min(30),
  options: z
    .array(
      z.object({
        label: z.enum(['A', 'B', 'C', 'D']),
        text: z.string().min(5),
        correct: z.boolean(),
        explanation: z.string().min(10),
      }),
    )
    .length(4)
    .refine(opts => opts.filter(o => o.correct).length === 1, {
      message: 'exactly one correct option required',
    }),
});

export const MisconceptionsSchema = z.object({
  items: z
    .array(
      z.object({
        misconception: z.string().min(10),
        correction: z.string().min(10),
      }),
    )
    .min(3)
    .max(5),
});

export const HowToSchema = z.object({
  steps: z
    .array(
      z.object({
        number: z.number().int().positive(),
        instruction: z.string().min(10),
      }),
    )
    .min(3)
    .max(8),
});

export const ComparisonCardSchema = z.object({
  subject_a: z.string().min(2),
  subject_b: z.string().min(2),
  dimensions: z
    .array(
      z.object({
        label: z.string().min(2),
        a: z.string().min(5),
        b: z.string().min(5),
      }),
    )
    .min(3)
    .max(6),
});

export const ReflectionPromptSchema = z.object({
  prompt: z.string().min(20),
  model_answer: z.string().min(50),
});

export const FlashcardSetSchema = z.object({
  cards: z
    .array(
      z.object({
        question: z.string().min(5),
        answer: z.string().min(5),
      }),
    )
    .min(5)
    .max(10),
});

export const CaseStudySchema = z.object({
  scenario: z.string().min(150),
  questions: z.array(z.string().min(10)).min(2).max(4),
});

export const GlossaryAnchorSchema = z.object({
  term: z.string().min(2),
  definition: z.string().min(20),
  correct_use: z.string().min(20),
  misuse: z.string().min(20),
});

export const MythVsEvidenceSchema = z.object({
  myth: z.string().min(20),
  evidence: z.string().min(30),
  sources: z.array(z.string()),
});

// Map type → schema for lookup
export const CONTENT_SCHEMAS: Record<MicroLearningType, z.ZodTypeAny> = {
  concept_explainer: ConceptExplainerSchema,
  scenario_quiz: ScenarioQuizSchema,
  misconceptions: MisconceptionsSchema,
  how_to: HowToSchema,
  comparison_card: ComparisonCardSchema,
  reflection_prompt: ReflectionPromptSchema,
  flashcard_set: FlashcardSetSchema,
  case_study: CaseStudySchema,
  glossary_anchor: GlossaryAnchorSchema,
  myth_vs_evidence: MythVsEvidenceSchema,
};

// Map type → human-readable label for prompts
export const TYPE_LABELS: Record<MicroLearningType, string> = {
  concept_explainer: 'Concept Explainer',
  scenario_quiz: 'Scenario Quiz',
  misconceptions: 'Misconceptions',
  how_to: 'How-To Guide',
  comparison_card: 'Comparison Card',
  reflection_prompt: 'Reflection Prompt',
  flashcard_set: 'Flashcard Set',
  case_study: 'Case Study',
  glossary_anchor: 'Glossary Anchor',
  myth_vs_evidence: 'Myth vs Evidence',
};

// ---------------------------------------------------------------------------
// PocketBase: Topic (fetched from PB before generation)
// ---------------------------------------------------------------------------

export interface TopicRecord {
  id: string;
  title: string;
  body: string;
  difficulty: 'introductory' | 'intermediate' | 'advanced';
  key_terms: string[];
  status: string;
}

// ---------------------------------------------------------------------------
// Job system
// ---------------------------------------------------------------------------

export type JobStatus = 'queued' | 'running' | 'done' | 'failed';

export interface JobProgress {
  topicsTotal: number;
  topicsProcessed: number;
  itemsTotal: number;
  itemsGenerated: number;
  itemsFailed: number;
}

export interface GenerationJob {
  id: string;
  themeId: string;
  status: JobStatus;
  progress: JobProgress;
  error: string | null;
  createdAt: Date;
  updatedAt: Date;
}

// ---------------------------------------------------------------------------
// API request schemas (Zod — validates external input)
// ---------------------------------------------------------------------------

export const GenerateBodySchema = z.object({
  themeId: z.string().min(1),
});

export type GenerateBody = z.infer<typeof GenerateBodySchema>;

export const PublishBodySchema = z.object({
  status: z.enum(['published', 'rejected']),
});

export type PublishBody = z.infer<typeof PublishBodySchema>;
