import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { API_CONFIG } from '../config/api.config';
import { ApiResponse } from '../models/api-response.model';

@Injectable({ providedIn: 'root' })
export class BaseApiService {
  private readonly http = inject(HttpClient);

  protected get<T>(resource: string, params?: Record<string, string | number | boolean>): Observable<T> {
    let httpParams = new HttpParams();

    if (params) {
      Object.entries(params).forEach(([key, value]) => {
        httpParams = httpParams.set(key, String(value));
      });
    }

    return this.http.get<T>(`${API_CONFIG.baseUrl}${resource}`, { params: httpParams });
  }

  protected post<T, R = unknown>(resource: string, body: T): Observable<R> {
    return this.http.post<R>(`${API_CONFIG.baseUrl}${resource}`, body);
  }

  protected put<T, R = unknown>(resource: string, body: T): Observable<R> {
    return this.http.put<R>(`${API_CONFIG.baseUrl}${resource}`, body);
  }

  protected delete<R = unknown>(resource: string): Observable<R> {
    return this.http.delete<R>(`${API_CONFIG.baseUrl}${resource}`);
  }
}
