import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import {
  MealPlanEntry,
  ShoppingListItem,
  UpsertMealPlanEntryInput,
} from '../models/meal-plan.models';

@Injectable({ providedIn: 'root' })
export class MealPlanService {
  constructor(private readonly http: HttpClient) {}

  getEntries(from: string, to: string): Observable<MealPlanEntry[]> {
    return this.http.get<MealPlanEntry[]>('/api/meal-plan', { params: { from, to } });
  }

  upsertEntry(input: UpsertMealPlanEntryInput): Observable<MealPlanEntry> {
    return this.http.put<MealPlanEntry>('/api/meal-plan', input);
  }

  deleteEntry(id: number): Observable<void> {
    return this.http.delete<void>(`/api/meal-plan/${id}`);
  }

  getShoppingList(from: string, to: string): Observable<ShoppingListItem[]> {
    return this.http.get<ShoppingListItem[]>('/api/meal-plan/shopping-list', { params: { from, to } });
  }
}
