import { CatalogEntityKind } from '@prisma/client';

export type CatalogTranslationSeed = {
  entityKind: CatalogEntityKind;
  entityCode: string;
  locale: string;
  label: string;
};

export type TreatmentTypeSeed = {
  code: string;
  labDependent: boolean;
  sortOrder: number;
  /** Selectable when booking an appointment. Defaults to true. */
  availableInAppointments?: boolean;
  /** Selectable as a treatment plan detail. Defaults to true. */
  availableInTreatment?: boolean;
};

export const TREATMENT_TYPES: readonly TreatmentTypeSeed[] = [
  { code: 'restoration', labDependent: false, sortOrder: 1 },
  { code: 'specialized_restoration', labDependent: false, sortOrder: 2 },
  { code: 'radiography', labDependent: false, sortOrder: 3 },
  { code: 'endo', labDependent: false, sortOrder: 4 },
  { code: 'surgery', labDependent: false, sortOrder: 5 },
  { code: 'prosthesis', labDependent: true, sortOrder: 6 },
  { code: 'implant', labDependent: false, sortOrder: 7 },
  { code: 'orthodontics', labDependent: false, sortOrder: 8 },
  { code: 'perio', labDependent: false, sortOrder: 9 },
  { code: 'pediatrics', labDependent: false, sortOrder: 10 },
  { code: 'extraction', labDependent: false, sortOrder: 11 },
  // Appointment-only: not real treatment plan details.
  {
    code: 'clinic_visit',
    labDependent: false,
    sortOrder: 12,
    availableInTreatment: false,
  },
  {
    code: 'continue_treatment',
    labDependent: false,
    sortOrder: 13,
    availableInTreatment: false,
  },
] as const;

/** Legacy codes kept for historical rows; hidden from catalog. */
export const LEGACY_TREATMENT_TYPES: readonly TreatmentTypeSeed[] = [
  { code: 'consultation', labDependent: false, sortOrder: 99 },
  { code: 'filling', labDependent: false, sortOrder: 100 },
  { code: 'visit', labDependent: false, sortOrder: 101 },
  { code: 'hygiene', labDependent: false, sortOrder: 102 },
] as const;

export const LAB_WORKFLOW_STEPS = [
  { code: 'intraoral_scan', sortOrder: 1 },
  { code: 'design', sortOrder: 2 },
  { code: 'milling_dry', sortOrder: 3 },
  { code: 'milling_wet', sortOrder: 4 },
  { code: 'printer_resin', sortOrder: 5 },
  { code: 'printer_metal', sortOrder: 6 },
  { code: 'sinter', sortOrder: 7 },
  { code: 'build_up', sortOrder: 8 },
  { code: 'stain', sortOrder: 9 },
  { code: 'glaze', sortOrder: 10 },
  { code: 'polish_prep', sortOrder: 11 },
  { code: 'packing', sortOrder: 12 },
  { code: 'shipping', sortOrder: 13 },
  { code: 'pressing', sortOrder: 14 },
] as const;

export type ProsthesisTypeSeed = {
  code: string;
  sortOrder: number;
  skipPackingShipping?: boolean;
  /** Manufacturing steps between design and packing (exclusive of universal scan/design/pack/ship). */
  manufacturingSteps: readonly string[];
};

