import type { OptimizationFactCard, OptimizedArticle, QaReport } from "../domain/types";
import type { AppRepository } from "./repository";
import {
  archiveOptimizationCase,
  createArticleJob,
  createBrandTemplate,
  createOptimizationCase,
  createOptimizationResultVersion,
  createPublicationRecord,
  getArticleJob,
  getBrandTemplate,
  getFactCard,
  getLatestOptimizedArticle,
  getLatestQaReport,
  getLatestScoringRun,
  getLatestScoringRunForResultVersion,
  getOptimizationCaseDetail,
  getOptimizationResultVersion,
  getPublicationRecord,
  findResultVersionForArticleRevision,
  listBrandTemplates,
  listOptimizationCases,
  listPerformanceSnapshots,
  listPublicationRecords,
  listPublicationRecordsForResultVersion,
  markOptimizationCaseFailed,
  restoreOptimizationCase,
  saveCaseInput,
  saveCalibrationEvent,
  saveFactCard,
  saveOptimizedArticle,
  savePerformanceSnapshot,
  saveQaReport,
  saveRubricVersion,
  saveScoringRun,
  updateOptimizationCaseMetadata,
  updateArticleJob,
  type ArticleJob,
  type NewArticleJob,
  type NewBrandTemplate,
} from "./repositories";

export function createSqliteRepository(dbPath?: string): AppRepository {
  return {
    createBrandTemplate(input: NewBrandTemplate) {
      return Promise.resolve(createBrandTemplate(dbPath, input));
    },
    listBrandTemplates() {
      return Promise.resolve(listBrandTemplates(dbPath));
    },
    getBrandTemplate(id: string) {
      return Promise.resolve(getBrandTemplate(dbPath, id));
    },
    createArticleJob(input: NewArticleJob) {
      return Promise.resolve(createArticleJob(dbPath, input));
    },
    getArticleJob(id: string) {
      return Promise.resolve(getArticleJob(dbPath, id));
    },
    updateArticleJob(
      id: string,
      changes: Partial<Pick<ArticleJob, "brand_template_id" | "status" | "export_paths">>,
    ) {
      return Promise.resolve(updateArticleJob(dbPath, id, changes));
    },
    saveFactCard(jobId: string, factCard: OptimizationFactCard) {
      return Promise.resolve(saveFactCard(dbPath, jobId, factCard));
    },
    getFactCard(jobId: string) {
      return Promise.resolve(getFactCard(dbPath, jobId));
    },
    saveOptimizedArticle(jobId: string, article: OptimizedArticle) {
      return Promise.resolve(saveOptimizedArticle(dbPath, jobId, article));
    },
    getLatestOptimizedArticle(jobId: string) {
      return Promise.resolve(getLatestOptimizedArticle(dbPath, jobId));
    },
    saveQaReport(jobId: string, revision: number, report: QaReport) {
      return Promise.resolve(saveQaReport(dbPath, jobId, revision, report));
    },
    getLatestQaReport(jobId: string) {
      return Promise.resolve(getLatestQaReport(dbPath, jobId));
    },
    saveRubricVersion(rubric) {
      return Promise.resolve(saveRubricVersion(dbPath, rubric));
    },
    saveScoringRun(run) {
      return Promise.resolve(saveScoringRun(dbPath, run));
    },
    getLatestScoringRun(jobId, revision) {
      return Promise.resolve(getLatestScoringRun(dbPath, jobId, revision));
    },
    getLatestScoringRunForResultVersion(resultVersionId) {
      return Promise.resolve(getLatestScoringRunForResultVersion(dbPath, resultVersionId));
    },
    createPublicationRecord(input) {
      return Promise.resolve(createPublicationRecord(dbPath, input));
    },
    listPublicationRecords(jobId) {
      return Promise.resolve(listPublicationRecords(dbPath, jobId));
    },
    listPublicationRecordsForResultVersion(resultVersionId) {
      return Promise.resolve(listPublicationRecordsForResultVersion(dbPath, resultVersionId));
    },
    getPublicationRecord(id) {
      return Promise.resolve(getPublicationRecord(dbPath, id));
    },
    savePerformanceSnapshot(snapshot) {
      return Promise.resolve(savePerformanceSnapshot(dbPath, snapshot));
    },
    listPerformanceSnapshots(publicationId) {
      return Promise.resolve(listPerformanceSnapshots(dbPath, publicationId));
    },
    saveCalibrationEvent(event) {
      return Promise.resolve(saveCalibrationEvent(dbPath, event));
    },
    createOptimizationCase(input) {
      return Promise.resolve(createOptimizationCase(dbPath, input));
    },
    saveCaseInput(input) {
      return Promise.resolve(saveCaseInput(dbPath, input));
    },
    listOptimizationCases(filters) {
      return Promise.resolve(listOptimizationCases(dbPath, filters));
    },
    getOptimizationCaseDetail(caseId) {
      return Promise.resolve(getOptimizationCaseDetail(dbPath, caseId));
    },
    updateOptimizationCaseMetadata(caseId, changes) {
      return Promise.resolve(updateOptimizationCaseMetadata(dbPath, caseId, changes));
    },
    archiveOptimizationCase(caseId) {
      return Promise.resolve(archiveOptimizationCase(dbPath, caseId));
    },
    restoreOptimizationCase(caseId) {
      return Promise.resolve(restoreOptimizationCase(dbPath, caseId));
    },
    markOptimizationCaseFailed(caseId, input) {
      return Promise.resolve(markOptimizationCaseFailed(dbPath, caseId, input));
    },
    createOptimizationResultVersion(input) {
      return Promise.resolve(createOptimizationResultVersion(dbPath, input));
    },
    getOptimizationResultVersion(versionId) {
      return Promise.resolve(getOptimizationResultVersion(dbPath, versionId));
    },
    findResultVersionForArticleRevision(jobId, revision) {
      return Promise.resolve(findResultVersionForArticleRevision(dbPath, jobId, revision));
    },
  };
}
