package auctiora;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Map;

@Slf4j
@Path("/api")
public class StatusResource {
   
   private static final DateTimeFormatter FORMATTER =
           DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z")
                            .withZone(ZoneId.systemDefault());
   
   @ConfigProperty(name = "application.version", defaultValue = "1.0-SNAPSHOT")
   String appVersion;
   @ConfigProperty(name = "application.groupId")
   String groupId;
   
   @ConfigProperty(name = "application.artifactId")
   String artifactId;
   
   @ConfigProperty(name = "application.version")
   String version;
   
   // Java 16+ Record for structured response
   public record StatusResponse(
           String groupId,
           String artifactId,
           String version,
           String status,
           String timestamp,
           String mvnVersion,
           String javaVersion,
           String os,
           String openCvVersion
   ) { }
   
   @GET
   @Path("/status")
   @Produces(MediaType.APPLICATION_JSON)
   public StatusResponse getStatus() {
      log.info("Status endpoint called");
      
      return new StatusResponse(groupId, artifactId, version,
                                "running",
                                FORMATTER.format(Instant.now()),
                                appVersion,
                                System.getProperty("java.version"),
                                System.getProperty("os.name") + " " + System.getProperty("os.arch"),
                                getOpenCvVersion()
      );
   }
   
   @GET
   @Path("/hello")
   @Produces(MediaType.APPLICATION_JSON)
   public Map<String, String> sayHello() {
      log.info("hello endpoint called");
      
      return Map.of(
              "message", "Hello from Scrape-UI!",
              "timestamp", FORMATTER.format(Instant.now()),
              "openCvVersion", getOpenCvVersion()
                   );
   }
   
   private String getOpenCvVersion() {
      try {
         // Load OpenCV if not already loaded (safe to call multiple times)
         nu.pattern.OpenCV.loadLocally();
         return org.opencv.core.Core.VERSION;
      } catch (Exception e) {
         return "4.9.0 (default)";
      }
   }
}