import { inject, Injectable } from '@angular/core';
import { firstValueFrom } from 'rxjs';

import { BaseApiService } from '../../../../../../core/services/base-api.service';
import { TenantService } from '../../../../../../core/services/tenant.service';

export interface TicketResponse {
  id: number;
  tenant_code: string;
  ticket: string;
  name: string;
  description: string | null;
  source_catalog_item_id: number | null;
  source_variant_id: number | null;
  starts_at: string | null;
  expires_at: string | null;
  used_at: string | null;
  is_valid: boolean;
  is_expired: boolean;
  is_used: boolean;
}

@Injectable()
export class TicketService extends BaseApiService {
  private readonly tenantService = inject(TenantService);

  getTickets(): Promise<TicketResponse[]> {
    return firstValueFrom(
      this.http.get<{ data: TicketResponse[] }>(
        `${this.tenantService.getTenantApiUrl()}/tickets`,
      ),
    ).then((response) => response.data ?? []);
  }

  downloadPdf(ticketIds: number[]): Promise<Blob> {
    return firstValueFrom(
      this.http.post(
        `${this.tenantService.getTenantApiUrl()}/tickets/pdf`,
        { ticket_ids: ticketIds },
        {
          responseType: 'blob',
        },
      ),
    );
  }
}
