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

export interface ChartType {
  id: number;
  name: string;
  displayName: string;
  description: string;
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
}

@Injectable({
  providedIn: 'root'
})
export class ChartTypeService {
  private chartTypesUrl = 'api/chart-types';

  constructor(private apiRequest: ApiRequestService) { }

  // Get all chart types
  getAllChartTypes(): Observable<ChartType[]> {
    console.log('Fetching all chart types from', this.chartTypesUrl);
    return this.apiRequest.get(this.chartTypesUrl);
  }

  // Get chart type by ID
  getChartTypeById(id: number): Observable<ChartType> {
    const url = `${this.chartTypesUrl}/${id}`;
    console.log(`Fetching chart type ${id} from ${url}`);
    return this.apiRequest.get(url);
  }

  // Create new chart type
  createChartType(chartType: Partial<ChartType>): Observable<ChartType> {
    console.log('Creating chart type:', chartType);
    return this.apiRequest.post(this.chartTypesUrl, chartType);
  }

  // Update chart type
  updateChartType(id: number, chartType: ChartType): Observable<ChartType> {
    const url = `${this.chartTypesUrl}/${id}`;
    console.log(`Updating chart type ${id}:`, chartType);
    return this.apiRequest.put(url, chartType);
  }

  // Delete chart type
  deleteChartType(id: number): Observable<void> {
    const url = `${this.chartTypesUrl}/${id}`;
    console.log(`Deleting chart type ${id}`);
    return this.apiRequest.delete(url);
  }
}