import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from '../../../environments/environment';
import { LogPage, LogQuery } from '../../models/log.model';

@Injectable({ providedIn: 'root' })
export class LogsApi {
  private readonly http = inject(HttpClient);
  private readonly baseUrl = `${environment.apiUrl}admin/logs`;

  loadLogs(query: LogQuery): Observable<LogPage> {
    return this.http.get<LogPage>(this.baseUrl, { params: this.toParams(query) });
  }

  private toParams(query: LogQuery): HttpParams {
    let params = new HttpParams();
    if (query.level) params = params.set('level', query.level);
    if (query.event) params = params.set('event', query.event);
    if (query.from) params = params.set('from', query.from);
    if (query.to) params = params.set('to', query.to);
    if (query.search) params = params.set('search', query.search);
    params = params.set('page', query.page).set('limit', query.limit);
    return params;
  }
}
