import { projectDirectoryPath, projectFilePath } from './project-path';

export const LEARNITIVE_PROJECT_CONFIG_VERSION = 1 as const;

export type LearnitiveCompiler = 'pdflatex' | 'xelatex' | 'lualatex';
export type LearnitivePdfCompileMode = 'full' | 'fast';
export type LearnitiveCompileTarget = 'local' | 'cloud';

export type LearnitiveProjectConfig = {
  version: typeof LEARNITIVE_PROJECT_CONFIG_VERSION;
  rootFile: string;
  compiler: LearnitiveCompiler;
  /** Local is browser/WASM and remains the default; cloud is explicit Pro compilation. */
  compileTarget?: LearnitiveCompileTarget;
  /**
   * Automatic Full TeX Live reliability fallback preference. Once a Local
   * Compile proves that this project needs Docker TeX Live Full, Learnitive keeps
   * routing reliability compiles (including Live PDF) through that fallback
   * for this project until the user explicitly retries Local Compile or changes
   * compiler settings. This is distinct from the manually selected Pro cloud
   * target.
   */
  cloudFallbackSticky?: boolean;
  /** pdfLaTeX only: full uses BusyTeX + SyncTeX; fast uses SwiftLaTeX preview. */
  pdfCompileMode?: LearnitivePdfCompileMode;
  assets: {
    imageDirectory: string;
  };
};

export const DEFAULT_LEARNITIVE_PROJECT_CONFIG: LearnitiveProjectConfig = Object.freeze({
  version: LEARNITIVE_PROJECT_CONFIG_VERSION,
  rootFile: 'main.tex',
  compiler: 'pdflatex',
  compileTarget: 'local',
  pdfCompileMode: 'full',
  assets: Object.freeze({
    imageDirectory: 'figures/',
  }),
});

export function createDefaultLearnitiveProjectConfig(): LearnitiveProjectConfig {
  return {
    version: DEFAULT_LEARNITIVE_PROJECT_CONFIG.version,
    rootFile: DEFAULT_LEARNITIVE_PROJECT_CONFIG.rootFile,
    compiler: DEFAULT_LEARNITIVE_PROJECT_CONFIG.compiler,
    compileTarget: DEFAULT_LEARNITIVE_PROJECT_CONFIG.compileTarget,
    pdfCompileMode: DEFAULT_LEARNITIVE_PROJECT_CONFIG.pdfCompileMode,
    assets: { ...DEFAULT_LEARNITIVE_PROJECT_CONFIG.assets },
  };
}

export function isLearnitiveProjectConfig(value: unknown): value is LearnitiveProjectConfig {
  if (!value || typeof value !== 'object') {
    return false;
  }
  const candidate = value as Partial<LearnitiveProjectConfig>;
  if (
    candidate.version !== LEARNITIVE_PROJECT_CONFIG_VERSION ||
    typeof candidate.rootFile !== 'string' ||
    (candidate.compiler !== 'pdflatex' &&
      candidate.compiler !== 'xelatex' &&
      candidate.compiler !== 'lualatex') ||
    (candidate.compileTarget !== undefined &&
      candidate.compileTarget !== 'local' &&
      candidate.compileTarget !== 'cloud') ||
    (candidate.cloudFallbackSticky !== undefined &&
      typeof candidate.cloudFallbackSticky !== 'boolean') ||
    (candidate.pdfCompileMode !== undefined &&
      candidate.pdfCompileMode !== 'full' &&
      candidate.pdfCompileMode !== 'fast') ||
    !candidate.assets ||
    typeof candidate.assets.imageDirectory !== 'string'
  ) {
    return false;
  }

  try {
    projectFilePath(candidate.rootFile);
    projectDirectoryPath(candidate.assets.imageDirectory);
    return true;
  } catch {
    return false;
  }
}
