'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('employee', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      company_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'company',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'RESTRICT'
      },
      employee_number: {
        type: Sequelize.STRING(20),
        allowNull: false
      },
      first_name: {
        type: Sequelize.STRING(100),
        allowNull: false
      },
      middle_name: {
        type: Sequelize.STRING(100),
        allowNull: true
      },
      last_name: {
        type: Sequelize.STRING(100),
        allowNull: true
      },
      date_of_birth: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      gender: {
        type: Sequelize.ENUM('MALE', 'FEMALE', 'OTHER'),
        allowNull: true
      },
      aadhaar_number: {
        type: Sequelize.STRING(12),
        allowNull: true
      },
      pan_number: {
        type: Sequelize.STRING(10),
        allowNull: true
      },
      department_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'department',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      designation_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'designation',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      employee_type: {
        type: Sequelize.ENUM('PERMANENT', 'CONTRACT', 'TRAINEE', 'INTERN', 'TEMPORARY'),
        allowNull: false,
        defaultValue: 'PERMANENT'
      },
      joining_date: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      confirmation_date: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      exit_date: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      work_email: {
        type: Sequelize.STRING(100),
        allowNull: true
      },
      personal_email: {
        type: Sequelize.STRING(100),
        allowNull: true
      },
      work_phone: {
        type: Sequelize.STRING(20),
        allowNull: true
      },
      mobile_phone: {
        type: Sequelize.STRING(20),
        allowNull: true
      },
      address_line1: {
        type: Sequelize.STRING(200),
        allowNull: true
      },
      address_line2: {
        type: Sequelize.STRING(200),
        allowNull: true
      },
      city_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'city',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      state_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'state',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      country_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'country',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      pincode: {
        type: Sequelize.STRING(10),
        allowNull: true
      },
      uan_number: {
        type: Sequelize.STRING(12),
        allowNull: true,
        comment: 'Universal Account Number for EPF'
      },
      esi_number: {
        type: Sequelize.STRING(17),
        allowNull: true,
        comment: 'ESI Insurance Number'
      },
      pf_contribution: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true
      },
      esi_contribution: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true
      },
      reporting_manager_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'employee',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      status: {
        type: Sequelize.ENUM('ACTIVE', 'INACTIVE', 'TERMINATED', 'RESIGNED', 'ON_LEAVE'),
        allowNull: false,
        defaultValue: 'ACTIVE'
      },
      effective_from: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      effective_to: {
        type: Sequelize.DATEONLY,
        allowNull: true
      },
      created_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      updated_by: {
        type: Sequelize.INTEGER,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')
      }
    });

    await queryInterface.addIndex('employee', ['company_id', 'employee_number'], { unique: true });
    await queryInterface.addIndex('employee', ['department_id']);
    await queryInterface.addIndex('employee', ['designation_id']);
    await queryInterface.addIndex('employee', ['status']);
    await queryInterface.addIndex('employee', ['aadhaar_number']);
    await queryInterface.addIndex('employee', ['pan_number']);
    await queryInterface.addIndex('employee', ['effective_from', 'effective_to']);
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('employee');
  }
};

