module.exports = (sequelize, DataTypes) => {
  const Department = sequelize.define('Department', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    department_code: {
      type: DataTypes.STRING(20),
      allowNull: false
    },
    department_name: {
      type: DataTypes.STRING(200),
      allowNull: false
    },
    parent_department_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Department',
        key: 'id'
      }
    },
    head_of_department: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Employee',
        key: 'id'
      }
    },
    cost_center_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'CostCenter',
        key: 'id'
      }
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    effective_from: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    effective_to: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    process_order_no: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: 'Process order number for managing department workflow sequence'
    },
    process_end_department: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: 'Flag to indicate if this is an end department in the process flow (0=No, 1=Yes)'
    },
    default_storage_location_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'StorageLocation',
        key: 'id'
      },
      comment: 'Default storage location for this department when receiving materials'
    },
    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: 'department',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'department_code']
      },
      {
        fields: ['parent_department_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['effective_from', 'effective_to']
      }
    ]
  });

  Department.associate = function(models) {
    Department.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    Department.belongsTo(models.Department, { foreignKey: 'parent_department_id', as: 'parentDepartment' });
    Department.hasMany(models.Department, { foreignKey: 'parent_department_id', as: 'childDepartments' });
    Department.belongsTo(models.Employee, { foreignKey: 'head_of_department', as: 'head' });
    Department.belongsTo(models.CostCenter, { foreignKey: 'cost_center_id', as: 'costCenter' });
    Department.belongsTo(models.StorageLocation, { foreignKey: 'default_storage_location_id', as: 'defaultStorageLocation' });
    Department.hasMany(models.Employee, { foreignKey: 'department_id', as: 'employees' });
  };

  return Department;
};

