module.exports = (sequelize, DataTypes) => {
  const VendorInvoice = sequelize.define('VendorInvoice', {
    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'
      }
    },
    invoice_number: {
      type: DataTypes.STRING(100),
      allowNull: false
    },
    invoice_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    vendor_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'BusinessPartner',
        key: 'id'
      }
    },
    grn_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'GoodsReceiptNote',
        key: 'id'
      }
    },
    po_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'PurchaseOrder',
        key: 'id'
      }
    },
    vendor_invoice_number: {
      type: DataTypes.STRING(100),
      allowNull: false
    },
    vendor_invoice_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    currency_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Currency',
        key: 'id'
      }
    },
    sub_total: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    discount_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    cgst_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    sgst_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    igst_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    cess_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    total_tax_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    round_off: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    total_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    tds_amount: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    net_payable: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'SUBMITTED', 'APPROVED', 'POSTED', 'PAID', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    payment_terms: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    due_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    submitted_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    submitted_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    approved_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    approved_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    posted_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    posted_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    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: 'vendor_invoice',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'invoice_number']
      },
      {
        fields: ['plant_id']
      },
      {
        fields: ['fiscal_year_id']
      },
      {
        fields: ['vendor_id']
      },
      {
        fields: ['grn_id']
      },
      {
        fields: ['po_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['invoice_date']
      },
      {
        fields: ['vendor_invoice_number']
      }
    ]
  });

  VendorInvoice.associate = function(models) {
    VendorInvoice.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    VendorInvoice.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    VendorInvoice.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    VendorInvoice.belongsTo(models.BusinessPartner, { foreignKey: 'vendor_id', as: 'vendor' });
    VendorInvoice.belongsTo(models.GoodsReceiptNote, { foreignKey: 'grn_id', as: 'goodsReceiptNote' });
    VendorInvoice.belongsTo(models.PurchaseOrder, { foreignKey: 'po_id', as: 'purchaseOrder' });
    VendorInvoice.belongsTo(models.Currency, { foreignKey: 'currency_id', as: 'currency' });
    VendorInvoice.hasMany(models.VendorInvoiceItem, { foreignKey: 'invoice_id', as: 'items' });
    VendorInvoice.belongsTo(models.User, { foreignKey: 'submitted_by', as: 'submittedByUser' });
    VendorInvoice.belongsTo(models.User, { foreignKey: 'approved_by', as: 'approvedByUser' });
    VendorInvoice.belongsTo(models.User, { foreignKey: 'posted_by', as: 'postedByUser' });
    VendorInvoice.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    VendorInvoice.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
  };

  return VendorInvoice;
};

