import { Injectable, Inject, Logger } from '@nestjs/common';
import {
  USER_TAG_REPOSITORY,
  IUserTagRepository,
} from '../../domain/repositories/user-tag.repository.interface';

@Injectable()
export class UserTagService {
  private readonly logger = new Logger(UserTagService.name);

  constructor(
    @Inject(USER_TAG_REPOSITORY)
    private readonly userTagRepo: IUserTagRepository,
  ) {}

  async getTagsByUserId(userId: string): Promise<string[]> {
    return this.userTagRepo.findTagsByUserId(userId);
  }

  async addTag(userId: string, tag: string): Promise<void> {
    await this.userTagRepo.addTag(userId, tag);
    this.logger.log(`Tag "${tag}" added to user ${userId}`);
  }

  async removeTags(userId: string, tags: string[]): Promise<void> {
    await this.userTagRepo.removeTags(userId, tags);
    this.logger.log(`Tags [${tags.join(', ')}] removed from user ${userId}`);
  }

  async syncTags(userId: string, tags: string[]): Promise<void> {
    await this.userTagRepo.syncTags(userId, tags);
    this.logger.log(`Tags synced for user ${userId}: [${tags.join(', ')}]`);
  }

  async getAllTags(): Promise<Array<{ tag: string; count: number }>> {
    return this.userTagRepo.getAllTags();
  }

  async getUserIdsByTag(tag: string): Promise<string[]> {
    return this.userTagRepo.findUserIdsByTag(tag);
  }
}
