import * as z from "zod";

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

export const Term = z.object({
  id: z.uuid(),
  synset_id: z.string(),
  pos: z.string(),
  created_at: z.iso.datetime(),
});

export type Term = z.infer<typeof Term>;

export const Translation = z.object({
  id: z.uuid(),
  term_id: z.string(),
  language_code: z.string(),
  text: z.string(),
  created_at: z.iso.datetime(),
});

export type Translation = z.infer<typeof Translation>;

export const TermGloss = z.object({
  id: z.uuid(),
  term_id: z.uuid(),
  language_code: z.string(),
  text: z.string(),
  created_at: z.iso.datetime(),
});

export type TermGloss = z.infer<typeof TermGloss>;

export const LanguagePair = z.object({
  id: z.uuid(),
  source_language: z.string(),
  target_language: z.string(),
  label: z.string(),
  active: z.boolean(),
  created_at: z.iso.datetime(),
});

export type LanguagePair = z.infer<typeof LanguagePair>;

export const User = z.object({
  id: z.uuid(),
  openauth_sub: z.string(),
  email: z.email(),
  display_name: z.string(),
  created_at: z.iso.datetime(),
  last_login_at: z.iso.datetime(),
});

export type User = z.infer<typeof User>;

export const Deck = z.object({
  id: z.uuid(),
  name: z.string(),
  description: z.string(),
  source_language: z.string(),
  validated_languages: z.array(z.string()),
  is_public: z.boolean(),
  created_at: z.iso.datetime(),
});

export type Deck = z.infer<typeof Deck>;

export const DeckTerm = z.object({
  deck_id: z.uuid(),
  term_id: z.uuid(),
  added: z.iso.datetime(),
});

export type DeckTerm = z.infer<typeof DeckTerm>;

export const GameRequestSchema = z
  .object({
    source_language: z.enum(SUPPORTED_LANGUAGE_CODES),
    target_language: z.enum(SUPPORTED_LANGUAGE_CODES),
    pos: z.enum(SUPPORTED_POS),
    rounds: z.enum(GAME_ROUNDS).transform(Number),
  })
  .refine((game) => game.target_language !== game.source_language, {
    error: "source and target language must be different",
  });

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