import { IListingResponse } from '@/core/models/service.model';
import {
  IGoodCategoryRawResponse,
  IGoodCategoryResponse,
  IGoodRawResponse,
  IGoodResponse,
} from '@/domains/pos/models/good.io';
import {
  IPosAccessibleRawResponse,
  IPosAccessibleResponse,
  IPosInfoRawResponse,
  IPosInfoResponse,
} from '@/domains/pos/models/pos.io';
import { IPosProfileRawResponse, IPosProfileResponse } from '@/domains/pos/models/profile.io';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { POS_API_ROUTES } from '../constants';
import { IPosOrderRawResponse, IPosOrderRequest, IPosOrderResponse } from '../models';

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

  private apiRoutes = POS_API_ROUTES;

  getProfile(): Observable<IPosProfileResponse> {
    return this.http.get<IPosProfileRawResponse>(this.apiRoutes.profile());
  }

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

  getAccessible(): Observable<IListingResponse<IPosAccessibleResponse>> {
    return this.http.get<IListingResponse<IPosAccessibleRawResponse>>(this.apiRoutes.accessible());
  }

  getGoods(searchQuery: string): Observable<IListingResponse<IGoodResponse>> {
    return this.http.get<IListingResponse<IGoodRawResponse>>(this.apiRoutes.goods(), {
      params: { search: searchQuery },
    });
  }

  getGoodCategories(): Observable<IListingResponse<IGoodCategoryResponse>> {
    return this.http.get<IListingResponse<IGoodCategoryRawResponse>>(
      this.apiRoutes.goodCategories(),
    );
  }

  submitOrder(payload: IPosOrderRequest): Observable<IPosOrderResponse> {
    return this.http.post<IPosOrderRawResponse>(this.apiRoutes.submitOrder(), payload);
  }
}
