import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { ApiService } from '../../../core/services/api.service';
import {
  RequestResponseDto,
  RequestDetailResponseDto,
  CreateRequestDto,
  UpdateRequestDto,
  PaginatedRequestsResponse,
  RequestFilters,
} from '../../../api/models';

@Injectable({
  providedIn: 'root',
})
export class RequestService {
  private readonly api = inject(ApiService);

  getRequests(filters?: RequestFilters): Observable<PaginatedRequestsResponse> {
    return this.api.get<PaginatedRequestsResponse>('/requests', filters as Record<string, string | number | boolean>);
  }

  getRequest(id: string): Observable<RequestDetailResponseDto> {
    return this.api.get<RequestDetailResponseDto>(`/requests/${id}`);
  }

  createRequest(dto: CreateRequestDto): Observable<RequestResponseDto> {
    return this.api.post<RequestResponseDto>('/requests', dto);
  }

  updateRequest(id: string, dto: UpdateRequestDto): Observable<RequestResponseDto> {
    return this.api.patch<RequestResponseDto>(`/requests/${id}`, dto);
  }

  submitRequest(id: string): Observable<RequestResponseDto> {
    return this.api.post<RequestResponseDto>(`/requests/${id}/submit`, {});
  }

  cancelRequest(id: string): Observable<RequestResponseDto> {
    return this.api.post<RequestResponseDto>(`/requests/${id}/cancel`, {});
  }

  deleteRequest(id: string): Observable<void> {
    return this.api.delete<void>(`/requests/${id}`);
  }
}
