import type {
  NotificationMessage,
  NotificationService,
} from "./notification-service";

export interface GotifyConfig {
  url: string;
  token: string;
  enabled: boolean;
}

export class GotifyNotificationService implements NotificationService {
  constructor(private readonly config: GotifyConfig) {}

  async notify(message: NotificationMessage): Promise<void> {
    if (!this.config.enabled) return;

    try {
      const response = await fetch(
        `${this.config.url}/message?token=${this.config.token}`,
        {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify({
            title: message.title,
            message: message.message,
            priority: message.priority ?? 5,
          }),
        },
      );

      if (!response.ok) {
        console.error(
          "Failed to send Gotify notification",
          await response.text(),
        );
      }
    } catch (error) {
      console.error("Error sending Gotify notification", error);
    }
  }
}
