// SPDX-FileCopyrightText: 2025 Contributors to the CitrineOS Project
//
// SPDX-License-Identifier: Apache-2.0
import type { ILogObj } from 'tslog';
import { Logger } from 'tslog';
import type { ILocationRepository } from '@dal/interfaces/repositories.js';
import { IncomingMessage } from 'http';
import { AuthenticatorFilter } from './AuthenticatorFilter.js';
import type { AuthenticationOptions } from '@citrineos/base';
import { UpgradeUnknownError } from './errors/UnknownError.js';

/**
 * Filter used to block connections from charging stations that are not recognized in the system.
 * It only applies when unknown charging stations are not allowed.
 */
export class UnknownStationFilter extends AuthenticatorFilter {
  private _locationRepository: ILocationRepository;

  constructor(locationRepository: ILocationRepository, logger?: Logger<ILogObj>) {
    super(logger);
    this._locationRepository = locationRepository;
  }

  protected shouldFilter(options: AuthenticationOptions) {
    return !options.allowUnknownChargingStations;
  }

  protected async filter(
    tenantId: number,
    identifier: string,
    _request: IncomingMessage,
  ): Promise<void> {
    const isStationKnown = await this._locationRepository.doesChargingStationExistByStationId(
      tenantId,
      identifier,
    );
    if (!isStationKnown) {
      throw new UpgradeUnknownError(`Unknown identifier ${identifier}`);
    }
  }
}
