package ai.decompile.workspace.controller;

import ai.decompile.workspace.model.dto.ProjectRequest;
import ai.decompile.workspace.model.dto.ProjectResponse;
import ai.decompile.workspace.service.ProjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
public class ProjectController {
  private final ProjectService projectService;

  @Operation(
      summary = "List projects of a workspace",
      description = "Retrieve all projects belonging to a workspace, optionally filtered by name.",
      tags = {"Project"},
      operationId = "getProjects")
  @GetMapping("/workspaces/{workspaceId}/projects")
  public List<ProjectResponse> getProjects(
      @PathVariable UUID workspaceId,
      @Parameter(description = "Filter projects whose name contains this value (case-insensitive)")
          @RequestParam(required = false)
          String name) {
    return projectService.getProjects(workspaceId, name);
  }

  @Operation(
      summary = "Get a project",
      description = "Retrieve a single project by ID.",
      tags = {"Project"},
      operationId = "getProject")
  @GetMapping("/projects/{projectId}")
  public ProjectResponse getProject(@PathVariable UUID projectId) {
    return projectService.getProjectById(projectId);
  }

  @Operation(
      summary = "Create a project",
      description = "Create a project in a workspace.",
      tags = {"Project"},
      operationId = "createProject")
  @PostMapping("/workspaces/{workspaceId}/projects")
  public ProjectResponse createProject(
      @PathVariable UUID workspaceId, @RequestBody ProjectRequest request) {
    return projectService.createProject(workspaceId, request);
  }

  @Operation(
      summary = "Update a project",
      description = "Update a project's name and description.",
      tags = {"Project"},
      operationId = "updateProject")
  @PutMapping("/projects/{projectId}")
  public ProjectResponse updateProject(
      @PathVariable UUID projectId, @RequestBody ProjectRequest request) {
    return projectService.updateProject(projectId, request);
  }

  @Operation(
      summary = "Delete a project",
      description = "Delete a project.",
      tags = {"Project"},
      operationId = "deleteProject")
  @DeleteMapping("/projects/{projectId}")
  public void deleteProject(@PathVariable UUID projectId) {
    projectService.deleteProject(projectId);
  }
}
