import { IPaginatedResponse } from '@/core/models/service.model';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

import { GUILD_STOCK_KEEPING_UNITS_API_ROUTES } from '../constants/apiRoutes/stockKeepingUnits';
import {
  IStockKeepingUnitRawResponse,
  IStockKeepingUnitRequest,
  IStockKeepingUnitResponse,
} from '../models';

@Injectable({ providedIn: 'root' })
export class StockKeepingUnitsService {
  constructor(private http: HttpClient) {}

  private apiRoutes = GUILD_STOCK_KEEPING_UNITS_API_ROUTES;

  getAll(guildId: string): Observable<IPaginatedResponse<IStockKeepingUnitResponse>> {
    return this.http.get<IPaginatedResponse<IStockKeepingUnitRawResponse>>(
      this.apiRoutes.list(guildId),
    );
  }

  create(
    guildId: string,
    payload: IStockKeepingUnitRequest,
  ): Observable<IStockKeepingUnitResponse> {
    return this.http.post<IStockKeepingUnitResponse>(this.apiRoutes.list(guildId), payload);
  }

  update(
    guildId: string,
    id: string,
    payload: IStockKeepingUnitRequest,
  ): Observable<IStockKeepingUnitResponse> {
    return this.http.patch<IStockKeepingUnitResponse>(this.apiRoutes.single(guildId, id), payload);
  }
}
