package ai.decompile.workspace.controller;

import ai.decompile.workspace.model.dto.WorkspaceRequest;
import ai.decompile.workspace.model.dto.WorkspaceResponse;
import ai.decompile.workspace.service.WorkspaceService;
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
@RequestMapping("/workspaces")
@RequiredArgsConstructor
public class WorkspaceController {
  private final WorkspaceService workspaceService;

  @Operation(
      summary = "Get a list of workspaces",
      description = "Retrieve a list of workspaces, optionally filtered by name.",
      tags = {"Workspace"},
      operationId = "getWorkspaces")
  @GetMapping
  public List<WorkspaceResponse> getWorkspaces(
      @Parameter(
              description = "Filter workspaces whose name contains this value (case-insensitive)")
          @RequestParam(required = false)
          String name) {
    return workspaceService.getWorkspaces(name);
  }

  @Operation(
      summary = "Get a workspace by ID",
      description = "Retrieve a single workspace by its unique identifier.",
      tags = {"Workspace"},
      operationId = "getWorkspace")
  @GetMapping("/{id}")
  public WorkspaceResponse getWorkspace(@PathVariable UUID id) {
    return workspaceService.getWorkspaceById(id);
  }

  @Operation(
      summary = "Create a workspace",
      description = "Create a workspace",
      tags = {"Workspace"},
      operationId = "createWorkspace")
  @PostMapping
  public WorkspaceResponse createWorkspace(@RequestBody WorkspaceRequest request) {
    return workspaceService.createWorkspace(request);
  }

  @Operation(
      summary = "Update a workspace",
      description = "Update a workspace",
      tags = {"Workspace"},
      operationId = "updateWorkspace")
  @PutMapping("/{id}")
  public WorkspaceResponse updateWorkspace(
      @PathVariable UUID id, @RequestBody WorkspaceRequest request) {
    return workspaceService.updateWorkspace(id, request);
  }

  @Operation(
      summary = "Delete a workspace",
      description = "Delete a workspace",
      tags = {"Workspace"},
      operationId = "deleteWorkspace")
  @DeleteMapping("/{id}")
  public void deleteWorkspace(@PathVariable UUID id) {
    workspaceService.deleteWorkspace(id);
  }
}
