package ai.decompile.job.controller;

import ai.decompile.job.model.dto.JobListResponse;
import ai.decompile.job.model.dto.JobResponse;
import ai.decompile.job.model.enums.JobStatus;
import ai.decompile.job.model.enums.JobType;
import ai.decompile.job.service.JobService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/jobs")
@RequiredArgsConstructor
public class JobController {
  private final JobService jobService;

  @Operation(
      summary = "List jobs",
      description =
          "Retrieve all jobs, optionally filtered by project, status, type, or search term (matches binary or project name).",
      tags = {"Job"},
      operationId = "getJobs")
  @GetMapping
  public JobListResponse getJobs(
      @Parameter(description = "Filter jobs by project id") @RequestParam(required = false)
          UUID projectId,
      @Parameter(description = "Filter jobs by status") @RequestParam(required = false)
          JobStatus status,
      @Parameter(description = "Filter jobs by type") @RequestParam(required = false) JobType type,
      @Parameter(description = "Search term for binary filename or project name (case-insensitive)")
          @RequestParam(required = false)
          String search) {
    return jobService.getJobs(projectId, status, type, search);
  }

  @Operation(
      summary = "Get a job by id",
      description = "Retrieve a single job by its id.",
      tags = {"Job"},
      operationId = "getJob")
  @GetMapping("/{jobId}")
  public JobResponse getJob(@PathVariable UUID jobId) {
    return jobService.getJob(jobId);
  }

  @Operation(
      summary = "Cancel a job",
      description =
          "Cancel a job that is enqueued, started, or in progress. Returns 409 if the job is already in a final state.",
      tags = {"Job"},
      operationId = "cancelJob")
  @PostMapping("/{jobId}/cancel")
  @ResponseStatus(HttpStatus.OK)
  public JobResponse cancelJob(@PathVariable UUID jobId) {
    return jobService.cancelJob(jobId);
  }
}
