module.exports = (sequelize, DataTypes) => {
  const IncomingInspectionReport = sequelize.define('IncomingInspectionReport', {
    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'
      }
    },
    iir_no: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true
    },
    iir_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    supplier_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'BusinessPartner',
        key: 'id'
      }
    },
    supplier_name: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    material_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Material',
        key: 'id'
      }
    },
    part_name: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    part_no: {
      type: DataTypes.STRING(100),
      allowNull: true
    },
    dc_no: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    dc_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    po_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'PurchaseOrder',
        key: 'id'
      }
    },
    po_number: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    total_qty: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: true,
      defaultValue: 0
    },
    uom: {
      type: DataTypes.STRING(20),
      allowNull: true
    },
    accepted_qty: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: true,
      defaultValue: 0
    },
    rejected_qty: {
      type: DataTypes.DECIMAL(18, 3),
      allowNull: true,
      defaultValue: 0
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    inspected_by: {
      type: DataTypes.STRING(100),
      allowNull: true
    },
    inspected_by_user_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    approved_by: {
      type: DataTypes.STRING(100),
      allowNull: true
    },
    approved_by_user_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    inspection_status: {
      type: DataTypes.ENUM('PENDING', 'IN_PROGRESS', 'COMPLETED', 'APPROVED', 'REJECTED'),
      allowNull: false,
      defaultValue: 'PENDING'
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'SUBMITTED', 'APPROVED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    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: 'incoming_inspection_report',
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      { fields: ['company_id'] },
      { fields: ['plant_id'] },
      { unique: true, fields: ['iir_no'] },
      { fields: ['iir_date'] },
      { fields: ['supplier_id'] },
      { fields: ['status'] }
    ]
  });

  IncomingInspectionReport.associate = function(models) {
    IncomingInspectionReport.belongsTo(models.Company, {
      foreignKey: 'company_id',
      as: 'company'
    });

    IncomingInspectionReport.belongsTo(models.Plant, {
      foreignKey: 'plant_id',
      as: 'plant'
    });

    IncomingInspectionReport.belongsTo(models.FiscalYear, {
      foreignKey: 'fiscal_year_id',
      as: 'fiscalYear'
    });

    IncomingInspectionReport.belongsTo(models.BusinessPartner, {
      foreignKey: 'supplier_id',
      as: 'supplier'
    });

    IncomingInspectionReport.belongsTo(models.Material, {
      foreignKey: 'material_id',
      as: 'material'
    });

    IncomingInspectionReport.belongsTo(models.PurchaseOrder, {
      foreignKey: 'po_id',
      as: 'purchaseOrder'
    });

    IncomingInspectionReport.belongsTo(models.User, {
      foreignKey: 'inspected_by_user_id',
      as: 'inspector'
    });

    IncomingInspectionReport.belongsTo(models.User, {
      foreignKey: 'approved_by_user_id',
      as: 'approver'
    });

    IncomingInspectionReport.belongsTo(models.User, {
      foreignKey: 'created_by',
      as: 'creator'
    });

    IncomingInspectionReport.belongsTo(models.User, {
      foreignKey: 'updated_by',
      as: 'updater'
    });

    IncomingInspectionReport.hasMany(models.IncomingInspectionReportDetail, {
      foreignKey: 'iir_id',
      as: 'details'
    });
  };

  return IncomingInspectionReport;
};
