module.exports = (sequelize, DataTypes) => {
  const GLAccount = sequelize.define('GLAccount', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    company_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'Company',
        key: 'id'
      }
    },
    account_code: {
      type: DataTypes.STRING(20),
      allowNull: false
    },
    account_name: {
      type: DataTypes.STRING(200),
      allowNull: false
    },
    account_type: {
      type: DataTypes.ENUM('ASSET', 'LIABILITY', 'EQUITY', 'REVENUE', 'EXPENSE'),
      allowNull: false
    },
    account_group: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Account group classification'
    },
    parent_account_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'GLAccount',
        key: 'id'
      }
    },
    balance_type: {
      type: DataTypes.ENUM('DEBIT', 'CREDIT', 'BOTH'),
      allowNull: false,
      defaultValue: 'BOTH'
    },
    is_active: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true
    },
    allows_posting: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true
    },
    is_control_account: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: 'True if this is a control account (e.g., Trade Receivables, Trade Payables)'
    },
    level: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 1,
      comment: 'Hierarchy level in chart of accounts'
    },
    sort_order: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE', 'BLOCKED'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    effective_from: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    effective_to: {
      type: DataTypes.DATEONLY,
      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: 'gl_account',
    indexes: [
      {
        unique: true,
        fields: ['company_id', 'account_code']
      },
      {
        fields: ['account_type']
      },
      {
        fields: ['parent_account_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['effective_from', 'effective_to']
      }
    ]
  });

  GLAccount.associate = function(models) {
    GLAccount.belongsTo(models.Company, { foreignKey: 'company_id', as: 'company' });
    GLAccount.belongsTo(models.GLAccount, { foreignKey: 'parent_account_id', as: 'parentAccount' });
    GLAccount.hasMany(models.GLAccount, { foreignKey: 'parent_account_id', as: 'childAccounts' });
  };

  return GLAccount;
};

