import { pool } from '../db/client';

const PUBLIC_SCHEMA = 'public';

export const tableExists = async (tableName: string): Promise<boolean> => {
  const result = await pool.query<{ exists: boolean }>(
    `
      select exists (
        select 1
        from information_schema.tables
        where table_schema = $1
          and table_name = $2
      ) as "exists"
    `,
    [PUBLIC_SCHEMA, tableName],
  );

  return result.rows[0]?.exists === true;
};

export const hasRequiredTables = async (tableNames: string[]): Promise<boolean> => {
  const checks = await Promise.all(tableNames.map((tableName) => tableExists(tableName)));
  return checks.every(Boolean);
};
