import { IPaginatedResponse } from '@/core/models/service.model';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { STOCK_KEEPING_UNITS_API_ROUTES } from '../constants';
import {
  IStockKeepingUnitRawResponse,
  IStockKeepingUnitRequest,
  IStockKeepingUnitResponse,
} from '../models';

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

  private apiRoutes = STOCK_KEEPING_UNITS_API_ROUTES;

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

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

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