import { IPaginatedResponse } from '@/core/models/service.model';
import { ISaleInvoiceFullRawResponse, ISaleInvoiceFullResponse } from '@/domains/consumer/models';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { POS_SALE_INVOICES_API_ROUTES } from '../constants';
import {
  IPosSaleInvoiceFiscalActionResponse,
  IPosSaleInvoiceFiscalAttemptsResponse,
  IPosSaleInvoiceFiscalStatusResponse,
  IPosSaleInvoicesFilterDto,
  IPosSaleInvoicesSummaryRawResponse,
  IPosSaleInvoicesSummaryResponse,
} from '../models';

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

  private apiRoutes = POS_SALE_INVOICES_API_ROUTES;

  getAll(
    query: IPosSaleInvoicesFilterDto = {},
  ): Observable<IPaginatedResponse<IPosSaleInvoicesSummaryResponse>> {
    return this.http.get<IPaginatedResponse<IPosSaleInvoicesSummaryRawResponse>>(
      this.apiRoutes.list(),
      { params: query as Record<string, string | number | boolean> },
    );
  }

  getSingle(invoiceId: string): Observable<ISaleInvoiceFullResponse> {
    return this.http.get<ISaleInvoiceFullRawResponse>(this.apiRoutes.single(invoiceId));
  }

  sendToTsp(invoiceId: string): Observable<IPosSaleInvoiceFiscalActionResponse> {
    return this.http.post<IPosSaleInvoiceFiscalActionResponse>(
      this.apiRoutes.tsp.send(invoiceId),
      {},
    );
  }

  retrySendToTsp(invoiceId: string): Observable<IPosSaleInvoiceFiscalActionResponse> {
    return this.http.post<IPosSaleInvoiceFiscalActionResponse>(
      this.apiRoutes.tsp.retry(invoiceId),
      {},
    );
  }

  getFiscalStatus(invoiceId: string): Observable<IPosSaleInvoiceFiscalStatusResponse> {
    return this.http.get<IPosSaleInvoiceFiscalStatusResponse>(this.apiRoutes.tsp.status(invoiceId));
  }

  getInquiry(invoiceId: string): Observable<IPosSaleInvoiceFiscalStatusResponse> {
    return this.http.get<IPosSaleInvoiceFiscalStatusResponse>(
      this.apiRoutes.tsp.getInquiry(invoiceId),
      {},
    );
  }

  revoke(invoiceId: string): Observable<IPosSaleInvoiceFiscalStatusResponse> {
    return this.http.post<IPosSaleInvoiceFiscalStatusResponse>(
      this.apiRoutes.tsp.revoke(invoiceId),
      {},
    );
  }

  getFiscalAttempts(invoiceId: string): Observable<IPosSaleInvoiceFiscalAttemptsResponse> {
    return this.http.get<IPosSaleInvoiceFiscalAttemptsResponse>(
      this.apiRoutes.tsp.attempts(invoiceId),
    );
  }
}
