package com.rdkr.tide_display.backend.firmware.controller;

import com.rdkr.tide_display.backend.firmware.service.FirmwareService;
import com.rdkr.tide_display.backend.gcp.Firmware;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
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
@RequiredArgsConstructor
@RequestMapping("/v1/versions")
public class VersionController {

  private final FirmwareService firmwareService;

  @GetMapping()
  public ResponseEntity<List<Firmware>> getVersions() {
    return ResponseEntity.of(Optional.ofNullable(firmwareService.getFirmwareVersions()));
  }

  @GetMapping("latest")
  public ResponseEntity<Firmware> getLatestVersion() {
    return ResponseEntity.of(Optional.of(firmwareService.getLatestFirmwareVersion()));
  }

  @GetMapping("{version:\\d\\.\\d\\.\\d}")
  public ResponseEntity<Firmware> getVersion(@PathVariable String version) {
    return ResponseEntity.of(firmwareService.getFirmwareVersion(version));
  }
}
