import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable, map } from 'rxjs';
import { bilingual } from '../../../shared/i18n/bilingual';
import { ApiConfigService } from '../../../core/services/api-config.service';
import {
  GivingCategoryDto, CreateGivingCategoryRequest, UpdateGivingCategoryRequest,
} from '../models/giving.model';

@Injectable({ providedIn: 'root' })
export class GivingCategoryApiService {
  private readonly endpoint: string;
  constructor(private http: HttpClient, apiConfig: ApiConfigService) {
    this.endpoint = apiConfig.getApiUrl('giving-categories');
  }

  getAll(includeInactive = false): Observable<GivingCategoryDto[]> {
    const params = new HttpParams().set('includeInactive', includeInactive);
    return this.http.get<GivingCategoryDto[]>(this.endpoint, { params }).pipe(
      map(list => list.map(c => ({ ...c, label: bilingual(c.name_en, c.name_zh) }))),
    );
  }
  create(request: CreateGivingCategoryRequest): Observable<{ id: number }> {
    return this.http.post<{ id: number }>(this.endpoint, request);
  }
  update(id: number, request: UpdateGivingCategoryRequest): Observable<void> {
    return this.http.put<void>(`${this.endpoint}/${id}`, request);
  }
  deactivate(id: number): Observable<void> {
    return this.http.delete<void>(`${this.endpoint}/${id}`);
  }
}
