module.exports = (sequelize, DataTypes) => {
  const CostCenter = sequelize.define('CostCenter', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    cost_center_code: {
      type: DataTypes.STRING(20),
      allowNull: false
    },
    description: {
      type: DataTypes.STRING(500),
      allowNull: false
    },
    cost_center_type: {
      type: DataTypes.ENUM('PRODUCTION', 'ADMINISTRATION', 'SALES', 'RESEARCH', 'OTHER'),
      allowNull: true
    },
    parent_cost_center_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'CostCenter',
        key: 'id'
      }
    },
    department_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Department',
        key: 'id'
      }
    },
    operation_name_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'OperationName',
        key: 'id'
      },
      comment: 'Reference to operation name master for operation-wise costing'
    },
    person_responsible: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Employee',
        key: 'id'
      }
    },
    currency_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Currency',
        key: 'id'
      }
    },
    cost_per_hour: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true,
      defaultValue: 0.000,
      comment: 'Operation cost per hour used in production costing'
    },
    standard_time: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: true,
      defaultValue: 0.000,
      comment: 'Standard operation time in hours'
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE', 'BLOCKED'),
      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: 'cost_center',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'cost_center_code']
      },
      {
        fields: ['parent_cost_center_id']
      },
      {
        fields: ['operation_name_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['effective_from', 'effective_to']
      }
    ]
  });

  CostCenter.associate = function(models) {
    CostCenter.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    CostCenter.belongsTo(models.CostCenter, { foreignKey: 'parent_cost_center_id', as: 'parentCostCenter' });
    CostCenter.hasMany(models.CostCenter, { foreignKey: 'parent_cost_center_id', as: 'childCostCenters' });
    CostCenter.belongsTo(models.Department, { foreignKey: 'department_id', as: 'department' });
    CostCenter.belongsTo(models.OperationName, { foreignKey: 'operation_name_id', as: 'operationName' });
    CostCenter.belongsTo(models.Employee, { foreignKey: 'person_responsible', as: 'responsiblePerson' });
    CostCenter.belongsTo(models.Currency, { foreignKey: 'currency_id', as: 'currency' });
    CostCenter.hasMany(models.WorkCenter, { foreignKey: 'cost_center_id', as: 'workCenters' });
  };

  return CostCenter;
};

