/**
 * Helpers partagés pour les tests de routes API
 */
import { vi } from "vitest";
import type { Pool } from "mysql2/promise";
import type { AppConfig } from "../../config.js";
import type { RouteContext } from "../types.js";

export const makeConfig = (overrides?: Partial<AppConfig>): AppConfig => ({
  appEnv: "test",
  host: "127.0.0.1",
  port: 0,
  ads: { enabled: false },
  auth: { jwtSecret: "test-secret" },
  cookies: { sameSite: "lax", secure: false },
  smtp: { secure: false },
  db: {
    host: "localhost",
    port: 3306,
    name: "test",
    user: "test",
    password: "test",
    poolSize: 1,
    ssl: false,
    sslRejectUnauthorized: true,
  },
  ...overrides,
});

export const makeDb = (executeFn?: (...args: unknown[]) => unknown) =>
  ({
    execute: vi.fn(executeFn ?? (async () => [[]])),
  }) as unknown as Pool;

export const makeCtx = (overrides?: Partial<RouteContext>): RouteContext => ({
  db: makeDb(),
  config: makeConfig(),
  mailer: null,
  signAccessToken: () => "access-tok",
  signRefreshToken: () => "refresh-tok",
  setRefreshCookie: () => undefined,
  getRefreshCookie: () => undefined,
  clearRefreshCookie: () => undefined,
  requireAuth: async () => undefined,
  requireAdmin: async () => undefined,
  packageVersion: "1.0.0-test",
  appEnv: "test",
  ...overrides,
});
