import { HttpClient } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from '../../../environments/environment';
import {
  CreateRecurringTransaction,
  RecurringTransaction,
  UpdateRecurringTransaction,
} from '../../models/recurring-transaction.model';

@Injectable({ providedIn: 'root' })
export class RecurringTransactionApi {
  private readonly http = inject(HttpClient);
  private readonly baseUrl = `${environment.apiUrl}recurring-transactions`;

  loadRecurringTransactions(teamId: number): Observable<RecurringTransaction[]> {
    return this.http.get<RecurringTransaction[]>(`${this.baseUrl}/${teamId}`);
  }

  createRecurringTransaction(
    entry: CreateRecurringTransaction,
  ): Observable<RecurringTransaction> {
    return this.http.post<RecurringTransaction>(this.baseUrl, entry);
  }

  updateRecurringTransaction(
    id: number,
    entry: UpdateRecurringTransaction,
  ): Observable<RecurringTransaction> {
    return this.http.patch<RecurringTransaction>(`${this.baseUrl}/${id}`, entry);
  }

  deleteRecurringTransaction(id: number): Observable<void> {
    return this.http.delete<void>(`${this.baseUrl}/${id}`);
  }
}
