import { HttpClient } from '@angular/common/http';
import { Inject, Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import {
  CreateEmbedConfigRequest,
  EmbedConfig,
  EmbedConfigListResponse,
  PublicEmbedConfig,
  UpdateEmbedConfigRequest,
  UpdateEventTypeEmbedRequest,
} from '../models/embed.model';
import { API_URL } from '../tokens/api-url.token';

@Injectable({ providedIn: 'root' })
export class EmbedApiService {
  constructor(
    private http: HttpClient,
    @Inject(API_URL) private apiUrl: string,
  ) {}

  list(): Observable<EmbedConfigListResponse> {
    return this.http.get<EmbedConfigListResponse>(
      `${this.apiUrl}/me/embed-configs`,
    );
  }

  create(data: CreateEmbedConfigRequest): Observable<EmbedConfig> {
    return this.http.post<EmbedConfig>(`${this.apiUrl}/me/embed-configs`, data);
  }

  update(id: string, data: UpdateEmbedConfigRequest): Observable<EmbedConfig> {
    return this.http.patch<EmbedConfig>(
      `${this.apiUrl}/me/embed-configs/${id}`,
      data,
    );
  }

  delete(id: string): Observable<void> {
    return this.http.delete<void>(`${this.apiUrl}/me/embed-configs/${id}`);
  }

  getForEventType(eventTypeId: string): Observable<EmbedConfig> {
    return this.http.get<EmbedConfig>(
      `${this.apiUrl}/me/event-types/${eventTypeId}/embed`,
    );
  }

  updateForEventType(
    eventTypeId: string,
    data: UpdateEventTypeEmbedRequest,
  ): Observable<EmbedConfig> {
    return this.http.put<EmbedConfig>(
      `${this.apiUrl}/me/event-types/${eventTypeId}/embed`,
      data,
    );
  }

  getPublicConfig(embedKey: string): Observable<PublicEmbedConfig> {
    return this.http.get<PublicEmbedConfig>(
      `${this.apiUrl}/embed/config/${embedKey}`,
    );
  }
}
