package com.davos.assessment.project;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/projects/{slug}")
public class ProjectPublicController {
  private final ProjectAccessService projectAccessService;

  public ProjectPublicController(ProjectAccessService projectAccessService) {
    this.projectAccessService = projectAccessService;
  }

  @GetMapping("/public-config")
  public ProjectConfigResponse publicConfig(@PathVariable String slug) {
    Project project = projectAccessService.findEnabledProject(slug);
    return ProjectConfigResponse.from(project);
  }

  public record ProjectConfigResponse(
      Long id,
      String slug,
      String name,
      String landingTitle,
      String landingDescription,
      String reportTitle,
      String logoUrl,
      boolean enabled) {
    static ProjectConfigResponse from(Project project) {
      return new ProjectConfigResponse(
          project.getId(),
          project.getSlug(),
          project.getName(),
          project.getLandingTitle(),
          project.getLandingDescription(),
          project.getReportTitle(),
          project.getLogoUrl(),
          project.isEnabled());
    }
  }
}
