import { HttpClient } from '@angular/common/http';
import { Inject, Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import {
  CreateLocationRequest,
  Location,
  LocationListResponse,
  UpdateLocationRequest,
} from '../models/location.model';
import { API_URL } from '../tokens/api-url.token';

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

  list(): Observable<LocationListResponse> {
    return this.http.get<LocationListResponse>(`${this.apiUrl}/me/locations`);
  }

  create(data: CreateLocationRequest): Observable<Location> {
    return this.http.post<Location>(`${this.apiUrl}/me/locations`, data);
  }

  update(id: string, data: UpdateLocationRequest): Observable<Location> {
    return this.http.patch<Location>(`${this.apiUrl}/me/locations/${id}`, data);
  }

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