export const PROSTHESIS_TYPES: ProsthesisTypeSeed[] = [
  {
    code: 'pfm_crown',
    sortOrder: 1,
    manufacturingSteps: ['milling_wet', 'build_up', 'stain', 'glaze', 'polish_prep'],
  },
  {
    code: 'pfz_crown',
    sortOrder: 2,
    manufacturingSteps: ['milling_dry', 'sinter', 'build_up', 'stain', 'glaze', 'polish_prep'],
  },
  {
    code: 'monolithic_zirconia',
    sortOrder: 3,
    manufacturingSteps: ['milling_dry', 'sinter', 'stain', 'glaze', 'polish_prep'],
  },
  {
    code: 'glass_ceramic_crown',
    sortOrder: 4,
    manufacturingSteps: ['milling_wet', 'stain', 'glaze', 'polish_prep'],
  },
  {
    code: 'full_metal_crown',
    sortOrder: 5,
    manufacturingSteps: ['milling_wet', 'polish_prep'],
  },
  {
    code: 'temporary_resin_crown',
    sortOrder: 6,
    manufacturingSteps: ['milling_wet', 'polish_prep'],
  },
  {
    code: 'pmma',
    sortOrder: 7,
    manufacturingSteps: ['milling_dry', 'polish_prep'],
  },
  {
    code: 'peek_crown',
    sortOrder: 8,
    manufacturingSteps: ['milling_dry', 'polish_prep'],
  },
  {
    code: 'veneer_zirconia',
    sortOrder: 9,
    manufacturingSteps: ['milling_dry', 'sinter', 'build_up', 'stain', 'glaze', 'polish_prep'],
  },
  {
    code: 'veneer_ips_press',
    sortOrder: 10,
    manufacturingSteps: [
      'printer_resin',
      'build_up',
      'stain',
      'glaze',
      'polish_prep',
      'pressing',
    ],
  },
  {
    code: 'veneer_ips_cad',
    sortOrder: 11,
    manufacturingSteps: ['milling_wet', 'stain', 'glaze', 'polish_prep'],
  },
  {
    code: 'soft_structure',
    sortOrder: 12,
    manufacturingSteps: ['milling_dry', 'sinter'],
  },
  {
    code: 'customized_abutment',
    sortOrder: 13,
    manufacturingSteps: ['milling_wet', 'polish_prep'],
  },
  {
    code: 'prefabricated_abutment',
    sortOrder: 14,
    manufacturingSteps: ['polish_prep'],
  },
  {
    code: 'ti_base_abutment',
    sortOrder: 15,
    manufacturingSteps: ['polish_prep'],
  },
  {
    code: 'multi_unit_abutment',
    sortOrder: 16,
    manufacturingSteps: ['polish_prep'],
  },
  {
    code: 'zirconia_abutment',
    sortOrder: 17,
    manufacturingSteps: ['milling_dry', 'sinter', 'polish_prep'],
  },
  {
    code: 'screw_retained',
    sortOrder: 18,
    manufacturingSteps: [
      'milling_wet',
      'printer_metal',
      'sinter',
      'build_up',
      'stain',
      'glaze',
      'polish_prep',
    ],
  },
  {
    code: 'zirconia_overlay',
    sortOrder: 19,
    manufacturingSteps: ['milling_dry', 'sinter', 'stain', 'glaze', 'polish_prep'],
  },
  {
    code: 'ips_overlay',
    sortOrder: 20,
    manufacturingSteps: ['milling_wet', 'stain', 'glaze', 'polish_prep'],
  },
  {
    code: 'smile_design',
    sortOrder: 21,
    skipPackingShipping: true,
    manufacturingSteps: ['printer_resin'],
  },
  {
    code: 'mockup',
    sortOrder: 22,
    manufacturingSteps: ['printer_resin'],
  },
];

const UNIVERSAL_PREFIX = ['intraoral_scan', 'design'] as const;
const UNIVERSAL_SUFFIX = ['packing', 'shipping'] as const;

export function buildProsthesisStepCodes(type: ProsthesisTypeSeed): string[] {
  const steps = [...UNIVERSAL_PREFIX, ...type.manufacturingSteps];
  if (!type.skipPackingShipping) {
    steps.push(...UNIVERSAL_SUFFIX);
  }
  return steps;
}

const TREATMENT_LABELS: Record<string, Record<string, string>> = {
  restoration: { en: 'Restoration', fa: 'ترمیم', nl: 'Restauratie' },
  specialized_restoration: { en: 'Specialized Restoration', fa: 'ترمیم تخصصی', nl: 'Gespecialiseerde Restauratie' },
  radiography: { en: 'Radiography', fa: 'رادیوگرافی', nl: 'Röntgen' },
  endo: { en: 'Endo', fa: 'اندو', nl: 'Endo' },
  surgery: { en: 'Surgery', fa: 'جراحی', nl: 'Chirurgie' },
  prosthesis: { en: 'Prosthesis', fa: 'پروتز', nl: 'Prothese' },
  implant: { en: 'Implant', fa: 'ایمپلنت', nl: 'Implantaat' },
  orthodontics: { en: 'Orthodontics', fa: 'ارتودنسی', nl: 'Orthodontie' },
  perio: { en: 'Perio', fa: 'پریو', nl: 'Paro' },
  pediatrics: { en: 'Pediatrics', fa: 'اطفال', nl: 'Kinderen' },
  extraction: { en: 'Extraction', fa: 'کشیدن', nl: 'Extractie' },
  clinic_visit: { en: 'Clinic Visit', fa: 'ویزیت درمانگاه', nl: 'Kliniekbezoek' },
  continue_treatment: { en: 'Continue Treatment', fa: 'ادامه درمان', nl: 'Behandeling Voortzetten' },
  consultation: { en: 'Consultation', fa: 'مشاوره', nl: 'Consult' },
  filling: { en: 'Filling', fa: 'پر کردن', nl: 'Vulling' },
  visit: { en: 'Visit', fa: 'ویزیت', nl: 'Bezoek' },
  hygiene: { en: 'Hygiene', fa: 'بهداشت', nl: 'Hygiëne' },
};

