import { RiskTier } from '../../../common/enums';
import { DomainTag, PrivacyClass } from '../../../generated/prisma';

export type DomainKeywordEntry = {
  domain: DomainTag;
  privacyDefault: PrivacyClass;
  riskTier: RiskTier;
  keywords: ReadonlyArray<string>;
};

export const DOMAIN_KEYWORDS: ReadonlyArray<DomainKeywordEntry> = Object.freeze([
  {
    domain: DomainTag.CODING,
    privacyDefault: PrivacyClass.CLOUD_PERMITTED,
    riskTier: RiskTier.LOW,
    keywords: [
      'debug',
      'function',
      'class',
      'compile',
      'syntax',
      'typescript',
      'python',
      'javascript',
      'rust',
      'java',
      'react',
      'angular',
      'vue',
      'sql',
      'database',
      'api',
      'endpoint',
      'graphql',
      'rest',
      'kubernetes',
      'docker',
      'microservice',
      'algorithm',
      'pointer',
      'memory leak',
      'refactor',
      'unit test',
      'pull request',
      'merge conflict',
      'stack trace',
      'exception',
      'null pointer',
      'segfault',
      'lint',
      'compile error',
    ],
  },
  {
    domain: DomainTag.MEDICAL,
    privacyDefault: PrivacyClass.LOCAL_PREFERRED,
    riskTier: RiskTier.HIGH,
    keywords: [
      'patient',
      'diagnosis',
      'symptom',
      'symptoms',
      'contraindication',
      'prescribe',
      'prescription',
      'dosage',
      'mg/dl',
      'cbc',
      'wbc',
      'hgb',
      'platelet',
      'preeclampsia',
      'pregnancy',
      'pregnant',
      'differential',
      'tenofovir',
      'warfarin',
      'fluconazole',
      'glp-1',
      'ascco',
      't2dm',
      'oncology',
      'cancer',
      'tumor',
      'icd-10',
      'mri',
      'ct scan',
      'biopsy',
      'sepsis',
      'cardiac',
      'renal',
      'hepatic',
      'physician',
      'clinician',
      'nurse',
      'medication',
      'therapy regimen',
      'guidelines',
    ],
  },
  {
    domain: DomainTag.LEGAL,
    privacyDefault: PrivacyClass.LOCAL_PREFERRED,
    riskTier: RiskTier.HIGH,
    keywords: [
      'contract',
      'clause',
      'arbitration',
      'tort',
      'negligence',
      'liability',
      'cease and desist',
      'cease-and-desist',
      'gdpr',
      'hipaa',
      'eu law',
      'jurisdiction',
      'plaintiff',
      'defendant',
      'motion to',
      'discovery',
      'subpoena',
      'will draft',
      'estate',
      'power of attorney',
      'trademark',
      'copyright',
      'patent',
      'litigation',
      'compliance',
      'consent decree',
      'injunction',
      'statute',
      'regulation',
    ],
  },
  {
    domain: DomainTag.FINANCE,
    privacyDefault: PrivacyClass.LOCAL_PREFERRED,
    riskTier: RiskTier.MEDIUM,
    keywords: [
      'wacc',
      'discount rate',
      'irr',
      'npv',
      'ifrs',
      'asc 842',
      'gaap',
      'revenue recognition',
      'lease accounting',
      'dcf model',
      'arr',
      'mrr',
      'cogs',
      'gross margin',
      'ebitda',
      'tax rate',
      'capital structure',
      'balance sheet',
      'income statement',
      'cash flow',
      'forecast',
      'budget',
      'audit',
      'reconciliation',
      'amortization',
      'depreciation',
      'earnings call',
      'sec filing',
      '10-k',
      '10-q',
      'analyst',
      'cfo',
    ],
  },
  {
    domain: DomainTag.MARKETING,
    privacyDefault: PrivacyClass.CLOUD_PERMITTED,
    riskTier: RiskTier.LOW,
    keywords: [
      'ad copy',
      'landing page',
      'campaign',
      'b2b',
      'b2c',
      'churn',
      'conversion rate',
      'funnel',
      'cta',
      'a/b test',
      'nurture',
      'email sequence',
      'linkedin ad',
      'google ads',
      'seo',
      'sem',
      'persona',
      'positioning',
      'messaging',
      'brand voice',
      'press release',
      'content marketing',
      'social media',
      'engagement',
      'impression',
      'click-through',
      'lead',
      'mql',
      'sql',
      'icp',
    ],
  },
  {
    domain: DomainTag.MECHANICAL,
    privacyDefault: PrivacyClass.CLOUD_PERMITTED,
    riskTier: RiskTier.MEDIUM,
    keywords: [
      'heat exchanger',
      'torque spec',
      'cvt',
      'transmission',
      'engine',
      'coolant',
      'piston',
      'cylinder',
      'crankshaft',
      'gearbox',
      'bearing',
      'thermal conductivity',
      'fluid mechanics',
      'hvac',
      'pneumatic',
      'hydraulic',
      'load bearing',
      'tensile',
      'fatigue',
      'finite element',
      'stress analysis',
      'cad model',
      'tolerance',
      'surface finish',
    ],
  },
  {
    domain: DomainTag.AUTOMOTIVE,
    privacyDefault: PrivacyClass.CLOUD_PERMITTED,
    riskTier: RiskTier.LOW,
    keywords: [
      'civic',
      'corolla',
      'camry',
      'mustang',
      'tesla',
      'bmw',
      'mercedes',
      'audi',
      'porsche',
      'ford',
      'chevrolet',
      'honda',
      'toyota',
      'turn signal',
      'brake pad',
      'rotor',
      'caliper',
      'suspension',
      'control arm',
      'cv joint',
      'spark plug',
      'oil change',
      'transmission fluid',
      'tsb',
      'recall',
      'check engine',
      'obd-ii',
      'cel',
    ],
  },
  {
    domain: DomainTag.BIOLOGY,
    privacyDefault: PrivacyClass.CLOUD_PERMITTED,
    riskTier: RiskTier.LOW,
    keywords: [
      'crispr',
      'cas9',
      'cas12',
      'mitochondrial',
      'rna',
      'dna',
      'protein',
      'enzyme',
      'gene editing',
      'genome',
      'pcr',
      'primer',
      'amplicon',
      'tm 60',
      'gel electrophoresis',
      'sequencing',
      'transcription',
      'translation',
      'photosynthesis',
      'mitosis',
      'meiosis',
      'apoptosis',
      'neuroscience',
      'synthetic biology',
      'metabolomics',
    ],
  },
  {
    domain: DomainTag.EDUCATION,
    privacyDefault: PrivacyClass.CLOUD_PERMITTED,
    riskTier: RiskTier.LOW,
    keywords: [
      'syllabus',
      'curriculum',
      'lecture',
      'exam',
      'quiz',
      'assignment',
      'rubric',
      'lesson plan',
      'classroom',
      'pedagogy',
      'andragogy',
      'multiple choice',
      'short answer',
      'essay prompt',
      'reading intervention',
      'common core',
      'sat',
      'gre',
      'gmat',
      'mcat',
      'lsat',
      'professor',
      'teacher',
      'student',
      'textbook',
      'flipped classroom',
    ],
  },
  {
    domain: DomainTag.CREATIVE_WRITING,
    privacyDefault: PrivacyClass.CLOUD_PERMITTED,
    riskTier: RiskTier.LOW,
    keywords: [
      'short story',
      'horror',
      'sci-fi',
      'fantasy',
      'plot',
      'protagonist',
      'antagonist',
      'character arc',
      'narrative',
      'haiku',
      'sonnet',
      'poem',
      'novel',
      'chapter',
      'screenplay',
      'dialogue',
      'monologue',
      'metaphor',
      'simile',
      'imagery',
      'mary oliver',
      'tolkien',
      'continue this poem',
      'creative writing',
    ],
  },
  {
    domain: DomainTag.RESEARCH,
    privacyDefault: PrivacyClass.CLOUD_PERMITTED,
    riskTier: RiskTier.MEDIUM,
    keywords: [
      'literature review',
      'meta-analysis',
      'p-value',
      'r-squared',
      'confidence interval',
      'cite',
      'doi',
      'arxiv',
      'pubmed',
      'systematic review',
      'rct',
      'cohort study',
      'cross-sectional',
      'qualitative coding',
      'thematic analysis',
      'survey design',
      'field study',
      'ethnography',
      'interview transcript',
      'find latest',
      'summarize the latest',
      'recent papers',
      'state of the art',
    ],
  },
  {
    domain: DomainTag.MENTAL_HEALTH,
    privacyDefault: PrivacyClass.LOCAL_PREFERRED,
    riskTier: RiskTier.HIGH,
    keywords: [
      'depression',
      'depressed',
      'anxiety',
      'anxious',
      'suicidal',
      'self-harm',
      'ptsd',
      'trauma',
      'therapy session',
      'counseling',
      'cbt',
      'dbt',
      'panic attack',
      'mental health crisis',
    ],
  },
  {
    domain: DomainTag.MULTIMEDIA,
    privacyDefault: PrivacyClass.CLOUD_PERMITTED,
    riskTier: RiskTier.LOW,
    keywords: [
      'youtube.com',
      'youtu.be',
      'video',
      'transcribe',
      'transcript',
      'image of',
      'generate an image',
      'photo of',
      'audio file',
      'transcribe audio',
      'spectrogram',
      'waveform',
      'wav file',
      'mp3',
      'mp4',
      'png',
      'jpeg',
    ],
  },
  {
    domain: DomainTag.HR,
    privacyDefault: PrivacyClass.LOCAL_PREFERRED,
    riskTier: RiskTier.MEDIUM,
    keywords: [
      'employee record',
      'salary spreadsheet',
      'compensation review',
      'performance review',
      'hiring rubric',
      'job description',
      'onboarding',
      'offboarding',
      'pip',
      'severance',
      'eeoc',
    ],
  },
  {
    domain: DomainTag.TRANSLATION,
    privacyDefault: PrivacyClass.CLOUD_PERMITTED,
    riskTier: RiskTier.LOW,
    keywords: [
      'translate',
      'translation',
      'in arabic',
      'in german',
      'in french',
      'in spanish',
      'in italian',
      'in portuguese',
      'in russian',
      'into arabic',
      'into german',
      'into french',
      'into spanish',
    ],
  },
]);

