import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';

export interface TagRecord {
  id: string;
  name: string;
  image_count: number;
}

export interface TagListResponse {
  items: TagRecord[];
  total: number;
  limit: number;
  offset: number;
}

@Injectable({ providedIn: 'root' })
export class TagService {
  private readonly base = '/api/v1';

  constructor(private http: HttpClient) {}

  list(prefix = '', limit = 100, offset = 0, sort?: string, minCount?: number): Observable<TagListResponse> {
    let params = new HttpParams().set('limit', limit).set('offset', offset);
    if (prefix) {
      params = params.set('q', prefix);
    }
    if (sort) {
      params = params.set('sort', sort);
    }
    if (minCount !== undefined) {
      params = params.set('min_count', minCount);
    }
    return this.http.get<TagListResponse>(`${this.base}/tags`, { params });
  }
}
