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

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

    constructor(private http: HttpClient) {}

    getTaskList(): Observable<Array<Task>> {
        return this.http.get<Task[]>(`${this.url}/tasks`);
    }

    getTaskById(taskId: number): Observable<Task> {
        return this.http.get<Task>(`${this.url}/task/${taskId}`);
    }

    createTask(task: Partial<Task>) {
        this.http.post(`${this.url}/tasks`, task).subscribe();
    }

    updateTask(id: number, task: Partial<Task>) {
        this.http.put(`${this.url}/task/${id}`, task).subscribe();
    }

    completeTask(id: number) {
        this.http.post(`${this.url}/task/${id}/complete`, {}).subscribe();
    }

    deleteTask(id: number) {
        this.http.delete(`${this.url}/task/${id}`).subscribe();
    }
}