export type MimeModalityEntry = {
  pattern: RegExp;
  domain: DomainTag | null;
  modalityIn:
    | 'PDF_INPUT'
    | 'IMAGE_INPUT'
    | 'AUDIO_INPUT'
    | 'VIDEO_INPUT'
    | 'SPREADSHEET_INPUT'
    | 'FILE_INPUT';
};

export const MIME_TO_MODALITY: ReadonlyArray<MimeModalityEntry> = Object.freeze([
  { pattern: /^application\/pdf$/i, domain: null, modalityIn: 'PDF_INPUT' },
  { pattern: /^image\//i, domain: DomainTag.MULTIMEDIA, modalityIn: 'IMAGE_INPUT' },
  { pattern: /^audio\//i, domain: DomainTag.MULTIMEDIA, modalityIn: 'AUDIO_INPUT' },
  { pattern: /^video\//i, domain: DomainTag.MULTIMEDIA, modalityIn: 'VIDEO_INPUT' },
  { pattern: /(spreadsheet|excel|csv)/i, domain: null, modalityIn: 'SPREADSHEET_INPUT' },
]);

export const LOW_CONFIDENCE_FLOOR = 0.6;

export const DOMAIN_PRIORITY: Readonly<Record<DomainTag, number>> = Object.freeze({
  [DomainTag.MENTAL_HEALTH]: 100,
  [DomainTag.MEDICAL]: 95,
  [DomainTag.LEGAL]: 90,
  [DomainTag.FINANCE]: 85,
  [DomainTag.HR]: 80,
  [DomainTag.RESEARCH]: 70,
  [DomainTag.CODING]: 65,
  [DomainTag.BIOLOGY]: 60,
  [DomainTag.MECHANICAL]: 55,
  [DomainTag.AUTOMOTIVE]: 50,
  [DomainTag.CHEMISTRY]: 50,
  [DomainTag.PHYSICS]: 50,
  [DomainTag.EDUCATION]: 45,
  [DomainTag.MARKETING]: 40,
  [DomainTag.SALES]: 38,
  [DomainTag.BUSINESS]: 35,
  [DomainTag.CREATIVE_WRITING]: 30,
  [DomainTag.LITERATURE]: 28,
  [DomainTag.MULTIMEDIA]: 25,
  [DomainTag.TRANSLATION]: 20,
  [DomainTag.GENERAL]: 0,
});

export const PRIVATE_MARKER_REGEX = /\[(private|confidential|sensitive)\]/i;
export const CRITICAL_RISK_REGEX = /\b(suicide|suicidal|self-harm|kill myself)\b/i;
export const YOUTUBE_REGEX = /(youtube\.com|youtu\.be)/i;
export const URL_REGEX = /https?:\/\//i;
export const IMAGE_GEN_REGEX =
  /\b(generate|create|make|draw|render)\s+(an?\s+|me\s+)?(image|picture|photo|illustration|render)\b/i;
export const STRUCTURED_OUT_REGEX = /\b(csv|json|spreadsheet|table|markdown|docx|pdf)\b/i;

export const RISK_TIER_TO_LEVEL: Readonly<Record<RiskTier, 'LOW' | 'MEDIUM' | 'HIGH'>> =
  Object.freeze({
    [RiskTier.LOW]: 'LOW',
    [RiskTier.MEDIUM]: 'MEDIUM',
    [RiskTier.HIGH]: 'HIGH',
  });
