module.exports = (sequelize, DataTypes) => {
  const WIPTracking = sequelize.define('WIPTracking', {
    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'
      }
    },
    production_order_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'ProductionOrder',
        key: 'id'
      }
    },
    material_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    wip_quantity: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: false,
      defaultValue: 0,
      comment: 'Work in Progress quantity (logical tracking only)'
    },
    uom_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'UOM',
        key: 'id'
      }
    },
    material_value: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0,
      comment: 'Material cost in WIP'
    },
    labor_value: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0,
      comment: 'Labor cost in WIP'
    },
    overhead_value: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0,
      comment: 'Overhead cost in WIP'
    },
    total_wip_value: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0,
      comment: 'Total WIP value (material + labor + overhead)'
    },
    as_on_date: {
      type: DataTypes.DATEONLY,
      allowNull: false,
      comment: 'As on date for WIP snapshot'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    }
  }, {
    tableName: 'wip_tracking',
    indexes: [
      {
        fields: ['company_id', 'plant_id', 'fiscal_year_id']
      },
      {
        fields: ['production_order_id']
      },
      {
        fields: ['material_id']
      },
      {
        fields: ['as_on_date']
      }
    ]
  });

  WIPTracking.associate = function(models) {
    WIPTracking.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    WIPTracking.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    WIPTracking.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    WIPTracking.belongsTo(models.ProductionOrder, { foreignKey: 'production_order_id', as: 'productionOrder' });
    WIPTracking.belongsTo(models.Material, { foreignKey: 'material_id', as: 'material' });
    WIPTracking.belongsTo(models.UOM, { foreignKey: 'uom_id', as: 'uom' });
  };

  return WIPTracking;
};

