import { DataTypes } from 'sequelize';
import { Migration, safeApplyColumnChanges } from '../migrate';

/**
 * Migration: Add credit_schedule_state column to subscriptions table
 *
 * This column stores the credit schedule state for subscriptions with
 * scheduled credit delivery, enabling features like:
 * - Periodic credit grants (hourly, daily, weekly, monthly)
 * - Trial period credit delivery
 * - Refresh-style credits that expire with next grant
 */
export const up: Migration = async ({ context }) => {
  await safeApplyColumnChanges(context, {
    subscriptions: [
      {
        name: 'credit_schedule_state',
        field: {
          type: DataTypes.JSON,
          allowNull: true,
          defaultValue: null,
        },
      },
    ],
  });
};

export const down: Migration = async ({ context }) => {
  const schema = await context.describeTable('subscriptions');
  if (schema.credit_schedule_state) {
    await context.removeColumn('subscriptions', 'credit_schedule_state');
  }
};
