module.exports = (sequelize, DataTypes) => {
  const WeldingVisualInspectionReport = sequelize.define('WeldingVisualInspectionReport', {
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
    company_id: { type: DataTypes.INTEGER, allowNull: false },
    plant_id: { type: DataTypes.INTEGER, allowNull: false },
    fiscal_year_id: { type: DataTypes.INTEGER, allowNull: false },
    wvir_no: { type: DataTypes.STRING(50), allowNull: false, unique: true },
    report_date: { type: DataTypes.DATEONLY, allowNull: false },
    doc_no: { type: DataTypes.STRING(50), allowNull: true, defaultValue: 'RI/QA/F/10' },
    rev_no: { type: DataTypes.STRING(20), allowNull: true, defaultValue: '0' },
    work_order_id: { type: DataTypes.INTEGER, allowNull: true },
    won: { type: DataTypes.STRING(80), allowNull: true },
    item_code: { type: DataTypes.STRING(120), allowNull: true },
    item_name: { type: DataTypes.STRING(250), allowNull: true },
    customer_id: { type: DataTypes.INTEGER, allowNull: true },
    customer_name: { type: DataTypes.STRING(250), allowNull: true },
    work_order_qty: { type: DataTypes.DECIMAL(18, 3), allowNull: true, defaultValue: 0 },
    sample_qty: { type: DataTypes.DECIMAL(18, 3), allowNull: true, defaultValue: 0 },
    inspected_by: { type: DataTypes.STRING(120), allowNull: true },
    inspected_date: { type: DataTypes.DATEONLY, allowNull: true },
    remarks: { type: DataTypes.TEXT, allowNull: true },
    status: {
      type: DataTypes.ENUM('DRAFT', 'SUBMITTED', 'APPROVED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    created_by: { type: DataTypes.INTEGER, allowNull: true },
    updated_by: { type: DataTypes.INTEGER, allowNull: true }
  }, {
    tableName: 'welding_visual_inspection_report',
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at'
  });

  WeldingVisualInspectionReport.associate = function(models) {
    WeldingVisualInspectionReport.belongsTo(models.BusinessPartner, { foreignKey: 'customer_id', as: 'customer' });
    WeldingVisualInspectionReport.belongsTo(models.WorkOrder, { foreignKey: 'work_order_id', as: 'workOrder' });
    WeldingVisualInspectionReport.hasMany(models.WeldingVisualInspectionReportDetail, { foreignKey: 'wvir_id', as: 'details' });
  };

  return WeldingVisualInspectionReport;
};

