package com.example.demo.controller;

import com.example.demo.model.ApiResult;
import com.example.demo.service.IntegrationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 接口集成控制器。
 * <p>提供第三方系统认证、数据推送、数据拉取、同步状态查询 RESTful API。</p>
 */
@Tag(name = "接口集成", description = "第三方系统认证、数据推送、数据拉取、同步状态查询")
@RestController
@RequestMapping("/api/v1")
public class IntegrationController {

    private final IntegrationService integrationService;

    public IntegrationController(IntegrationService integrationService) {
        this.integrationService = integrationService;
    }

    /**
     * 第三方系统认证。
     *
     * @requirement(name="预留标准化接口", id="SRS-F-06-001")
     */
    @Operation(summary = "第三方认证")
    @PostMapping("/auth")
    public ApiResult<Map<String, Object>> auth(
            @Parameter(description = "API Key") @RequestParam String apiKey,
            @Parameter(description = "Secret") @RequestParam String secret) {
        return ApiResult.success(integrationService.auth(apiKey, secret));
    }

    /**
     * 数据推送到第三方。
     *
     * @requirement(name="支持与第三方系统数据对接、数据同步", id="SRS-F-06-002")
     */
    @Operation(summary = "数据推送")
    @PostMapping("/push")
    public ApiResult<Map<String, Object>> push(@RequestBody Map<String, Object> data) {
        return ApiResult.success(integrationService.pushData(data));
    }

    /**
     * 从第三方拉取数据。
     *
     * @requirement(name="支持与第三方系统数据对接、数据同步", id="SRS-F-06-002")
     */
    @Operation(summary = "数据拉取")
    @GetMapping("/pull")
    public ApiResult<Map<String, Object>> pull(
            @Parameter(description = "同步类型") @RequestParam String syncType,
            @Parameter(description = "上次同步时间") @RequestParam(required = false) String lastSyncTime) {
        return ApiResult.success(integrationService.pullData(syncType, lastSyncTime));
    }

    /**
     * 查询同步状态。
     *
     * @requirement(name="支持与第三方系统数据对接、数据同步", id="SRS-F-06-002")
     */
    @Operation(summary = "查询同步状态")
    @GetMapping("/sync/status")
    public ApiResult<Map<String, Object>> syncStatus(
            @Parameter(description = "同步记录ID") @RequestParam String syncId) {
        return ApiResult.success(integrationService.syncStatus(syncId));
    }
}
