import { Transaction } from "../generated/kysely-tailordb";
import { CannotDeactivateBaseCurrencyError, CurrencyNotFoundError } from "../lib/errors.generated";
import { ok, err } from "@tailor-platform/erp-kit/module";

export interface DeactivateCurrencyInput {
  currencyId: string;
}

/**
 * Function: deactivateCurrency
 *
 * Disables a currency from being used in new transactions while preserving
 * historical data. Base currency cannot be deactivated.
 */
export async function run(db: Transaction, input: DeactivateCurrencyInput) {
  // 1. Find currency by ID
  const currency = await db
    .selectFrom("Currency")
    .selectAll()
    .where("id", "=", input.currencyId)
    .forUpdate()
    .executeTakeFirst();

  // 2. If not found, throw error
  if (!currency) {
    return err(new CurrencyNotFoundError(input.currencyId));
  }

  // 3. Check if base currency
  if (currency.isBaseCurrency) {
    return err(new CannotDeactivateBaseCurrencyError(input.currencyId));
  }

  // 4. If already inactive, return currency (idempotent)
  if (!currency.isActive) {
    return ok({ currency });
  }

  // 5. Update isActive = false
  const updatedCurrency = await db
    .updateTable("Currency")
    .set({
      isActive: false,
      updatedAt: new Date(),
    })
    .where("id", "=", input.currencyId)
    .returningAll()
    .executeTakeFirst();

  // 6. Return updated currency
  return ok({ currency: updatedCurrency! });
}
