import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Allowance } from '../models/allowance';

@Injectable({
    providedIn: 'root'
})
export class AllowanceService {
    private url = 'https://allowanceplanner.seeseepuff.be/api';

    constructor(private http: HttpClient) {}

    getAllowanceList(userId: number): Observable<Array<Allowance>> {
        return this.http.get<Allowance[]>(`${this.url}/user/${userId}/allowance`);
    }

    getAllowanceById(allowanceId: number, userId: number): Observable<Allowance> {
        return this.http.get<Allowance>(`${this.url}/user/${userId}/allowance/${allowanceId}`);
    }

    createAllowance(allowance: Partial<Allowance>, userId: number) {
        this.http.post(`${this.url}/user/${userId}/allowance`, allowance).subscribe();
    }

    updateAllowance(allowance: Partial<Allowance>, allowanceId: number, userId: number) {
        this.http.put(`${this.url}/user/${userId}/allowance/${allowanceId}`, allowance).subscribe();
    }

    deleteAllowance(allowanceId: number, userId: number) {
        this.http.delete(`${this.url}/user/${userId}/allowance/${allowanceId}`).subscribe();
    }

    completeGoal(goalId: number, userId: number) {
        this.http.post(`${this.url}/user/${userId}/allowance/${goalId}/complete`, {}).subscribe();
    }

    addOrSpendAllowance(goalId: number, userId: number, amount: number, description: string) {
        this.http.post(`${this.url}/user/${userId}/allowance/${goalId}/add`, { amount, description }).subscribe();
    }
}