package com.gcsc.connection.service.controller;

import com.gcsc.connection.common.dto.ApiResponse;
import com.gcsc.connection.service.dto.ApiDetailResponse;
import com.gcsc.connection.service.dto.ApiParamResponse;
import com.gcsc.connection.service.dto.ApiSpecResponse;
import com.gcsc.connection.service.dto.CreateServiceApiRequest;
import com.gcsc.connection.service.dto.CreateServiceRequest;
import com.gcsc.connection.service.dto.SaveApiParamRequest;
import com.gcsc.connection.service.dto.SaveApiSpecRequest;
import com.gcsc.connection.service.dto.ServiceApiResponse;
import com.gcsc.connection.service.dto.ServiceResponse;
import com.gcsc.connection.service.dto.UpdateServiceApiRequest;
import com.gcsc.connection.service.dto.UpdateServiceRequest;
import com.gcsc.connection.service.service.ServiceManagementService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 서비스 관리 API
 */
@RestController
@RequestMapping("/api/services")
@RequiredArgsConstructor
public class ServiceController {

    private final ServiceManagementService serviceManagementService;

    /**
     * 전체 서비스 목록 조회
     */
    @GetMapping
    public ResponseEntity<ApiResponse<List<ServiceResponse>>> getServices() {
        List<ServiceResponse> services = serviceManagementService.getServices();
        return ResponseEntity.ok(ApiResponse.ok(services));
    }

    /**
     * 서비스 단건 조회
     */
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<ServiceResponse>> getService(@PathVariable Long id) {
        ServiceResponse service = serviceManagementService.getService(id);
        return ResponseEntity.ok(ApiResponse.ok(service));
    }

    /**
     * 서비스 생성
     */
    @PostMapping
    public ResponseEntity<ApiResponse<ServiceResponse>> createService(
            @RequestBody @Valid CreateServiceRequest request) {
        ServiceResponse service = serviceManagementService.createService(request);
        return ResponseEntity.ok(ApiResponse.ok(service));
    }

    /**
     * 서비스 수정
     */
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<ServiceResponse>> updateService(
            @PathVariable Long id,
            @RequestBody @Valid UpdateServiceRequest request) {
        ServiceResponse service = serviceManagementService.updateService(id, request);
        return ResponseEntity.ok(ApiResponse.ok(service));
    }

    /**
     * 서비스 삭제 (비활성화)
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteService(@PathVariable Long id) {
        serviceManagementService.deleteService(id);
        return ResponseEntity.ok(ApiResponse.ok(null));
    }

    /**
     * 서비스 API 목록 조회
     */
    @GetMapping("/{id}/apis")
    public ResponseEntity<ApiResponse<List<ServiceApiResponse>>> getServiceApis(
            @PathVariable Long id) {
        List<ServiceApiResponse> apis = serviceManagementService.getServiceApis(id);
        return ResponseEntity.ok(ApiResponse.ok(apis));
    }

    /**
     * 서비스 API 생성
     */
    @PostMapping("/{id}/apis")
    public ResponseEntity<ApiResponse<ServiceApiResponse>> createServiceApi(
            @PathVariable Long id,
            @RequestBody @Valid CreateServiceApiRequest request) {
        ServiceApiResponse api = serviceManagementService.createServiceApi(id, request);
        return ResponseEntity.ok(ApiResponse.ok(api));
    }

    /**
     * 서비스 API 수정
     */
    @PutMapping("/{serviceId}/apis/{apiId}")
    public ResponseEntity<ApiResponse<ServiceApiResponse>> updateServiceApi(
            @PathVariable Long serviceId, @PathVariable Long apiId,
            @RequestBody @Valid UpdateServiceApiRequest request) {
        ServiceApiResponse api = serviceManagementService.updateServiceApi(serviceId, apiId, request);
        return ResponseEntity.ok(ApiResponse.ok(api));
    }

    /**
     * 서비스 API 삭제 (비활성화)
     */
    @DeleteMapping("/{serviceId}/apis/{apiId}")
    public ResponseEntity<ApiResponse<Void>> deleteServiceApi(
            @PathVariable Long serviceId, @PathVariable Long apiId) {
        serviceManagementService.deleteServiceApi(serviceId, apiId);
        return ResponseEntity.ok(ApiResponse.ok(null));
    }

    /**
     * API 상세 명세 조회 (스펙 + 파라미터)
     */
    @GetMapping("/{serviceId}/apis/{apiId}/spec")
    public ResponseEntity<ApiResponse<ApiDetailResponse>> getApiDetail(
            @PathVariable Long serviceId, @PathVariable Long apiId) {
        ApiDetailResponse detail = serviceManagementService.getApiDetail(serviceId, apiId);
        return ResponseEntity.ok(ApiResponse.ok(detail));
    }

    /**
     * API 명세 저장 (upsert)
     */
    @PutMapping("/{serviceId}/apis/{apiId}/spec")
    public ResponseEntity<ApiResponse<ApiSpecResponse>> saveApiSpec(
            @PathVariable Long serviceId, @PathVariable Long apiId,
            @RequestBody @Valid SaveApiSpecRequest request) {
        ApiSpecResponse spec = serviceManagementService.saveApiSpec(serviceId, apiId, request);
        return ResponseEntity.ok(ApiResponse.ok(spec));
    }

    /**
     * API 파라미터 전체 교체
     */
    @PutMapping("/{serviceId}/apis/{apiId}/params")
    public ResponseEntity<ApiResponse<List<ApiParamResponse>>> saveApiParams(
            @PathVariable Long serviceId, @PathVariable Long apiId,
            @RequestBody List<SaveApiParamRequest> requests) {
        List<ApiParamResponse> params = serviceManagementService.saveApiParams(serviceId, apiId, requests);
        return ResponseEntity.ok(ApiResponse.ok(params));
    }
}
