package com.zsElectric.boot.business.controller.applet;

import com.zsElectric.boot.business.model.vo.AppletConnectorListVO;
import com.zsElectric.boot.business.model.vo.AppletStationDetailVO;
import com.zsElectric.boot.business.model.vo.AppletStationPriceListVO;
import com.zsElectric.boot.business.model.vo.applet.AppChargingCostVO;
import com.zsElectric.boot.business.model.vo.applet.AppStationSearchVO;
import com.zsElectric.boot.business.model.vo.applet.AppletConnectorDetailVO;
import com.zsElectric.boot.business.service.AppletHomeService;
import com.zsElectric.boot.common.annotation.ApiRateLimit;
import com.zsElectric.boot.core.web.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
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.math.BigDecimal;
import java.util.List;

@Tag(name = "充电相关接口")
@RestController
@RequestMapping("/applet/v1/station")
@RequiredArgsConstructor
public class AppletStationController {

    private final AppletHomeService appletHomeService;

    /**
     * 获取电站详情
     *
     * @param stationId 站点ID
     * @param longitude 经度（用于计算距离）
     * @param latitude 纬度（用于计算距离）
     * @return 电站详情
     */
    @Operation(summary = "获取电站详情")
    @GetMapping("/detail")
    public Result<AppletStationDetailVO> getStationDetail(
            @RequestParam Long stationId,
            @RequestParam(required = false) BigDecimal longitude,
            @RequestParam(required = false) BigDecimal latitude) {
        return Result.success(appletHomeService.getStationDetail(stationId, longitude, latitude));
    }

    /**
     * 获取充电终端列表
     *
     * @param stationId 站点ID
     * @return 充电终端列表
     */
    @Operation(summary = "获取充电终端列表")
    @GetMapping("/connectors")
    public Result<AppletConnectorListVO> getConnectorList(@RequestParam Long stationId) {
        return Result.success(appletHomeService.getConnectorList(stationId));
    }

    /**
     * 获取电站价格列表
     *
     * @param stationId 站点ID
     * @return 电站价格列表
     */
    @Operation(summary = "获取电站价格列表")
    @GetMapping("/prices")
    public Result<AppletStationPriceListVO> getStationPriceList(@RequestParam Long stationId) {
        return Result.success(appletHomeService.getStationPriceList(stationId));
    }

    /**
     * 获取当前充电订单实时费用
     * 根据当前登录用户查询正在充电中的订单
     *
     * @return 实时充电费用信息，如果没有正在充电的订单则返回null
     */
    @Operation(summary = "获取当前充电订单实时费用")
    @GetMapping("/charging-cost")
    @ApiRateLimit(prefix = "applet:charging_cost", limitType = ApiRateLimit.LimitType.IP, count = 2000, time = 60, message = "获取充电费用请求过于频繁，请稍后再试")
    public Result<AppChargingCostVO> getCurrentChargingCost() {
        return Result.success(appletHomeService.getCurrentChargingCost());
    }

    /**
     * 搜索充电站
     *
     * @param keyword 搜索关键词（站点名称或地址）
     * @param longitude 经度（用于计算距离，可选）
     * @param latitude 纬度（用于计算距离，可选）
     * @return 搜索结果列表
     */
    @Operation(summary = "搜索充电站")
    @GetMapping("/search")
    public Result<List<AppStationSearchVO>> searchStations(
            @RequestParam String keyword,
            @RequestParam(required = false) BigDecimal longitude,
            @RequestParam(required = false) BigDecimal latitude) {
        return Result.success(appletHomeService.searchStations(keyword, longitude, latitude));
    }

    /**
     * 获取充电设备接口详情
     *
     * @return 设备接口详情
     */
    @Operation(summary = "获取充电设备接口详情")
    @GetMapping("/connector/detail")
    public Result<AppletConnectorDetailVO> getConnectorDetail(@RequestParam String connectorCode) {
        return Result.success(appletHomeService.getConnectorDetail(connectorCode));
    }
}