module.exports = (sequelize, DataTypes) => {
  const MaterialIssue = sequelize.define('MaterialIssue', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    plant_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Plant',
        key: 'id'
      }
    },
    fiscal_year_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'FiscalYear',
        key: 'id'
      }
    },
    issue_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    issue_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    production_order_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'ProductionOrder',
        key: 'id'
      }
    },
    issue_type: {
      type: DataTypes.ENUM('MANUAL', 'BACKFLUSH', 'AUTO'),
      allowNull: false,
      defaultValue: 'MANUAL'
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'IN_TRANSIT', 'RECEIVED', 'POSTED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    posted_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    posted_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    received_at: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: 'Date and time when materials were received by production department'
    },
    received_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      },
      comment: 'User who received the materials in production department'
    },
    issued_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    issued_to: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    material_request_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'MaterialRequest',
        key: 'id'
      }
    },
    department_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Department',
        key: 'id'
      },
      comment: 'Receiving department for department-based material issues (optional)'
    },
    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: 'material_issue',
    indexes: [
      {
        fields: ['company_id', 'plant_id', 'fiscal_year_id']
      },
      {
        fields: ['issue_number']
      },
      {
        fields: ['production_order_id']
      },
      {
        fields: ['status']
      }
    ]
  });

  MaterialIssue.associate = function(models) {
    MaterialIssue.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    MaterialIssue.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    MaterialIssue.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    MaterialIssue.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    MaterialIssue.belongsTo(models.MaterialRequest, { foreignKey: 'material_request_id', as: 'materialRequest' });
    MaterialIssue.belongsTo(models.Department, { foreignKey: 'department_id', as: 'department' });
    MaterialIssue.hasMany(models.MaterialIssueItem, { foreignKey: 'issue_id', as: 'items' });
    MaterialIssue.belongsTo(models.User, { foreignKey: 'posted_by', as: 'postedByUser' });
    MaterialIssue.belongsTo(models.User, { foreignKey: 'received_by', as: 'receivedByUser' });
    MaterialIssue.belongsTo(models.User, { foreignKey: 'issued_by', as: 'issuedByUser' });
    MaterialIssue.belongsTo(models.User, { foreignKey: 'issued_to', as: 'issuedToUser' });
    MaterialIssue.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    MaterialIssue.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return MaterialIssue;
};

