import * as z from "zod";

import {
  SUPPORTED_LANGUAGE_CODES,
  SUPPORTED_POS,
  GAME_ROUNDS,
  DIFFICULTY_LEVELS,
} from "../constants.js";

export const GameRequestSchema = z.object({
  source_language: z.enum(SUPPORTED_LANGUAGE_CODES),
  target_language: z.enum(SUPPORTED_LANGUAGE_CODES),
  pos: z.enum(SUPPORTED_POS),
  difficulty: z.enum(DIFFICULTY_LEVELS),
  rounds: z.literal(GAME_ROUNDS),
});

export type GameRequest = z.infer<typeof GameRequestSchema>;

export const AnswerOptionSchema = z.object({
  optionId: z.number().int().min(0).max(3),
  text: z.string().min(1),
});

export type AnswerOption = z.infer<typeof AnswerOptionSchema>;

export const GameQuestionSchema = z.object({
  questionId: z.uuid(),
  prompt: z.string().min(1),
  gloss: z.string().min(1).nullable(),
  options: z.array(AnswerOptionSchema).length(4),
});

export type GameQuestion = z.infer<typeof GameQuestionSchema>;

export const GameSessionSchema = z.object({
  sessionId: z.uuid(),
  questions: z.array(GameQuestionSchema),
});

export type GameSession = z.infer<typeof GameSessionSchema>;

export const AnswerSubmissionSchema = z.object({
  sessionId: z.uuid(),
  questionId: z.uuid(),
  selectedOptionId: z.number().int().min(0).max(3),
});

export type AnswerSubmission = z.infer<typeof AnswerSubmissionSchema>;

export const AnswerResultSchema = z.object({
  questionId: z.uuid(),
  isCorrect: z.boolean(),
  correctOptionId: z.number().int().min(0).max(3),
  selectedOptionId: z.number().int().min(0).max(3),
});

export type AnswerResult = z.infer<typeof AnswerResultSchema>;
