package com.realnet.DataLake.Controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
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.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.realnet.DataLake.Entity.Data_lake;
import com.realnet.DataLake.Services.Data_lakeService;
import com.realnet.fnd.response.EntityResponse;

@RequestMapping(value = "/Data_lake")
@CrossOrigin("*")
@RestController
public class Data_lakeController {
	@Autowired
	private Data_lakeService Service;

	@Value("${projectPath}")
	private String projectPath;

	@PostMapping("/Data_lake")
	public Data_lake Savedata(@RequestBody Data_lake data) {
		Data_lake save = Service.Savedata(data);

		System.out.println("data saved..." + save);

		return save;
	}

	@PutMapping("/Data_lake/{id}")
	public Data_lake update(@RequestBody Data_lake data, @PathVariable Integer id) {
		Data_lake update = Service.update(data, id);
		System.out.println("data update..." + update);
		return update;
	}

//	get all with pagination
	@GetMapping("/Data_lake/getall/page")
	public Page<Data_lake> getall(@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "size", required = false) Integer size) {
		Pageable paging = PageRequest.of(page, size);
		Page<Data_lake> get = Service.getAllWithPagination(paging);

		return get;

	}

	@GetMapping("/Data_lake")
	public List<Data_lake> getdetails() {
		List<Data_lake> get = Service.getdetails();
		return get;
	}
// get all without authentication 

	@GetMapping("/token/Data_lake")
	public List<Data_lake> getallwioutsec() {
		List<Data_lake> get = Service.getdetails();
		return get;
	}

	@GetMapping("/Data_lake/{id}")
	public Data_lake getdetailsbyId(@PathVariable Integer id) {
		Data_lake get = Service.getdetailsbyId(id);
		return get;
	}

	@DeleteMapping("/Data_lake/{id}")
	public ResponseEntity<?> delete_by_id(@PathVariable Integer id) {
		Service.delete_by_id(id);
		return new ResponseEntity<>(new EntityResponse("Deleted"), HttpStatus.OK);

	}

//	json Updtaed

	@PutMapping("/Data_lake/json/{id}")
	public Data_lake update(@PathVariable Integer id) throws JsonProcessingException {
		Data_lake update = Service.Updatejson(id);
		System.out.println("json update..." + update);
		return update;
	}

	@GetMapping("/Data_lake/merge/{id}")
	public ResponseEntity<?> mergeBatchData(@PathVariable Integer id) {
		try {
			// Get merged JSON string from service
			String mergedJson = Service.mergeBatchData(id);

			// Parse the merged JSON string back into JSON structure
			ObjectMapper mapper = new ObjectMapper();
			JsonNode jsonNode = mapper.readTree(mergedJson);

			// Return as actual JSON (not as string)
			return ResponseEntity.ok(jsonNode);

		} catch (Exception e) {
			// Build error response manually (Map.of() not available in Java 8)
			Map<String, String> error = new HashMap<>();
			error.put("error", e.getMessage());
			return ResponseEntity.status(500).body(error);
		}
	}
}