import { createHash } from 'node:crypto';

export const REWARD_COPIES = [
  '이 생각은 이제 Inkling이 들고 있습니다.',
  '머릿속에서 꺼내 두었습니다.',
  '방금 한 줄 잡아뒀습니다.',
  '기록 완료. 이제 잊어도 됩니다.'
] as const;

export interface NotificationDeps {
  isSupported: () => boolean;
  send: (body: string) => void;
}

export class NotificationService {
  constructor(private deps: NotificationDeps) {}

  celebrate(noteId: string): void {
    if (!this.deps.isSupported()) return;
    const idx = this.pick(noteId);
    const body = REWARD_COPIES[idx]!;
    try {
      this.deps.send(body);
    } catch {
      // Swallow notification errors — capture must not fail because of toast.
    }
  }

  private pick(noteId: string): number {
    const hash = createHash('sha256').update(noteId).digest();
    return hash[0]! % REWARD_COPIES.length;
  }
}
