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

@Injectable({
  providedIn: 'root'
})
export class ComponentPropertyService {
  private componentPropertiesUrl = 'api/component-properties';

  constructor(private apiRequest: ApiRequestService) { }

  // Get all component properties for a component
  getComponentPropertiesByComponent(componentId: number): Observable<ComponentProperty[]> {
    const url = `${this.componentPropertiesUrl}/component/${componentId}`;
    return this.apiRequest.get(url);
  }

  // Get component property by ID
  getComponentPropertyById(id: number): Observable<ComponentProperty> {
    const url = `${this.componentPropertiesUrl}/${id}`;
    return this.apiRequest.get(url);
  }

  // Create new component property
  createComponentProperty(componentProperty: Partial<ComponentProperty>): Observable<ComponentProperty> {
    return this.apiRequest.post(this.componentPropertiesUrl, componentProperty);
  }

  // Update component property
  updateComponentProperty(id: number, componentProperty: ComponentProperty): Observable<ComponentProperty> {
    const url = `${this.componentPropertiesUrl}/${id}`;
    return this.apiRequest.put(url, componentProperty);
  }

  // Delete component property
  deleteComponentProperty(id: number): Observable<void> {
    const url = `${this.componentPropertiesUrl}/${id}`;
    return this.apiRequest.delete(url);
  }
}