module.exports = (sequelize, DataTypes) => {
  const AccountingDocument = sequelize.define('AccountingDocument', {
    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: true,
      references: {
        model: 'Plant',
        key: 'id'
      }
    },
    fiscal_year_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'FiscalYear',
        key: 'id'
      }
    },
    fiscal_period_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'FiscalPeriod',
        key: 'id'
      }
    },
    document_number: {
      type: DataTypes.STRING(50),
      allowNull: false
    },
    document_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    posting_date: {
      type: DataTypes.DATEONLY,
      allowNull: false
    },
    document_type: {
      type: DataTypes.ENUM('GRN', 'VENDOR_INVOICE', 'SALES_INVOICE', 'GOODS_RECEIPT', 'GOODS_ISSUE', 'STOCK_TRANSFER', 'STOCK_ADJUSTMENT', 'JOURNAL_ENTRY'),
      allowNull: false
    },
    reference_type: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Model name of the source transaction (e.g., GoodsReceiptNote, VendorInvoice)'
    },
    reference_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: 'ID of the source transaction'
    },
    reference_number: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Document number of the source transaction'
    },
    currency_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Currency',
        key: 'id'
      }
    },
    exchange_rate: {
      type: DataTypes.DECIMAL(18, 6),
      allowNull: false,
      defaultValue: 1.0
    },
    total_debit: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0,
      comment: 'Total debit amount in base currency'
    },
    total_credit: {
      type: DataTypes.DECIMAL(18, 2),
      allowNull: false,
      defaultValue: 0,
      comment: 'Total credit amount in base currency'
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('DRAFT', 'POSTED', 'REVERSED'),
      allowNull: false,
      defaultValue: 'POSTED'
    },
    reversed_document_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'AccountingDocument',
        key: 'id'
      },
      comment: 'Reference to reversed document if this is a reversal'
    },
    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: 'accounting_document',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'document_number']
      },
      {
        fields: ['document_date']
      },
      {
        fields: ['posting_date']
      },
      {
        fields: ['fiscal_year_id']
      },
      {
        fields: ['fiscal_period_id']
      },
      {
        fields: ['document_type']
      },
      {
        fields: ['reference_type', 'reference_id']
      },
      {
        fields: ['status']
      }
    ]
  });

  AccountingDocument.associate = function(models) {
    AccountingDocument.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    AccountingDocument.belongsTo(models.Plant, { foreignKey: 'plant_id', as: 'plant' });
    AccountingDocument.belongsTo(models.FiscalYear, { foreignKey: 'fiscal_year_id', as: 'fiscalYear' });
    AccountingDocument.belongsTo(models.FiscalPeriod, { foreignKey: 'fiscal_period_id', as: 'fiscalPeriod' });
    AccountingDocument.belongsTo(models.Currency, { foreignKey: 'currency_id', as: 'currency' });
    AccountingDocument.belongsTo(models.AccountingDocument, { foreignKey: 'reversed_document_id', as: 'reversedDocument' });
    AccountingDocument.hasMany(models.AccountingDocumentItem, { foreignKey: 'document_id', as: 'items' });
    AccountingDocument.belongsTo(models.User, { foreignKey: 'posted_by', as: 'postedBy' });
    AccountingDocument.belongsTo(models.User, { foreignKey: 'created_by', as: 'createdBy' });
    AccountingDocument.belongsTo(models.User, { foreignKey: 'updated_by', as: 'updatedBy' });
  };

  return AccountingDocument;
};

