import { IPaginatedResponse } from '@/core/models/service.model';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { POS_API_ROUTES } from '../constants';
import {
  IPosInfoRawResponse,
  IPosInfoResponse,
  IPosOrderRequest,
  IPosProductCategoriesRawResponse,
  IPosProductCategoriesResponse,
  IPosStockRawResponse,
  IPosStockResponse,
} from '../models';

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

  private apiRoutes = POS_API_ROUTES;

  getInfo(posId: number): Observable<IPosInfoResponse> {
    return this.http.get<IPosInfoRawResponse>(this.apiRoutes.info(posId));
  }

  getStock(
    posId: number,
    searchQuery: string = '',
  ): Observable<IPaginatedResponse<IPosStockResponse>> {
    return this.http.get<IPaginatedResponse<IPosStockRawResponse>>(this.apiRoutes.stock(posId), {
      params: {
        q: searchQuery,
      },
    });
  }

  getCategories(posId: number): Observable<IPaginatedResponse<IPosProductCategoriesResponse>> {
    return this.http.get<IPaginatedResponse<IPosProductCategoriesRawResponse>>(
      this.apiRoutes.productCategories(posId),
    );
  }

  submitOrder(posId: number, payload: IPosOrderRequest): Observable<any> {
    return this.http.post<any>(this.apiRoutes.submitOrder(posId), payload);
  }

  getHeldOrders(posId: number): Observable<IPaginatedResponse<any>> {
    return this.http.get<IPaginatedResponse<any>>(this.apiRoutes.heldOrders(posId));
  }

  getOrder(posId: number, orderId: number): Observable<any> {
    return this.http.get<any>(this.apiRoutes.order(posId, orderId));
  }

  cancelOrder(posId: number, orderId: number): Observable<any> {
    return this.http.post<any>(this.apiRoutes.cancelOrder(posId, orderId), {});
  }

  rejectOrder(posId: number, orderId: number): Observable<any> {
    return this.http.post<any>(this.apiRoutes.rejectOrder(posId, orderId), {});
  }

  confirmOrder(posId: number, orderId: number): Observable<any> {
    return this.http.post<any>(this.apiRoutes.confirmOrder(posId, orderId), {});
  }
}
