import { HttpClient } from '@angular/common/http'
import { Injectable, inject } from '@angular/core'
import { RestExtractor } from '@app/core'
import { Observable } from 'rxjs'
import { catchError } from 'rxjs/operators'
import { environment } from 'src/environments/environment'

export interface EventRegistrationPayload {
  firstName: string
  lastName: string
  email: string
  phone: string
  companyName: string
  jobTitle: string
  country: string
}

@Injectable({
  providedIn: 'root'
})
export class EventRegistrationService {
  private http = inject(HttpClient)
  private restExtractor = inject(RestExtractor)

  private static BASE_EVENTS_URL = environment.apiUrl + '/api/v1/events'
  registerUser (eventId: number, payload: EventRegistrationPayload): Observable<any> {
    const url = `${EventRegistrationService.BASE_EVENTS_URL}/${eventId}/registrations`

    return this.http
      .post(url, payload)
      .pipe(
        catchError(err => this.restExtractor.handleError(err))
      )
  }
  getAllRegistrations (): Observable<any> {
    const url = `${EventRegistrationService.BASE_EVENTS_URL}/registrations`
  
    return this.http
      .get<any>(url)
      .pipe(
        catchError(err => this.restExtractor.handleError(err))
      )
  }
  deleteRegistration (eventId: number, registrationId: number): Observable<any> {
    const url = `${EventRegistrationService.BASE_EVENTS_URL}/${eventId}/registrations/${registrationId}`
  
    return this.http
      .delete(url)
      .pipe(
        catchError(err => this.restExtractor.handleError(err))
      )
  }
}