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

export type UserNotificationType = 'success' | 'info' | 'warning';

export interface UserNotification {
  id: string;
  message: string;
  type: UserNotificationType;
  /** Route to navigate to when the notification is clicked. Absent means not clickable. */
  route?: string[];
}

@Injectable({ providedIn: 'root' })
export class UserNotificationService {
  private readonly state = signal<UserNotification[]>([]);

  readonly notifications = this.state.asReadonly();

  show(message: string, type: UserNotificationType = 'info', durationMs: number = 2500, route?: string[]): void {
    const next: UserNotification = {
      id: `note-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
      message,
      type,
      ...(route ? { route } : {}),
    };

    this.state.update(items => [next, ...items].slice(0, 4));

    setTimeout(() => this.dismiss(next.id), durationMs);
  }

  dismiss(id: string): void {
    this.state.update(items => items.filter(item => item.id !== id));
  }
}
