package com.realnet.DynamicDashbard.controllers;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.realnet.DynamicDashbard.entities.ChartTemplate;
import com.realnet.DynamicDashbard.entities.ChartType;
import com.realnet.DynamicDashbard.services.ChartTemplateService;
import com.realnet.DynamicDashbard.services.ChartTypeService;

@RestController
@RequestMapping("/api/chart-templates")
@CrossOrigin(origins = "*")
public class ChartTemplateController {

	@Autowired
	private ChartTemplateService chartTemplateService;

	@Autowired
	private ChartTypeService chartTypeService;

	@GetMapping("/all")
	public List<ChartTemplate> getAllChartTemplates() {
		return chartTemplateService.getAllChartTemplates();
	}

	@GetMapping("/chart-type/{chartTypeId}")
	public List<ChartTemplate> getChartTemplatesByChartType(@PathVariable Long chartTypeId) {
		return chartTemplateService.getChartTemplatesByChartType(chartTypeId);
	}

	@GetMapping("/{id}")
	public ResponseEntity<ChartTemplate> getChartTemplateById(@PathVariable Long id) {
		Optional<ChartTemplate> chartTemplate = chartTemplateService.getChartTemplateById(id);
		return chartTemplate.map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
	}

	@PostMapping
	public ResponseEntity<ChartTemplate> createChartTemplate(@RequestParam Long chartTypeId,
			@RequestBody ChartTemplate chartTemplate) {
		try {
			// If chartType is provided as ID only, fetch the full ChartType object
			if (chartTypeId != null) {
				ChartType chartType = chartTypeService.getChartTypeById(chartTypeId)
						.orElseThrow(() -> new RuntimeException(
								"ChartType not found with id: " + chartTemplate.getChartType().getId()));
				chartTemplate.setChartType(chartType);
			}

			ChartTemplate createdChartTemplate = chartTemplateService.saveChartTemplate(chartTemplate);
			return ResponseEntity.ok(createdChartTemplate);
		} catch (Exception e) {
			return ResponseEntity.badRequest().build();
		}
	}

	@PutMapping("/{id}")
	public ResponseEntity<ChartTemplate> updateChartTemplate(@PathVariable Long id,
			@RequestBody ChartTemplate chartTemplateDetails) {
		try {
			ChartTemplate updatedChartTemplate = chartTemplateService.updateChartTemplate(id, chartTemplateDetails);
			return ResponseEntity.ok(updatedChartTemplate);
		} catch (RuntimeException e) {
			return ResponseEntity.notFound().build();
		}
	}

	@DeleteMapping("/{id}")
	public ResponseEntity<Void> deleteChartTemplate(@PathVariable Long id) {
		try {
			chartTemplateService.deleteChartTemplate(id);
			return ResponseEntity.ok().build();
		} catch (RuntimeException e) {
			return ResponseEntity.notFound().build();
		}
	}
}