package marketing.heyday.hartmann.fotodocumentation.rest;

import java.util.Optional;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.resteasy.annotations.GZIP;
import org.wildfly.security.auth.server.SecurityIdentity;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.ejb.EJB;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import marketing.heyday.hartmann.fotodocumentation.core.service.CustomerPictureService;
import marketing.heyday.hartmann.fotodocumentation.core.utils.LoginUtils;
import marketing.heyday.hartmann.fotodocumentation.rest.jackson.JsonSchemaValidate;
import marketing.heyday.hartmann.fotodocumentation.rest.vo.CustomerPictureValue;

/**
 * 
 * <p>Copyright: Copyright (c) 2024</p>
 * <p>Company: heyday Marketing GmbH</p>
 * @author <a href="mailto:p.verboom@heyday.marketing">Patrick Verboom</a>
 * @version 1.0
 * 
 * created: 19 Jan 2026
 */
@RequestScoped
@Path("customer-picture")
public class CustomerPictureResource {
	private static final Log LOG = LogFactory.getLog(CustomerPictureResource.class);
		
	@EJB
	private CustomerPictureService customerPictureService;

	@Inject
	private LoginUtils loginUtils;
	
	@GZIP
	@POST
	@Path("")
	@Consumes(MediaType.APPLICATION_JSON)
	@Operation(summary = "Add Customer Image to database")
	@ApiResponse(responseCode = "200", description = "Add successfull")
	public Response doAddCustomerPicture(@Context HttpServletRequest httpServletRequest, @JsonSchemaValidate("schema/customer_picture_add.json") CustomerPictureValue customerPictureValue) {
		Optional<SecurityIdentity> identity = loginUtils.authenticate(httpServletRequest);
		if (identity.isEmpty()) {
			LOG.debug("identity empty login invalid");
			return Response.status(Status.UNAUTHORIZED).build();
		}
		
		boolean success = customerPictureService.addCustomerPicture(customerPictureValue);
		return success ? Response.ok().build() : Response.status(Status.BAD_REQUEST).build();
	}
}
