/**
 * i18n module — types and schemas.
 * Translation management with locale/namespace structure and fallback chain.
 */

import { z } from 'zod';

export interface TranslationDoc {
  id: string; // `${locale}:${key}`
  locale: string; // partition key — e.g. 'en', 'fr', 'ja'
  key: string; // dot-separated: 'common.save', 'dashboard.title'
  namespace: string; // first segment of key: 'common', 'dashboard'
  value: string;
  productId: string;
  createdAt: string;
  updatedAt: string;
}

export interface LocaleDoc {
  id: string; // locale code
  locale: string; // partition key (same as id)
  name: string; // 'English', 'Français'
  nativeName: string; // 'English', 'Français'
  direction: 'ltr' | 'rtl';
  productId: string;
  enabled: boolean;
  createdAt: string;
  updatedAt: string;
}

// ── Schemas ────────────────────────────────────────────────────

export const UpsertTranslationSchema = z.object({
  value: z.string().min(1).max(5000),
});

export const BulkImportSchema = z.object({
  translations: z.record(z.string().min(1).max(5000)), // { "common.save": "Save", ... }
  overwrite: z.boolean().default(false),
});

export const CreateLocaleSchema = z.object({
  locale: z
    .string()
    .min(2)
    .max(10)
    .regex(/^[a-z]{2}(-[A-Z]{2})?$/),
  name: z.string().min(1).max(100),
  nativeName: z.string().min(1).max(100),
  direction: z.enum(['ltr', 'rtl']).default('ltr'),
  enabled: z.boolean().default(true),
});

export type UpsertTranslationInput = z.infer<typeof UpsertTranslationSchema>;
export type BulkImportInput = z.infer<typeof BulkImportSchema>;
export type CreateLocaleInput = z.infer<typeof CreateLocaleSchema>;
