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

@Injectable({
  providedIn: 'root'
})
export class DesignApiService {
  private apiUrl = 'http://localhost:3000/api/designs';

  constructor(private http: HttpClient) { }

  recordDesignUsage(design: any): Observable<any> {
    return this.http.post(`${this.apiUrl}/record`, design).pipe(
      catchError((error: HttpErrorResponse) => {
        // 提取更详细的错误信息
        const errorMsg = error.error?.error || 
                        error.message || 
                        '保存设计时发生未知错误';
        return throwError(() => new Error(errorMsg));
      })
    );
  }

  getColorStatistics(): Observable<any> {
    return this.http.get(`${this.apiUrl}/color-statistics`);
  }
}