/* eslint-disable @typescript-eslint/member-ordering */
import {
  Component,
  computed,
  inject,
  input
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { lucideUserCheck, lucideUserPlus } from '@ng-icons/lucide';
import { AppI18nService, APP_TRANSLATE_IMPORTS } from '../../../../core/i18n';
import { FriendService } from '../../application/services/friend.service';
import type { User } from '../../../../shared-kernel';

@Component({
  selector: 'app-friend-button',
  standalone: true,
  imports: [
    CommonModule,
    NgIcon,
    ...APP_TRANSLATE_IMPORTS
  ],
  viewProviders: [provideIcons({ lucideUserCheck, lucideUserPlus })],
  templateUrl: './friend-button.component.html'
})
export class FriendButtonComponent {
  private readonly friends = inject(FriendService);
  private readonly i18n = inject(AppI18nService);

  readonly user = input.required<User>();
  readonly userId = computed(() => this.user().oderId || this.user().id);
  readonly isFriend = computed(() => this.friends.isFriend(this.userId()));
  readonly ariaLabel = computed(() =>
    this.isFriend()
      ? this.i18n.instant('dm.friend.remove')
      : this.i18n.instant('dm.friend.add')
  );

  toggle(event: Event): void {
    event.stopPropagation();
    void this.friends.toggleFriend(this.userId());
  }
}
