import { z } from 'zod';

// ---------------------------------------------------------------------------
// KB snapshot — input to generator
// ---------------------------------------------------------------------------

export const KBTopicSchema = z.object({
  id: z.string(),
  title: z.string(),
  complexityWeight: z.number().min(1).max(5),
  difficulty: z.string(),
  prerequisiteTopics: z.array(z.string()),
  relatedTopics: z.array(z.string()),
  contrastTopics: z.array(z.string()),
});

export const KBThemeSchema = z.object({
  id: z.string(),
  title: z.string(),
  description: z.string(),
  topics: z.array(KBTopicSchema),
});

export const KBSnapshotSchema = z.object({
  themes: z.array(KBThemeSchema),
});

export type KBTopic = z.infer<typeof KBTopicSchema>;
export type KBTheme = z.infer<typeof KBThemeSchema>;
export type KBSnapshot = z.infer<typeof KBSnapshotSchema>;

// ---------------------------------------------------------------------------
// Curriculum draft — AI output
// ---------------------------------------------------------------------------

export const CurriculumWeekDraftSchema = z.object({
  weekNumber: z.number().min(1).max(26),
  themeId: z.string(),
  topicIds: z.array(z.string()),
  estimatedDurationMinutes: z.number().min(15).max(45),
  rationale: z.string(),
});

export const CurriculumDraftSchema = z.object({
  weeks: z.array(CurriculumWeekDraftSchema).length(26),
});

export type CurriculumWeekDraft = z.infer<typeof CurriculumWeekDraftSchema>;
export type CurriculumDraft = z.infer<typeof CurriculumDraftSchema>;

// ---------------------------------------------------------------------------
// Employee state
// ---------------------------------------------------------------------------

export interface EmployeeState {
  userId: string;
  currentCycle: number;
  currentWeek: number;
  startDate: string;
  activeVersionId: string;
  nextSessionTheme: { id: string; title: string } | null;
  nextSessionTopics: { id: string; title: string; complexityWeight: number }[];
}

// ---------------------------------------------------------------------------
// Job tracking
// ---------------------------------------------------------------------------

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

export interface GenerationJob {
  id: string;
  triggeredBy: string;
  reason: 'new_topics' | 'manual';
  status: JobStatus;
  versionId: string | null;
  error: string | null;
  createdAt: Date;
  updatedAt: Date;
}

// ---------------------------------------------------------------------------
// Cycle variant context
// ---------------------------------------------------------------------------

export interface CycleContext {
  cycleNumber: number;
  employeeHistory: {
    typesUsed: string[];
    typesNotUsed: string[];
    lowEngagementTopics: string[];
  };
}

// ---------------------------------------------------------------------------
// PocketBase record shapes
// ---------------------------------------------------------------------------

export interface CurriculumVersionRecord {
  id: string;
  version: number;
  status: 'draft' | 'active' | 'superseded';
  generated_at: string;
  generation_notes: string;
}

export interface CurriculumWeekRecord {
  id: string;
  curriculum_version: string;
  week_number: number;
  theme: string;
  topics: string[];
  topic_order: number[];
  estimated_duration_minutes: number;
  admin_notes: string;
}

export interface EmployeeStateRecord {
  id: string;
  user: string;
  current_cycle: number;
  current_week: number;
  start_date: string;
  active_version: string;
}
