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.ComponentProperty;
import com.realnet.DynamicDashbard.entities.UiComponent;
import com.realnet.DynamicDashbard.services.ComponentPropertyService;
import com.realnet.DynamicDashbard.services.UiComponentService;

@RestController
@RequestMapping("/api/component-properties")
@CrossOrigin(origins = "*")
public class ComponentPropertyController {

	@Autowired
	private ComponentPropertyService componentPropertyService;

	@Autowired
	private UiComponentService uiComponentService;

	@GetMapping
	public List<ComponentProperty> getAllComponentProperties() {
		return componentPropertyService.getAllComponentProperties();
	}

	@GetMapping("/component/{componentId}")
	public List<ComponentProperty> getComponentPropertiesByComponent(@PathVariable Long componentId) {
		return componentPropertyService.getComponentPropertiesByComponent(componentId);
	}

	@GetMapping("/{id}")
	public ResponseEntity<ComponentProperty> getComponentPropertyById(@PathVariable Long id) {
		Optional<ComponentProperty> componentProperty = componentPropertyService.getComponentPropertyById(id);
		return componentProperty.map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
	}

	@PostMapping
	public ResponseEntity<ComponentProperty> createComponentProperty(@RequestBody ComponentProperty componentProperty) {
		try {
			// If component is provided as ID only, fetch the full UiComponent object
			if (componentProperty.getComponent() != null && componentProperty.getComponent().getId() != null
					&& (componentProperty.getComponent().getComponentName() == null
							|| componentProperty.getComponent().getComponentName().isEmpty())) {
				UiComponent component = uiComponentService.getUiComponentById(componentProperty.getComponent().getId())
						.orElseThrow(() -> new RuntimeException(
								"UiComponent not found with id: " + componentProperty.getComponent().getId()));
				componentProperty.setComponent(component);
			}

			ComponentProperty createdComponentProperty = componentPropertyService
					.saveComponentProperty(componentProperty);
			return ResponseEntity.ok(createdComponentProperty);
		} catch (Exception e) {
			return ResponseEntity.badRequest().build();
		}
	}

	@PutMapping("/{id}")
	public ResponseEntity<ComponentProperty> updateComponentProperty(@PathVariable Long id,
			@RequestBody ComponentProperty componentPropertyDetails) {
		try {
			ComponentProperty updatedComponentProperty = componentPropertyService.updateComponentProperty(id,
					componentPropertyDetails);
			return ResponseEntity.ok(updatedComponentProperty);
		} catch (RuntimeException e) {
			return ResponseEntity.notFound().build();
		}
	}

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