const PROSTHESIS_LABELS: Record<string, Record<string, string>> = {
  pfm_crown: { en: 'PFM Crown', fa: 'روکش PFM', nl: 'PFM Kroon' },
  pfz_crown: { en: 'PFZ Crown', fa: 'روکش PFZ', nl: 'PFZ Kroon' },
  monolithic_zirconia: { en: 'Monolithic Zirconia', fa: 'زیرکونیا مونولیتیک', nl: 'Monolithisch Zirconia' },
  glass_ceramic_crown: { en: 'Glass Ceramic Crown', fa: 'روکش سرامیک شیشه‌ای', nl: 'Glaskeramische Kroon' },
  full_metal_crown: { en: 'Full Metal Crown', fa: 'روکش تمام فلز', nl: 'Volledige Metalen Kroon' },
  temporary_resin_crown: { en: 'Temporary Resin Crown', fa: 'روکش موقت رزینی', nl: 'Tijdelijke Harskroon' },
  pmma: { en: 'PMMA', fa: 'PMMA', nl: 'PMMA' },
  peek_crown: { en: 'PEEK Crown', fa: 'روکش PEEK', nl: 'PEEK Kroon' },
  veneer_zirconia: { en: 'Veneer Zirconia', fa: 'ونیر زیرکونیا', nl: 'Veneer Zirconia' },
  veneer_ips_press: { en: 'Veneer IPS Press', fa: 'ونیر IPS پرس', nl: 'Veneer IPS Press' },
  veneer_ips_cad: { en: 'Veneer IPS CAD', fa: 'ونیر IPS CAD', nl: 'Veneer IPS CAD' },
  soft_structure: { en: 'Soft Structure', fa: 'ساختار نرم', nl: 'Zachte Structuur' },
  customized_abutment: { en: 'Customized Abutment', fa: 'اباتمنت سفارشی', nl: 'Aangepast Abutment' },
  prefabricated_abutment: { en: 'Prefabricated Abutment', fa: 'اباتمنت آماده', nl: 'Prefab Abutment' },
  ti_base_abutment: { en: 'Ti Base Abutment', fa: 'اباتمنت پایه تیتانیوم', nl: 'Ti Basis Abutment' },
  multi_unit_abutment: { en: 'Multi Unit Abutment', fa: 'اباتمنت مولتی یونیت', nl: 'Multi Unit Abutment' },
  zirconia_abutment: { en: 'Zirconia Abutment', fa: 'اباتمنت زیرکونیا', nl: 'Zirconia Abutment' },
  screw_retained: { en: 'Screw Retained', fa: 'پیچی', nl: 'Schroefgehouden' },
  zirconia_overlay: { en: 'Zirconia Overlay', fa: 'اورلی زیرکونیا', nl: 'Zirconia Overlay' },
  ips_overlay: { en: 'IPS Overlay', fa: 'اورلی IPS', nl: 'IPS Overlay' },
  smile_design: { en: 'Smile Design', fa: 'طراحی لبخند', nl: 'Smile Design' },
  mockup: { en: 'Mockup', fa: 'ماکاپ', nl: 'Mockup' },
};

const WORKFLOW_STEP_LABELS: Record<string, Record<string, string>> = {
  intraoral_scan: { en: 'Intraoral Scan', fa: 'اسکن داخل دهان', nl: 'Intraorale Scan' },
  design: { en: 'Design', fa: 'طراحی', nl: 'Ontwerp' },
  milling_dry: { en: 'Milling Dry', fa: 'فرز خشک', nl: 'Droog Frezen' },
  milling_wet: { en: 'Milling Wet', fa: 'فرز تر', nl: 'Nat Frezen' },
  printer_resin: { en: 'Printer Resin', fa: 'پرینتر رزین', nl: 'Harsprinter' },
  printer_metal: { en: 'Printer Metal', fa: 'پرینتر فلز', nl: 'Metaalprinter' },
  sinter: { en: 'Sinter', fa: 'سینتر', nl: 'Sinteren' },
  build_up: { en: 'Build Up', fa: 'بیلدآپ', nl: 'Opbouw' },
  stain: { en: 'Stain', fa: 'رنگ‌آمیزی', nl: 'Kleuren' },
  glaze: { en: 'Glaze', fa: 'گلیز', nl: 'Glazuur' },
  polish_prep: { en: 'Polish/Prep', fa: 'پولیش/آماده‌سازی', nl: 'Polijsten/Voorbereiding' },
  packing: { en: 'Packing', fa: 'بسته‌بندی', nl: 'Verpakken' },
  shipping: { en: 'Shipping', fa: 'ارسال', nl: 'Verzending' },
  pressing: { en: 'Pressing', fa: 'پرس', nl: 'Persen' },
};

function labelsToTranslations(
  entityKind: CatalogEntityKind,
  labels: Record<string, Record<string, string>>,
): CatalogTranslationSeed[] {
  const out: CatalogTranslationSeed[] = [];
  for (const [entityCode, locales] of Object.entries(labels)) {
    for (const [locale, label] of Object.entries(locales)) {
      out.push({ entityKind, entityCode, locale, label });
    }
  }
  return out;
}

export const CATALOG_TRANSLATIONS: CatalogTranslationSeed[] = [
  ...labelsToTranslations(CatalogEntityKind.TREATMENT_TYPE, TREATMENT_LABELS),
  ...labelsToTranslations(CatalogEntityKind.PROSTHESIS_TYPE, PROSTHESIS_LABELS),
  ...labelsToTranslations(CatalogEntityKind.LAB_WORKFLOW_STEP, WORKFLOW_STEP_LABELS),
];
