'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.addColumn('clients', 'portalUserId', {
      type: Sequelize.UUID,
      allowNull: true,
    });
    await queryInterface.addConstraint('clients', {
      fields: ['portalUserId'],
      type: 'unique',
      name: 'clients_portal_user_id_unique',
    });
    await queryInterface.addColumn('clients', 'ai_processing_consent_granted', {
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false,
    });
    await queryInterface.addColumn('clients', 'ai_processing_consent_at', {
      type: Sequelize.DATE,
      allowNull: true,
    });
    await queryInterface.addColumn('clients', 'ai_processing_consent_policy_version', {
      type: Sequelize.TEXT,
      allowNull: true,
    });
    await queryInterface.addColumn('clients', 'recording_consent_granted', {
      type: Sequelize.BOOLEAN,
      allowNull: false,
      defaultValue: false,
    });
    await queryInterface.addColumn('clients', 'recording_consent_at', {
      type: Sequelize.DATE,
      allowNull: true,
    });
    await queryInterface.addColumn('clients', 'recording_consent_policy_version', {
      type: Sequelize.TEXT,
      allowNull: true,
    });
    await queryInterface.addColumn('clients', 'consent_withdrawn_at', {
      type: Sequelize.DATE,
      allowNull: true,
    });
    await queryInterface.addColumn('clients', 'consent_updated_by_id', {
      type: Sequelize.UUID,
      allowNull: true,
    });
    await queryInterface.addColumn('intake_leads', 'consent_ai_notes_at', {
      type: Sequelize.DATE,
      allowNull: true,
    });
    await queryInterface.addColumn('intake_leads', 'consent_policy_version', {
      type: Sequelize.TEXT,
      allowNull: true,
    });

    await queryInterface.sequelize.query(`
      UPDATE clients
      SET "portalUserId" = users.id
      FROM users
      WHERE lower(clients.email) = lower(users.email)
        AND clients."portalUserId" IS NULL
    `);
  },

  async down(queryInterface) {
    await queryInterface.removeColumn('intake_leads', 'consent_policy_version');
    await queryInterface.removeColumn('intake_leads', 'consent_ai_notes_at');
    await queryInterface.removeColumn('clients', 'consent_updated_by_id');
    await queryInterface.removeColumn('clients', 'consent_withdrawn_at');
    await queryInterface.removeColumn('clients', 'recording_consent_policy_version');
    await queryInterface.removeColumn('clients', 'recording_consent_at');
    await queryInterface.removeColumn('clients', 'recording_consent_granted');
    await queryInterface.removeColumn('clients', 'ai_processing_consent_policy_version');
    await queryInterface.removeColumn('clients', 'ai_processing_consent_at');
    await queryInterface.removeColumn('clients', 'ai_processing_consent_granted');
    await queryInterface.removeConstraint('clients', 'clients_portal_user_id_unique');
    await queryInterface.removeColumn('clients', 'portalUserId');
  },
};
