package gc.mda.kcg.domain.aircraft;

import gc.mda.kcg.collector.aircraft.AircraftCacheStore;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/api/aircraft")
@RequiredArgsConstructor
public class AircraftController {

    private static final Set<String> VALID_REGIONS = Set.of("iran", "korea");

    private final AircraftCacheStore cacheStore;

    @GetMapping
    public ResponseEntity<Map<String, Object>> getAircraft(
            @RequestParam(defaultValue = "iran") String region) {

        if (!VALID_REGIONS.contains(region)) {
            return ResponseEntity.badRequest()
                    .body(Map.of("error", "유효하지 않은 region: " + region));
        }

        List<AircraftDto> aircraft = cacheStore.get(region);
        long lastUpdated = cacheStore.getLastUpdated(region);

        return ResponseEntity.ok(Map.of(
                "region", region,
                "count", aircraft.size(),
                "lastUpdated", lastUpdated,
                "aircraft", aircraft
        ));
    }
}
