import type { ConfirmedFactCard, OptimizedArticle, QaReport } from "../domain/types";
import type { AppRepository } from "./repository";
import {
  createArticleJob,
  createBrandTemplate,
  getArticleJob,
  getBrandTemplate,
  getFactCard,
  getLatestOptimizedArticle,
  getLatestQaReport,
  listBrandTemplates,
  saveFactCard,
  saveOptimizedArticle,
  saveQaReport,
  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: ConfirmedFactCard) {
      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));
    },
  };
}
