import { Injectable, computed, inject } from '@angular/core';
import { RoleId } from '../../models/role.model';
import { TeamRole } from '../../models/team-role.model';
import { AuthStore } from '../auth/auth-store';
import { TeamStore } from '../team/team-store';

@Injectable({ providedIn: 'root' })
export class HelpAccessService {
  private readonly authStore = inject(AuthStore);
  private readonly teamStore = inject(TeamStore);

  readonly canOpenGuide = computed(() => {
    const user = this.authStore.currentUser();
    if (!user) return false;
    if (user.role?.id === RoleId.Admin) return true;

    return (
      this.teamStore
        .team()
        ?.players?.some(
          (player) =>
            player.active &&
            player.user?.id === user.id &&
            (player.teamRole?.id ?? 0) >= TeamRole.ScndTreasurer,
        ) ?? false
    );
  });

  readonly showContextHelp = computed(
    () => this.canOpenGuide() && this.authStore.currentUser()?.helpTextsEnabled !== false,
  );
}
