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 {
  MinistryDto, CreateMinistryRequest, UpdateMinistryRequest,
} from '../models/ministry.model';

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

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