package pro.taskana;

import jakarta.inject.Inject;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pro.taskana.classification.api.ClassificationService;
import pro.taskana.classification.api.models.Classification;
import pro.taskana.task.api.models.Task;
import pro.taskana.task.internal.models.ObjectReferenceImpl;
import pro.taskana.workbasket.api.WorkbasketType;
import pro.taskana.workbasket.api.models.Workbasket;

@Path("/test")
public class TaskanaCdiTestRestController {

  private static final Logger LOGGER = LoggerFactory.getLogger(TaskanaCdiTestRestController.class);
  private static final String CDIDOMAIN = "CDIDOMAIN";
  private static final String CLASSIFICATION_TYPE = "T1";

  private final TaskanaEjb taskanaEjb;

  private final ClassificationService classificationService;

  public TaskanaCdiTestRestController() {
    this.taskanaEjb = null;
    this.classificationService = null;
  }

  @Inject
  public TaskanaCdiTestRestController(
      TaskanaEjb taskanaEjb, ClassificationService classificationService) {
    this.taskanaEjb = taskanaEjb;
    this.classificationService = classificationService;
  }

  @GET
  public Response startTask() throws Exception {
    Workbasket workbasket = taskanaEjb.getWorkbasketService().newWorkbasket("KEY", CDIDOMAIN);
    workbasket.setName("wb");
    workbasket.setType(WorkbasketType.PERSONAL);
    workbasket = taskanaEjb.getWorkbasketService().createWorkbasket(workbasket);
    Classification classification =
        classificationService.newClassification("TEST", CDIDOMAIN, CLASSIFICATION_TYPE);
    taskanaEjb.getClassificationService().createClassification(classification);

    Task task = taskanaEjb.getTaskService().newTask(workbasket.getId());
    task.setClassificationKey(classification.getKey());
    task.setName("startTask");
    ObjectReferenceImpl objRef = new ObjectReferenceImpl();
    objRef.setCompany("aCompany");
    objRef.setSystem("aSystem");
    objRef.setSystemInstance("anInstance");
    objRef.setType("aType");
    objRef.setValue("aValue");
    task.setPrimaryObjRef(objRef);

    Task result = taskanaEjb.getTaskService().createTask(task);

    LOGGER.info(result.getId() + ":" + result.getOwner());
    return Response.status(200).entity(result.getId()).build();
  }

  @POST
  public Response rollbackTask() throws Exception {
    Workbasket workbasket =
        taskanaEjb.getWorkbasketService().newWorkbasket("KEY_ROLLBACK", CDIDOMAIN);
    workbasket.setName("wb_rollback");
    workbasket.setType(WorkbasketType.PERSONAL);
    workbasket = taskanaEjb.getWorkbasketService().createWorkbasket(workbasket);
    Classification classification =
        classificationService.newClassification("TEST_ROLLBACK", CDIDOMAIN, CLASSIFICATION_TYPE);
    taskanaEjb.getClassificationService().createClassification(classification);

    try {
      taskanaEjb.triggerRollback(workbasket.getId(), classification.getKey());
    } catch (Exception e) {
      if (!"java.lang.RuntimeException: Expected Test Exception".equals(e.getMessage())) {
        throw e;
      }
    }
    return Response.status(204).build();
  }

  @DELETE
  @Path("{id}")
  public void completeTask(@PathParam("id") String id) throws Exception {
    LOGGER.info(id);
    taskanaEjb.getTaskService().forceCompleteTask(id);
  }
}
