import { DataTypes, type QueryInterface } from 'sequelize';

/**
 * F.R.A.M.E. weekly entry — split the "Week Of" concept. `week_of` is now the
 * canonical Sunday-start ISO date (`YYYY-MM-DD`, normalized server-side); this
 * adds an optional free-text `week_label` for the author's extra note
 * (e.g. "Spring Break week"). Idempotent: the column is only added if missing.
 */
async function columnExists(
  queryInterface: QueryInterface,
  table: string,
  column: string,
): Promise<boolean> {
  const [results] = await queryInterface.sequelize.query(`
    SELECT 1 FROM information_schema.columns
    WHERE table_name = '${table}' AND column_name = '${column}'
  `);
  return (results as unknown[]).length > 0;
}

export default {
  up: async (queryInterface: QueryInterface) => {
    if (!(await columnExists(queryInterface, 'frame_entries', 'week_label'))) {
      await queryInterface.addColumn('frame_entries', 'week_label', {
        type: DataTypes.TEXT,
        allowNull: true,
      });
    }
  },

  down: async (queryInterface: QueryInterface) => {
    await queryInterface.removeColumn('frame_entries', 'week_label');
  },
};
