package net.siegeln.cameleer.saas.vendor;

import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/api/vendor/infrastructure")
@PreAuthorize("hasAuthority('SCOPE_platform:admin')")
public class InfrastructureController {

    private final InfrastructureService infraService;

    public InfrastructureController(InfrastructureService infraService) {
        this.infraService = infraService;
    }

    public record InfraOverviewResponse(
            InfrastructureService.PostgresOverview postgres,
            InfrastructureService.ClickHouseOverview clickhouse) {}

    @GetMapping
    public ResponseEntity<InfraOverviewResponse> overview() {
        return ResponseEntity.ok(new InfraOverviewResponse(
                infraService.getPostgresOverview(),
                infraService.getClickHouseOverview()));
    }

    @GetMapping("/postgres")
    public ResponseEntity<Map<String, Object>> postgres() {
        return ResponseEntity.ok(Map.of(
                "overview", infraService.getPostgresOverview(),
                "tenants", infraService.getPostgresTenantStats()));
    }

    @GetMapping("/postgres/{slug}")
    public ResponseEntity<List<InfrastructureService.TableStats>> postgresDetail(
            @PathVariable String slug) {
        return ResponseEntity.ok(infraService.getPostgresTenantDetail(slug));
    }

    @GetMapping("/clickhouse")
    public ResponseEntity<Map<String, Object>> clickhouse() {
        return ResponseEntity.ok(Map.of(
                "overview", infraService.getClickHouseOverview(),
                "tenants", infraService.getClickHouseTenantStats()));
    }

    @GetMapping("/clickhouse/{tenantId}")
    public ResponseEntity<List<InfrastructureService.ChTableStats>> clickhouseDetail(
            @PathVariable String tenantId) {
        return ResponseEntity.ok(infraService.getClickHouseTenantDetail(tenantId));
    }
}
