import { z } from "zod";

// Core domain types matching database schema

export interface Word {
  id: number;
  english: string;
  hindi: string;
  note?: string | null;
  examples?: Example[];
  tags?: Tag[];
  seeAlso?: SeeAlso[];
}

export interface Example {
  id: number;
  english: string;
  hindi: string;
  note?: string | null;
}

export type TagLevel = 'page' | 'subheading' | 'grammar' | 'gender';

export interface Tag {
  id: number;
  name: string;
  description?: string | null;
  level: TagLevel;
  parentPageId?: number | null;
}

export interface SeeAlso {
  id: number;
  reference: string;
  note?: string | null;
}

// Input types for creating new records

export interface NewWord {
  hindi: string;
  english: string;
  note?: string;
  examples?: NewExample[];
  tagIds?: number[]; // Now includes grammar and gender tags
  seeAlso?: NewSeeAlso[];
}

export interface NewExample {
  hindi: string;
  english: string;
  note?: string;
}

export interface NewTag {
  name: string;
  description?: string;
  level: TagLevel;
  parentPageId?: number;
}

export interface NewSeeAlso {
  reference: string;
  note?: string;
}

// Zod schemas for validation

export const exampleSchema = z.object({
  id: z.number(),
  english: z.string(),
  hindi: z.string(),
  note: z.string().optional().nullable(),
});

export const tagSchema = z.object({
  id: z.number(),
  name: z.string(),
  description: z.string().optional().nullable(),
  level: z.enum(['page', 'subheading', 'grammar', 'gender']),
  parentPageId: z.number().optional().nullable(),
});

export const seeAlsoSchema = z.object({
  id: z.number(),
  reference: z.string(),
  note: z.string().optional().nullable(),
});

export const wordSchema = z.object({
  id: z.number(),
  english: z.string(),
  hindi: z.string(),
  note: z.string().optional().nullable(),
  examples: z.array(exampleSchema).optional(),
  tags: z.array(tagSchema).optional(),
  seeAlso: z.array(seeAlsoSchema).optional(),
});

export type WordSchema = z.infer<typeof wordSchema>;
export type ExampleSchema = z.infer<typeof exampleSchema>;
export type TagSchema = z.infer<typeof tagSchema>;
export type SeeAlsoSchema = z.infer<typeof seeAlsoSchema>;

// Legacy aliases for backward compatibility
export type VocabWord = Word;
export const vocabWordSchema = wordSchema;
export type VocabWordSchema = WordSchema;

// Legacy type for backward compatibility during migration
export interface VocabList {
  slug: string;
  about: string;
  words: Word[];
}
