module.exports = (sequelize, DataTypes) => {
  const Employee = sequelize.define('Employee', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    employee_number: {
      type: DataTypes.STRING(20),
      allowNull: false
    },
    first_name: {
      type: DataTypes.STRING(100),
      allowNull: false
    },
    middle_name: {
      type: DataTypes.STRING(100),
      allowNull: true
    },
    last_name: {
      type: DataTypes.STRING(100),
      allowNull: true
    },
    date_of_birth: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    gender: {
      type: DataTypes.ENUM('MALE', 'FEMALE', 'OTHER'),
      allowNull: true
    },
    aadhaar_number: {
      type: DataTypes.STRING(12),
      allowNull: true
    },
    pan_number: {
      type: DataTypes.STRING(10),
      allowNull: true
    },
    department_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Department',
        key: 'id'
      }
    },
    designation_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Designation',
        key: 'id'
      }
    },
    employee_type: {
      type: DataTypes.ENUM('PERMANENT', 'CONTRACT', 'TRAINEE', 'INTERN', 'TEMPORARY'),
      allowNull: false,
      defaultValue: 'PERMANENT'
    },
    joining_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    confirmation_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    exit_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    work_email: {
      type: DataTypes.STRING(100),
      allowNull: true
    },
    personal_email: {
      type: DataTypes.STRING(100),
      allowNull: true
    },
    work_phone: {
      type: DataTypes.STRING(20),
      allowNull: true
    },
    mobile_phone: {
      type: DataTypes.STRING(20),
      allowNull: true
    },
    address_line1: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    address_line2: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    city_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'City',
        key: 'id'
      }
    },
    state_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'State',
        key: 'id'
      }
    },
    country_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Country',
        key: 'id'
      }
    },
    pincode: {
      type: DataTypes.STRING(10),
      allowNull: true
    },
    uan_number: {
      type: DataTypes.STRING(12),
      allowNull: true,
      comment: 'Universal Account Number for EPF'
    },
    esi_number: {
      type: DataTypes.STRING(17),
      allowNull: true,
      comment: 'ESI Insurance Number'
    },
    pf_contribution: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true
    },
    esi_contribution: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true
    },
    reporting_manager_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Employee',
        key: 'id'
      }
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE', 'TERMINATED', 'RESIGNED', 'ON_LEAVE'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    effective_from: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    effective_to: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    updated_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    }
  }, {
    tableName: 'employee',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'employee_number']
      },
      {
        fields: ['department_id']
      },
      {
        fields: ['designation_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['aadhaar_number']
      },
      {
        fields: ['pan_number']
      },
      {
        fields: ['effective_from', 'effective_to']
      }
    ]
  });

  Employee.associate = function(models) {
    Employee.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    Employee.belongsTo(models.Department, { foreignKey: 'department_id', as: 'department' });
    Employee.belongsTo(models.Designation, { foreignKey: 'designation_id', as: 'designation' });
    Employee.belongsTo(models.Employee, { foreignKey: 'reporting_manager_id', as: 'reportingManager' });
    Employee.hasMany(models.Employee, { foreignKey: 'reporting_manager_id', as: 'subordinates' });
    Employee.belongsTo(models.City, { foreignKey: 'city_id', as: 'city' });
    Employee.belongsTo(models.State, { foreignKey: 'state_id', as: 'state' });
    Employee.belongsTo(models.Country, { foreignKey: 'country_id', as: 'country' });
    Employee.hasOne(models.User, { foreignKey: 'employee_id', as: 'user' });
  };

  return Employee;
};

