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.ChartType;
import com.realnet.DynamicDashbard.entities.DynamicField;
import com.realnet.DynamicDashbard.services.ChartTypeService;
import com.realnet.DynamicDashbard.services.DynamicFieldService;

@RestController
@RequestMapping("/api/dynamic-fields")
@CrossOrigin(origins = "*")
public class DynamicFieldController {

	@Autowired
	private DynamicFieldService dynamicFieldService;

	@Autowired
	private ChartTypeService chartTypeService;

	@GetMapping
	public List<DynamicField> getAllDynamicFields() {
		return dynamicFieldService.getAllDynamicFields();
	}

	@GetMapping("/chart-type/{chartTypeId}")
	public List<DynamicField> getDynamicFieldsByChartType(@PathVariable Long chartTypeId) {
		return dynamicFieldService.getDynamicFieldsByChartType(chartTypeId);
	}

	@GetMapping("/{id}")
	public ResponseEntity<DynamicField> getDynamicFieldById(@PathVariable Long id) {
		Optional<DynamicField> dynamicField = dynamicFieldService.getDynamicFieldById(id);
		return dynamicField.map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
	}

	@PostMapping
	public ResponseEntity<DynamicField> createDynamicField(@RequestParam Long chartTypeId,
			@RequestBody DynamicField dynamicField) {
		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: " + dynamicField.getChartType().getId()));
				dynamicField.setChartType(chartType);
			}

			DynamicField createdDynamicField = dynamicFieldService.saveDynamicField(dynamicField);
			return ResponseEntity.ok(createdDynamicField);
		} catch (Exception e) {
			return ResponseEntity.badRequest().build();
		}
	}

	@PutMapping("/{id}")
	public ResponseEntity<DynamicField> updateDynamicField(@PathVariable Long id,
			@RequestBody DynamicField dynamicFieldDetails) {
		try {
			DynamicField updatedDynamicField = dynamicFieldService.updateDynamicField(id, dynamicFieldDetails);
			return ResponseEntity.ok(updatedDynamicField);
		} catch (RuntimeException e) {
			return ResponseEntity.notFound().build();
		}
	}

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