import { z } from "zod";
import { LanguageTypes } from "@/validations/mutual/language/validations";
import { applicationFieldsTr } from "@/languages/custom/application/turkish";
import { applicationFieldsEn } from "@/languages/custom/application/english";

interface ApplicationData {
  uu_id: string;
  name: string;
  application_code: string;
  site_url: string;
  application_type: string;
  application_for?: string;
  description?: string;
  active: boolean;
  deleted?: boolean;
  created_at?: string;
  updated_at?: string;
}

const labelTranslations = {
  tr: applicationFieldsTr,
  en: applicationFieldsEn,
};

const errorMessages = {
  en: {
    nameRequired: "Name is required",
    applicationCodeRequired: "Application code is required",
    siteUrlRequired: "Site URL is required",
    applicationTypeRequired: "Application type is required",
  },
  tr: {
    nameRequired: "İsim gereklidir",
    applicationCodeRequired: "Uygulama kodu gereklidir",
    siteUrlRequired: "Site URL'si gereklidir",
    applicationTypeRequired: "Uygulama tipi gereklidir",
  },
};

function getSchemaByLanguage(lang: LanguageTypes) {
  const createSchema = z.object({
    // Identification fields
    uu_id: z.string().optional(),
    name: z.string().min(1, errorMessages[lang].nameRequired),
    application_code: z
      .string()
      .min(1, errorMessages[lang].applicationCodeRequired),

    // Application details
    site_url: z.string().min(1, errorMessages[lang].siteUrlRequired),
    application_type: z
      .string()
      .min(1, errorMessages[lang].applicationTypeRequired),
    application_for: z.string().optional(),
    description: z.string().optional(),

    // Status fields
    active: z.boolean().default(true),
    deleted: z.boolean().default(false),

    // System fields
    created_at: z.string().optional(),
    updated_at: z.string().optional(),
  });

  const updateSchema = z.object({
    // Identification fields
    uu_id: z.string().optional(),
    name: z.string().min(1, errorMessages[lang].nameRequired),
    application_code: z
      .string()
      .min(1, errorMessages[lang].applicationCodeRequired),

    // Application details
    site_url: z.string().min(1, errorMessages[lang].siteUrlRequired),
    application_type: z
      .string()
      .min(1, errorMessages[lang].applicationTypeRequired),
    application_for: z.string().optional(),
    description: z.string().optional(),

    // Status fields
    active: z.boolean().default(true),
    deleted: z.boolean().default(false),

    // System fields
    created_at: z.string().optional(),
    updated_at: z.string().optional(),
  });

  const detailSchema = z.object({
    uu_id: z.string(),
    name: z.string(),
    application_code: z.string(),
    site_url: z.string(),
    application_type: z.string(),
    application_for: z.string().optional(),
    description: z.string().optional(),
    active: z.boolean(),
    deleted: z.boolean(),
    created_at: z.string(),
    updated_at: z.string(),
  });

  const shortSchema = z.object({
    uu_id: z.string(),
    name: z.string(),
    application_code: z.string(),
    site_url: z.string(),
  });

  const columns = Object.keys(detailSchema.shape);
  const shortColumns = Object.keys(shortSchema.shape);

  const setColumns = columns.map((column) => {
    return labelTranslations[lang][
      column as keyof (typeof labelTranslations)[typeof lang]
    ];
  });
  const setShortColumns = shortColumns.map((column) => {
    return labelTranslations[lang][
      column as keyof (typeof labelTranslations)[typeof lang]
    ];
  });
  return {
    createSchema,
    updateSchema,
    detailSchema,
    shortSchema,
    labels: labelTranslations[lang],
    columns: setColumns,
    shortColumns: setShortColumns,
  };
}

export { getSchemaByLanguage };
