import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
import { ClientGrpc } from '@nestjs/microservices';
import { Observable } from 'rxjs';

type GrpcMethod<TRequest = unknown, TResponse = unknown> = (request: TRequest) => Observable<TResponse>;

@Injectable()
export class CoreGrpcService implements OnModuleInit {
  auth!: Record<string, GrpcMethod>;
  admin!: Record<string, GrpcMethod>;
  rbac!: Record<string, GrpcMethod>;
  security!: Record<string, GrpcMethod>;
  settings!: Record<string, GrpcMethod>;
  profile!: Record<string, GrpcMethod>;
  documents!: Record<string, GrpcMethod>;
  addresses!: Record<string, GrpcMethod>;
  oauth!: Record<string, GrpcMethod>;
  otp!: Record<string, GrpcMethod>;
  advancedAuth!: Record<string, GrpcMethod>;
  notifications!: Record<string, GrpcMethod>;
  chat!: Record<string, GrpcMethod>;
  family!: Record<string, GrpcMethod>;
  media!: Record<string, GrpcMethod>;
  bot!: Record<string, GrpcMethod>;

  constructor(@Inject('SSO_CORE') private readonly client: ClientGrpc) {}

  onModuleInit() {
    this.auth = this.client.getService<Record<string, GrpcMethod>>('AuthService');
    this.admin = this.client.getService<Record<string, GrpcMethod>>('AdminService');
    this.rbac = this.client.getService<Record<string, GrpcMethod>>('RbacService');
    this.security = this.client.getService<Record<string, GrpcMethod>>('SecurityService');
    this.settings = this.client.getService<Record<string, GrpcMethod>>('SettingsService');
    this.profile = this.client.getService<Record<string, GrpcMethod>>('ProfileService');
    this.documents = this.client.getService<Record<string, GrpcMethod>>('DocumentsService');
    this.addresses = this.client.getService<Record<string, GrpcMethod>>('AddressesService');
    this.oauth = this.client.getService<Record<string, GrpcMethod>>('OAuthCoreService');
    this.otp = this.client.getService<Record<string, GrpcMethod>>('OtpService');
    this.advancedAuth = this.client.getService<Record<string, GrpcMethod>>('AdvancedAuthService');
    this.family = this.client.getService<Record<string, GrpcMethod>>('FamilyService');
    this.notifications = this.client.getService<Record<string, GrpcMethod>>('NotificationsService');
    this.chat = this.client.getService<Record<string, GrpcMethod>>('ChatService');
    this.media = this.client.getService<Record<string, GrpcMethod>>('MediaService');
    this.bot = this.client.getService<Record<string, GrpcMethod>>('BotService');
  }
}
