import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { ApiRequestService } from 'src/app/services/api/api-request.service';
import { ChartTemplate } from './chart-config-manager.component';

@Injectable({
  providedIn: 'root'
})
export class ChartTemplateService {
  private chartTemplatesUrl = 'api/chart-templates';

  constructor(private apiRequest: ApiRequestService) { }

  // Get all chart templates for a chart type
  getChartTemplatesByChartType(chartTypeId: number): Observable<ChartTemplate[]> {
    const url = `${this.chartTemplatesUrl}/chart-type/${chartTypeId}`;
    return this.apiRequest.get(url);
  }

  // Get chart template by ID
  getChartTemplateById(id: number): Observable<ChartTemplate> {
    const url = `${this.chartTemplatesUrl}/${id}`;
    return this.apiRequest.get(url);
  }

  // Create new chart template with optional chart type ID as parameter
  createChartTemplate(chartTemplate: Partial<ChartTemplate>, chartTypeId?: number): Observable<ChartTemplate> {
    let url = this.chartTemplatesUrl;
    if (chartTypeId) {
      url = `${this.chartTemplatesUrl}?chartTypeId=${chartTypeId}`;
    }
    return this.apiRequest.post(url, chartTemplate);
  }

  // Update chart template
  updateChartTemplate(id: number, chartTemplate: ChartTemplate): Observable<ChartTemplate> {
    const url = `${this.chartTemplatesUrl}/${id}`;
    return this.apiRequest.put(url, chartTemplate);
  }

  // Delete chart template
  deleteChartTemplate(id: number): Observable<void> {
    const url = `${this.chartTemplatesUrl}/${id}`;
    return this.apiRequest.delete(url);
  }
}