import { getDb } from "./db";
import type { ImportConfigTemplate, ImportFormatRow } from "../shared/types";

/**
 * A template is a named format, nothing more. Taking `ImportFormatRow` here —
 * the same shape `importSourceService` takes — is what keeps both writers on
 * the codec: a field added to the format cannot reach one table and miss the
 * other.
 */
export interface ImportTemplateInput extends ImportFormatRow {
  name: string;
}

export async function getAllTemplates(): Promise<ImportConfigTemplate[]> {
  const db = await getDb();
  return db.select<ImportConfigTemplate[]>(
    "SELECT * FROM import_config_templates ORDER BY name"
  );
}

export async function createTemplate(
  template: ImportTemplateInput
): Promise<number> {
  const db = await getDb();
  const result = await db.execute(
    `INSERT INTO import_config_templates (name, delimiter, encoding, date_format, skip_lines, has_header, column_mapping, amount_mode, sign_convention)
     VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
    [
      template.name,
      template.delimiter,
      template.encoding,
      template.date_format,
      template.skip_lines,
      template.has_header,
      template.column_mapping,
      template.amount_mode,
      template.sign_convention,
    ]
  );
  return result.lastInsertId as number;
}

/**
 * Editing a template touches `import_config_templates` and nothing else. A
 * source that was configured from this template keeps its own eight columns —
 * `import_sources.template_id` is a provenance tag, never re-read as format.
 */
export async function updateTemplate(
  id: number,
  template: ImportTemplateInput
): Promise<void> {
  const db = await getDb();
  await db.execute(
    `UPDATE import_config_templates
     SET name=$1, delimiter=$2, encoding=$3, date_format=$4, skip_lines=$5,
         has_header=$6, column_mapping=$7, amount_mode=$8, sign_convention=$9
     WHERE id=$10`,
    [
      template.name,
      template.delimiter,
      template.encoding,
      template.date_format,
      template.skip_lines,
      template.has_header,
      template.column_mapping,
      template.amount_mode,
      template.sign_convention,
      id,
    ]
  );
}

export async function deleteTemplate(id: number): Promise<void> {
  const db = await getDb();
  await db.execute("DELETE FROM import_config_templates WHERE id = $1", [id]);
}
