package shop.alien.store.controller;

import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.geo.Point;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import shop.alien.entity.result.R;
import shop.alien.entity.store.dto.NearMeDto;
import shop.alien.store.service.NearMeService;

import java.util.List;

/**
 * 我附近的商家
 *
 * @author andy
 * @since 2025-04-22
 */
@Slf4j
@Api(tags = {"二期-我附近的"})
@ApiSort(19)
@CrossOrigin
@RestController
@RequestMapping("/near/me")
@RequiredArgsConstructor
public class NearMeController {

    private final NearMeService nearMeService;

    @ApiOperation("我附近的商家")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lon", value = "经度", dataType = "double", paramType = "query", required = true),
            @ApiImplicitParam(name = "lat", value = "纬度", dataType = "double", paramType = "query", required = true),
            @ApiImplicitParam(name = "distance", value = "半径/km", dataType = "double", paramType = "query", required = true),
            @ApiImplicitParam(name = "flag", value = "true:id,false:name", dataType = "Boolean", paramType = "query", required = true)
    })
    @GetMapping("/mer")
    public R<List<NearMeDto>> findMerchant(Double lon, Double lat, Double distance, Boolean flag) {
        log.info("NearMeController.findMerchant, lat:{}, lon:{}, distance:{}, flag:{}", lat, lon, distance, flag);
        List<NearMeDto> res = nearMeService.nearbyMerchants(lat, lon, distance, flag);
        return R.data(res);
    }

    @ApiOperation("新增商家")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lon", value = "经度", dataType = "double", paramType = "query", required = true),
            @ApiImplicitParam(name = "lat", value = "纬度", dataType = "double", paramType = "query", required = true),
            @ApiImplicitParam(name = "content", value = "内容", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "flag", value = "true:id,false:name", dataType = "Boolean", paramType = "query", required = true)
    })
    @GetMapping("/inMer")
    public R inGeolocation(Double lon, Double lat, String content, Boolean flag) {
        log.info("NearMeController.inGeolocation, lat:{}, lon:{}, content:{}, flag:{}", lat, lon, content, flag);
        try {
            nearMeService.inGeolocation(new Point(lon, lat), content, flag);
            return R.success("成功");
        } catch (Exception e) {
            return R.fail("失败");
        }
    }

    @ApiOperation("删除商家")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "内容", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "flag", value = "true:id,false:name", dataType = "Boolean", paramType = "query", required = true)
    })
    @GetMapping("/delMer")
    public R inGeolocation(Boolean flag, String content) {
        log.info("NearMeController.inGeolocation, content:{}, type:{}", content, flag);
        try {
            nearMeService.removeGeolocation(flag, content);
            return R.success("成功");
        } catch (Exception e) {
            return R.fail("失败");
        }
    }
}
