package com.realnet.DynamicDashbard.services;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.realnet.DynamicDashbard.entities.ChartType;
import com.realnet.DynamicDashbard.repositories.ChartTypeRepository;

@Service
public class ChartTypeService {

	@Autowired
	private ChartTypeRepository chartTypeRepository;

	public List<ChartType> getAllChartTypes() {
		return chartTypeRepository.findAll();
	}

	public List<ChartType> getActiveChartTypes() {
		return chartTypeRepository.findByIsActiveTrue();
	}

	public Optional<ChartType> getChartTypeById(Long id) {
		return chartTypeRepository.findById(id);
	}

	public Optional<ChartType> getChartTypeByName(String name) {
		return chartTypeRepository.findByName(name);
	}

	public ChartType saveChartType(ChartType chartType) {
		return chartTypeRepository.save(chartType);
	}

	public ChartType updateChartType(Long id, ChartType chartTypeDetails) {
		ChartType chartType = chartTypeRepository.findById(id)
				.orElseThrow(() -> new RuntimeException("ChartType not found with id: " + id));

		chartType.setName(chartTypeDetails.getName());
		chartType.setDisplayName(chartTypeDetails.getDisplayName());
		chartType.setDescription(chartTypeDetails.getDescription());
		chartType.setIsActive(chartTypeDetails.getIsActive());

		return chartTypeRepository.save(chartType);
	}

	public void deleteChartType(Long id) {
		ChartType chartType = chartTypeRepository.findById(id)
				.orElseThrow(() -> new RuntimeException("ChartType not found with id: " + id));
		chartTypeRepository.delete(chartType);
	}
}