import 'dotenv/config';
import db from '../src/database/models/index.js';

type SlaDefault = {
  stage: string;
  role: string;
  tat: number;
  unit: 'hours' | 'days';
};

const defaults: SlaDefault[] = [
  // ONBOARDING
  { stage: 'ASM Review', role: 'ASM', tat: 2, unit: 'days' },
  { stage: 'ZM Review', role: 'DD-ZM', tat: 3, unit: 'days' },
  { stage: 'Level 1 Interview', role: 'RBM, DD-ZM', tat: 2, unit: 'days' },
  { stage: 'Level 2 Interview', role: 'DD-Lead, ZBH', tat: 3, unit: 'days' },
  { stage: 'Level 3 Interview', role: 'NBH, DD-Head', tat: 5, unit: 'days' },
  { stage: 'FDD Verification', role: 'FDD', tat: 10, unit: 'days' },
  { stage: 'Finance Verification', role: 'Finance', tat: 7, unit: 'days' },
  { stage: 'LOI Approval', role: 'NBH', tat: 5, unit: 'days' },
  { stage: 'LOA Approval', role: 'NBH', tat: 5, unit: 'days' },

  // RESIGNATION
  { stage: 'Resignation ASM Review', role: 'ASM', tat: 2, unit: 'days' },
  { stage: 'Resignation Evaluation', role: 'RBM, DD-ZM', tat: 3, unit: 'days' },
  { stage: 'Resignation ZBH Review', role: 'ZBH', tat: 3, unit: 'days' },
  { stage: 'Resignation Lead Review', role: 'DD-Lead', tat: 5, unit: 'days' },
  { stage: 'Resignation NBH Approval', role: 'NBH', tat: 5, unit: 'days' },
  { stage: 'Resignation Legal Letter', role: 'Legal', tat: 7, unit: 'days' },

  // TERMINATION
  { stage: 'Termination ASM Review', role: 'ASM', tat: 2, unit: 'days' },
  { stage: 'Termination Evaluation', role: 'RBM, DD-ZM', tat: 3, unit: 'days' },
  { stage: 'Termination ZBH Review', role: 'ZBH', tat: 3, unit: 'days' },
  { stage: 'Termination Lead Review', role: 'DD-Lead', tat: 5, unit: 'days' },
  { stage: 'Termination Legal Verification', role: 'Legal', tat: 7, unit: 'days' },
  { stage: 'Termination NBH Evaluation', role: 'NBH', tat: 5, unit: 'days' },
  { stage: 'Termination CEO Approval', role: 'CEO', tat: 7, unit: 'days' },

  // RELOCATION
  { stage: 'Relocation ASM Review', role: 'ASM', tat: 2, unit: 'days' },
  { stage: 'Relocation ZM Review', role: 'DD-ZM', tat: 3, unit: 'days' },
  { stage: 'Relocation RBM Review', role: 'RBM', tat: 3, unit: 'days' },
  { stage: 'Relocation ZBH Review', role: 'ZBH', tat: 3, unit: 'days' },
  { stage: 'Relocation Lead Review', role: 'DD-Lead', tat: 5, unit: 'days' },

  // CONSTITUTIONAL CHANGE
  { stage: 'Constitution Legal Review', role: 'Legal', tat: 7, unit: 'days' },
  { stage: 'Constitution Lead Review', role: 'DD-Lead', tat: 5, unit: 'days' },
  { stage: 'Constitution NBH Approval', role: 'NBH', tat: 5, unit: 'days' },
];

async function seedSlaConfigs() {
  const { sequelize, SLAConfiguration, SLAReminder, SLAEscalationConfig } = db as any;
  await sequelize.authenticate();
  console.log('Database connected.');

  const transaction = await sequelize.transaction();

  try {
    for (const item of defaults) {
      const [config, created] = await SLAConfiguration.findOrCreate({
        where: { activityName: item.stage },
        defaults: {
          activityName: item.stage,
          ownerRole: item.role,
          tatHours: item.tat,
          tatUnit: item.unit,
          isActive: true,
        },
        transaction,
      });

      if (!created) {
        await config.update(
          {
            ownerRole: item.role,
            tatHours: item.tat,
            tatUnit: item.unit,
          },
          { transaction }
        );
      }

      await SLAReminder.destroy({ where: { slaConfigId: config.id }, transaction });
      await SLAEscalationConfig.destroy({ where: { slaConfigId: config.id }, transaction });

      await SLAReminder.bulkCreate(
        [
          { slaConfigId: config.id, timeValue: 1, timeUnit: 'days', isEnabled: true },
          { slaConfigId: config.id, timeValue: 4, timeUnit: 'hours', isEnabled: true },
        ],
        { transaction }
      );

      await SLAEscalationConfig.bulkCreate(
        [
          { slaConfigId: config.id, level: 1, timeValue: 4, timeUnit: 'hours', notifyRole: 'ZBH' },
          { slaConfigId: config.id, level: 2, timeValue: 12, timeUnit: 'hours', notifyRole: 'DD Lead' },
          { slaConfigId: config.id, level: 3, timeValue: 24, timeUnit: 'hours', notifyRole: 'NBH' },
        ],
        { transaction }
      );
    }

    await transaction.commit();
    console.log(`SLA configurations seeded successfully. Total stages: ${defaults.length}`);
  } catch (error) {
    await transaction.rollback();
    console.error('SLA seed failed:', error);
    throw error;
  } finally {
    await sequelize.close();
  }
}

seedSlaConfigs().catch((err) => {
  console.error(err);
  process.exit(1);
});
