import { Injectable, computed, signal } from '@angular/core';

const KEY = 'bms_client_token';
const NAME_KEY = 'bms_client_name';

@Injectable({ providedIn: 'root' })
export class ClientAuthService {
  readonly token = signal<string | null>(localStorage.getItem(KEY));
  readonly name = signal<string | null>(localStorage.getItem(NAME_KEY));
  readonly isAuthed = computed(() => !!this.token());

  set(token: string, name?: string): void {
    this.token.set(token);
    localStorage.setItem(KEY, token);
    if (name) { this.name.set(name); localStorage.setItem(NAME_KEY, name); }
  }

  clear(): void {
    this.token.set(null);
    this.name.set(null);
    localStorage.removeItem(KEY);
    localStorage.removeItem(NAME_KEY);
  }
}
