module.exports = (sequelize, DataTypes) => {
  const BPGSTIN = sequelize.define('BPGSTIN', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    bp_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'BusinessPartner',
        key: 'id'
      }
    },
    gstin: {
      type: DataTypes.STRING(15),
      allowNull: false
    },
    trade_name: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    registration_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    cancellation_date: {
      type: DataTypes.DATEONLY,
      allowNull: true
    },
    address_line1: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    address_line2: {
      type: DataTypes.STRING(200),
      allowNull: true
    },
    city_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'City',
        key: 'id'
      }
    },
    state_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'State',
        key: 'id'
      }
    },
    country_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'Country',
        key: 'id'
      }
    },
    pincode: {
      type: DataTypes.STRING(10),
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('ACTIVE', 'INACTIVE', 'CANCELLED'),
      allowNull: false,
      defaultValue: 'ACTIVE'
    },
    is_default: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    },
    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: 'bp_gstin',
    indexes: [
      {
        unique: true,
        fields: ['gstin']
      },
      {
        fields: ['bp_id']
      },
      {
        fields: ['status']
      }
    ]
  });

  BPGSTIN.associate = function(models) {
    BPGSTIN.belongsTo(models.BusinessPartner, { foreignKey: 'bp_id', as: 'businessPartner' });
    BPGSTIN.belongsTo(models.City, { foreignKey: 'city_id', as: 'city' });
    BPGSTIN.belongsTo(models.State, { foreignKey: 'state_id', as: 'state' });
    BPGSTIN.belongsTo(models.Country, { foreignKey: 'country_id', as: 'country' });
  };

  return BPGSTIN;
};

