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.RestController;

import com.realnet.DynamicDashbard.entities.ChartType;
import com.realnet.DynamicDashbard.entities.UiComponent;
import com.realnet.DynamicDashbard.services.ChartTypeService;
import com.realnet.DynamicDashbard.services.UiComponentService;

@RestController
@RequestMapping("/api/ui-components")
@CrossOrigin(origins = "*")
public class UiComponentController {

	@Autowired
	private UiComponentService uiComponentService;

	@Autowired
	private ChartTypeService chartTypeService;

	@GetMapping
	public List<UiComponent> getAllUiComponents() {
		return uiComponentService.getAllUiComponents();
	}

	@GetMapping("/chart-type/{chartTypeId}")
	public List<UiComponent> getUiComponentsByChartType(@PathVariable Long chartTypeId) {
		return uiComponentService.getUiComponentsByChartType(chartTypeId);
	}

	@GetMapping("/{id}")
	public ResponseEntity<UiComponent> getUiComponentById(@PathVariable Long id) {
		Optional<UiComponent> uiComponent = uiComponentService.getUiComponentById(id);
		return uiComponent.map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
	}

	@PostMapping
	public ResponseEntity<UiComponent> createUiComponent(@RequestBody UiComponent uiComponent) {
		try {
			// If chartType is provided as ID only, fetch the full ChartType object
			if (uiComponent.getChartType() != null && uiComponent.getChartType().getId() != null
					&& (uiComponent.getChartType().getName() == null
							|| uiComponent.getChartType().getName().isEmpty())) {
				ChartType chartType = chartTypeService.getChartTypeById(uiComponent.getChartType().getId())
						.orElseThrow(() -> new RuntimeException(
								"ChartType not found with id: " + uiComponent.getChartType().getId()));
				uiComponent.setChartType(chartType);
			}

			UiComponent createdUiComponent = uiComponentService.saveUiComponent(uiComponent);
			System.out.println(createdUiComponent.getComponentName() + " ui component created successfully..");
			return ResponseEntity.ok(createdUiComponent);
		} catch (Exception e) {
			return ResponseEntity.badRequest().build();
		}
	}

	@PutMapping("/{id}")
	public ResponseEntity<UiComponent> updateUiComponent(@PathVariable Long id,
			@RequestBody UiComponent uiComponentDetails) {
		try {
			UiComponent updatedUiComponent = uiComponentService.updateUiComponent(id, uiComponentDetails);
			return ResponseEntity.ok(updatedUiComponent);
		} catch (RuntimeException e) {
			return ResponseEntity.notFound().build();
		}
	}

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