import { TypeOrmPersistenceSchemaError } from './TypeOrmPersistenceSchemaError';

type InvalidDriverSchemaErrorParams = {
  fieldName: string;
  reason: TypeOrmPersistenceSchemaError['reason'];
  message?: string;
};

export class InvalidDriverSchemaError extends TypeOrmPersistenceSchemaError {
  override readonly name: string = 'InvalidDriverSchemaError';

  constructor(params: InvalidDriverSchemaErrorParams);
  constructor(message: string);
  constructor(paramsOrMessage: InvalidDriverSchemaErrorParams | string) {
    if (typeof paramsOrMessage === 'string') {
      super({
        entityName: 'Driver',
        fieldName: 'unknown',
        reason: 'invalid_shape',
        message: paramsOrMessage,
      });
      return;
    }

    super({
      entityName: 'Driver',
      fieldName: paramsOrMessage.fieldName,
      reason: paramsOrMessage.reason,
      ...(paramsOrMessage.message ? { message: paramsOrMessage.message } : {}),
    });
  }
}