module.exports = (sequelize, DataTypes) => {
  const SalesInvoice = sequelize.define('SalesInvoice', {
    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(50),
      allowNull: false
    },
    invoice_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    customer_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'BusinessPartner',
        key: 'id'
      }
    },
    customer_address_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'BPAddress',
        key: 'id'
      }
    },
    customer_gstin_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'BPGSTIN',
        key: 'id'
      }
    },
    delivery_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Delivery',
        key: 'id'
      }
    },
    so_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'SalesOrder',
        key: 'id'
      }
    },
    customer_po_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'CustomerPO',
        key: 'id'
      },
      comment: 'Reference to Customer PO if invoice is created from Customer PO'
    },
    customer_po_no: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    reference_invoice_no: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    sales_order_no: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    billing_address: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    shipping_address: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    due_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    gst_type: {
      type: DataTypes.ENUM('CGST_SGST', 'IGST'),
      allowNull: true
    },
    transport_details: {
      type: DataTypes.STRING(255),
      allowNull: true
    },
    company_bank_detail_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'CompanyBankDetail',
        key: 'id'
      }
    },
    bank_account_holder_name: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    bank_name: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    bank_account_no: {
      type: DataTypes.STRING(50),
      allowNull: true
    },
    bank_branch: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    bank_ifsc_code: {
      type: DataTypes.STRING(20),
      allowNull: true
    },
    delivery_note_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    other_references: {
      type: DataTypes.STRING(255),
      allowNull: true
    },
    motor_vehicle_no: {
      type: DataTypes.STRING(100),
      allowNull: true
    },
    mode_terms_of_payment: {
      type: DataTypes.STRING(255),
      allowNull: true
    },
    terms_of_delivery: {
      type: DataTypes.STRING(255),
      allowNull: true,
      defaultValue: 'By Road'
    },
    currency_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Currency',
        key: 'id'
      }
    },
    place_of_supply: {
      type: DataTypes.STRING(100),
      allowNull: true
    },
    payment_terms: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    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
    },
    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
    },
    dispatched_qty: {
      type: DataTypes.DECIMAL(15, 3),
      allowNull: false,
      defaultValue: 0
    },
    dispatch_status: {
      type: DataTypes.ENUM('NOT_DISPATCHED', 'PARTIALLY_DISPATCHED', 'FULLY_DISPATCHED'),
      allowNull: false,
      defaultValue: 'NOT_DISPATCHED'
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'APPROVED', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'DRAFT'
    },
    posted_at: {
      type: DataTypes.DATE,
      allowNull: true
    },
    posted_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'User',
        key: 'id'
      }
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    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: 'sales_invoice',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'plant_id', 'fiscal_year_id', 'invoice_number']
      },
      {
        fields: ['customer_id']
      },
      {
        fields: ['delivery_id']
      },
      {
        fields: ['so_id']
      },
      {
        fields: ['invoice_date']
      },
      {
        fields: ['status']
      },
      {
        fields: ['dispatch_status']
      }
    ]
  });

  SalesInvoice.associate = function(models) {
    SalesInvoice.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    SalesInvoice.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    SalesInvoice.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    SalesInvoice.belongsTo(models.BusinessPartner, { foreignKey: 'customer_id', as: 'customer' });
    SalesInvoice.belongsTo(models.BPAddress, { foreignKey: 'customer_address_id', as: 'customerAddress' });
    SalesInvoice.belongsTo(models.BPGSTIN, { foreignKey: 'customer_gstin_id', as: 'customerGSTIN' });
    SalesInvoice.belongsTo(models.Delivery, { foreignKey: 'delivery_id', as: 'delivery' });
    SalesInvoice.belongsTo(models.SalesOrder, { foreignKey: 'so_id', as: 'salesOrder' });
    SalesInvoice.belongsTo(models.CustomerPO, { foreignKey: 'customer_po_id', as: 'customerPO' });
    SalesInvoice.belongsTo(models.CompanyBankDetail, { foreignKey: 'company_bank_detail_id', as: 'companyBankDetail' });
    SalesInvoice.belongsTo(models.Currency, { foreignKey: 'currency_id', as: 'currency' });
    SalesInvoice.hasMany(models.SalesInvoiceItem, { foreignKey: 'invoice_id', as: 'items' });
    SalesInvoice.hasMany(models.SalesReturn, { foreignKey: 'invoice_id', as: 'salesReturns' });
    SalesInvoice.hasMany(models.DeliveryChallan, { foreignKey: 'invoice_id', as: 'deliveryChallans' });
    SalesInvoice.hasMany(models.ReceiptEntry, { foreignKey: 'invoice_id', as: 'receipts' });
    SalesInvoice.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdByUser' });
    SalesInvoice.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedByUser' });
    SalesInvoice.belongsTo(models.User, { foreignKey: 'posted_by', as: 'postedByUser' });
  };

  return SalesInvoice;
};

