import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { ApiConfigService } from '../../../core/services/api-config.service';
import {
  SiteSettingDto, UpdateSiteSettingRequest,
  NotificationSettingDto, UpdateNotificationSettingRequest,
  TestEmailRequest, TestLineRequest, NotificationResult,
} from '../models/settings.model';

@Injectable({ providedIn: 'root' })
export class SettingsApiService {
  private readonly endpoint: string;

  constructor(private http: HttpClient, apiConfig: ApiConfigService) {
    this.endpoint = apiConfig.getApiUrl('settings');
  }

  getSite(): Observable<SiteSettingDto> {
    return this.http.get<SiteSettingDto>(`${this.endpoint}/site`);
  }

  updateSite(request: UpdateSiteSettingRequest): Observable<void> {
    return this.http.put<void>(`${this.endpoint}/site`, request);
  }

  getNotification(): Observable<NotificationSettingDto> {
    return this.http.get<NotificationSettingDto>(`${this.endpoint}/notification`);
  }

  updateNotification(request: UpdateNotificationSettingRequest): Observable<void> {
    return this.http.put<void>(`${this.endpoint}/notification`, request);
  }

  testEmail(request: TestEmailRequest): Observable<NotificationResult> {
    return this.http.post<NotificationResult>(`${this.endpoint}/notification/test-email`, request);
  }

  testLine(request: TestLineRequest): Observable<NotificationResult> {
    return this.http.post<NotificationResult>(`${this.endpoint}/notification/test-line`, request);
  }
}
