package com.realnet.OpenAi.Controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.realnet.OpenAi.Services.HtmlBuilder3;
import com.realnet.OpenAi.Services.HtmlGenerTorService;
import com.realnet.fnd.response.EntityResponse;

@RestController
@RequestMapping("/api/html")
public class HtmlGeneratorController {

	@Autowired
	private HtmlGenerTorService htmlGenerTorService;

	@PostMapping
	public String generateHtmlFromJson(@RequestBody String jsonBody) {
		String json = htmlGenerTorService.generateHtmlFromJson(jsonBody);
		return json;
	}
//
//	@PostMapping("/generate")
//	public ResponseEntity<Map<String, String>> generateHtml(@RequestBody HtmlRequest request) {
//		String sectionType = request.getSectionType();
//		Map<String, Object> structure = request.getJsonStructure();
//
//		String html = HtmlRenderer.render(sectionType, structure);
//
//		Map<String, String> result = new HashMap<>();
//		result.put("html", html);
//		return ResponseEntity.ok(result);
//	}

	@PostMapping("/generate")
	public ResponseEntity<?> generateHtml(@RequestBody Map<String, Object> request) {
		try {
			Object jsonStructure = request.get("jsonStructure");

			if (jsonStructure == null) {
				return ResponseEntity.badRequest().body("Missing 'jsonStructure' in request.");
			}

			String html = HtmlBuilder3.buildHtml(jsonStructure);
			return ResponseEntity.ok(new EntityResponse(html));
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseEntity.status(500).body("⚠️ Failed to generate HTML due to server error.");
		}
	}
}
