package cn.skcks.matrix.v2.api.locatioin.record;

import cn.skcks.matrix.v2.annotation.web.JsonMapping;
import cn.skcks.matrix.v2.annotation.web.auth.Auth;
import cn.skcks.matrix.v2.annotation.web.methods.PostJson;
import cn.skcks.matrix.v2.auth.claims.Claim;
import cn.skcks.matrix.v2.config.swagger.SwaggerConfig;
import cn.skcks.matrix.v2.model.jwt.dto.Claims;
import cn.skcks.matrix.v2.model.location.record.convert.LocationRecordConvertor;
import cn.skcks.matrix.v2.model.location.record.dto.LocationRecordDto;
import cn.skcks.matrix.v2.model.location.record.dto.LocationRecordParams;
import cn.skcks.matrix.v2.model.location.record.dto.PageLocationRecordParams;
import cn.skcks.matrix.v2.model.location.record.vo.LocationRecordVo;
import cn.skcks.matrix.v2.model.services.ServiceResult;
import cn.skcks.matrix.v2.orm.mybatis.dynamic.model.LocationRecord;
import cn.skcks.matrix.v2.services.jwt.JwtService;
import cn.skcks.matrix.v2.services.location.record.LocationRecordService;
import cn.skcks.matrix.v2.utils.json.JsonResponse;
import cn.skcks.matrix.v2.utils.page.PageWrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;

@Auth
@Tag(name = "定位记录")
@RestController
@JsonMapping("/location/record")
@RequiredArgsConstructor
@Slf4j
public class LocationRecordController {
    private final JwtService jwtService;
    private final LocationRecordService locationRecordService;

    @Bean
    public GroupedOpenApi docs() {
        return SwaggerConfig.api("Location Record", "/location/record");
    }

    @PostJson("/post")
    @Operation(summary = "上报定位记录")
    public JsonResponse<Void> postLocationRecord(@Claim Claims claims, @RequestBody LocationRecordDto dto) throws Exception {
        locationRecordService.postLocationRecord(dto,claims);
        return JsonResponse.success(null);
    }


    @PostJson("/list")
    @Operation(summary = "查询记录")
    public JsonResponse<Collection<LocationRecordVo>> getLocationRecord(@Claim Claims claims, @RequestBody LocationRecordParams params){
        ServiceResult<Collection<LocationRecord>> result = locationRecordService.getLocationRecord(params, claims);
        return JsonResponse.success(LocationRecordConvertor.INSTANCE.daoToVo(result.getResult()));
    }

    @PostJson("/page")
    @Operation(summary = "分页查询记录")
    public JsonResponse<PageWrapper<LocationRecordVo>> getPageLocationRecord(@Claim Claims claims, @RequestBody PageLocationRecordParams params){
        PageInfo<LocationRecordVo> pageInfo =
                LocationRecordConvertor.INSTANCE.daoToVo(locationRecordService.getPageLocationRecord(params, claims).getResult());

        return JsonResponse.success(PageWrapper.of(pageInfo));
    }
}
