'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.addColumn('workspace_settings', 'reminders_enabled', {
      type: Sequelize.BOOLEAN, allowNull: false, defaultValue: true,
    });
    await queryInterface.addColumn('workspace_settings', 'default_reminder_minutes', {
      type: Sequelize.INTEGER, allowNull: false, defaultValue: 60,
    });
    await queryInterface.addColumn('workspace_settings', 'timezone', {
      type: Sequelize.TEXT, allowNull: false, defaultValue: 'UTC',
    });
    await queryInterface.addColumn('clients', 'reminders_enabled', {
      type: Sequelize.BOOLEAN, allowNull: false, defaultValue: true,
    });
    await queryInterface.addColumn('clients', 'reminder_minutes', {
      type: Sequelize.INTEGER, allowNull: true,
    });
    await queryInterface.createTable('coaching_reminder_deliveries', {
      id: { type: Sequelize.UUID, allowNull: false, primaryKey: true },
      clientId: { type: Sequelize.UUID, allowNull: false },
      session_at: { type: Sequelize.DATE, allowNull: false },
      scheduled_for: { type: Sequelize.DATE, allowNull: false },
      status: { type: Sequelize.STRING(16), allowNull: false, defaultValue: 'ready' },
      dismissed_at: { type: Sequelize.DATE },
      createdAt: { type: Sequelize.DATE, allowNull: false },
      updatedAt: { type: Sequelize.DATE, allowNull: false },
    });
    await queryInterface.addConstraint('coaching_reminder_deliveries', {
      fields: ['clientId', 'session_at', 'scheduled_for'],
      type: 'unique',
      name: 'coaching_reminder_delivery_unique',
    });
  },

  async down(queryInterface) {
    await queryInterface.dropTable('coaching_reminder_deliveries');
    await queryInterface.removeColumn('clients', 'reminder_minutes');
    await queryInterface.removeColumn('clients', 'reminders_enabled');
    await queryInterface.removeColumn('workspace_settings', 'timezone');
    await queryInterface.removeColumn('workspace_settings', 'default_reminder_minutes');
    await queryInterface.removeColumn('workspace_settings', 'reminders_enabled');
  },
};
