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

export interface Recipe {
  _id?: string;
  title: string;
  description: string;
  category: string;
  ingredients: string[];
  instructions: string;
  prepTime: number;
  cookTime: number;
  rating: number;
  createdAt?: string;
}

@Injectable({
  providedIn: 'root'
})
export class RecipeService {
  private baseUrl = 'http://localhost:5038/api/recipes';

  constructor(private http: HttpClient) {}

  getRecipes(): Observable<Recipe[]> {
    return this.http.get<Recipe[]>(`${this.baseUrl}/GetRecipes`);
  }

  getRecipe(id: string): Observable<Recipe> {
    return this.http.get<Recipe>(`${this.baseUrl}/GetRecipe/${id}`);
  }

  addRecipe(recipe: Recipe): Observable<any> {
    return this.http.post(`${this.baseUrl}/AddRecipe`, recipe);
  }

  updateRecipe(id: string, recipe: Recipe): Observable<any> {
    return this.http.put(`${this.baseUrl}/UpdateRecipe/${id}`, recipe);
  }

  deleteRecipe(id: string): Observable<any> {
    return this.http.delete(`${this.baseUrl}/DeleteRecipe/${id}`);
  }
}
