package com.seer.teach.mall.app.order.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.DecryptionAnnotation;
import com.seer.teach.common.annotation.EncryptionAnnotation;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.mall.request.AddressSaveReq;
import com.seer.teach.mall.request.AddressSetDefaultReq;
import com.seer.teach.mall.request.AddressUpdateReq;
import com.seer.teach.mall.response.AddressResp;
import com.seer.teach.mall.service.IAddressService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  收货地址管理前端控制器
 * </p>
 *
 * @author Captain
 * @since 2025-06-10
 */
@LogPrint
@DecryptionAnnotation
@EncryptionAnnotation
@RestController
@RequestMapping("/app/user/address")
@Tag(name = "APP - 收货地址管理")
@Validated
@RequiredArgsConstructor
@SaCheckLogin(type = StpUtil.TYPE)
public class AppAddressController {

    private final IAddressService addressService;

    @GetMapping("/list")
    @SaCheckPermission("app:address:list")
    @Operation(summary = "查询当前登录用户的所有收货地址")
    public ResultBean<List<AddressResp>> list(){
        List<AddressResp> results = addressService.listAddress();
        return ResultBean.success(results);
    }

    @PostMapping("/save")
    @SaCheckPermission("app:address:create")
    @Operation(summary = "新增收货地址")
    public ResultBean<AddressResp> saveAddress(@RequestBody @Validated AddressSaveReq params){
        return ResultBean.success(addressService.saveAddress(params));
    }

    @GetMapping("/{id}")
    @SaCheckPermission("app:address:get")
    @Operation(summary = "根据Id查询地址信息")
    public ResultBean<AddressResp> getAddressById(@PathVariable Integer id){
        AddressResp result = addressService.getAddressById(id);
        Integer userId = StpUtil.getLoginIdAsInt();
        if(!result.getUserId().equals(userId)){
            return ResultBean.error(ResultCodeEnum.NO_TRACKING_SHIPPING_ADDRESS);
        }
        return ResultBean.success(addressService.getAddressById(id));
    }

    @PostMapping("/update")
    @SaCheckPermission("app:address:update")
    @Operation(summary = "修改收货地址")
    public ResultBean<Boolean> updateAddress(@RequestBody @Validated AddressUpdateReq params){
        return ResultBean.success(addressService.updateAddress(params));
    }

    @DeleteMapping("/delete/{id}")
    @SaCheckPermission("app:address:delete")
    @Operation(summary = "根据Id删除收货地址")
    public ResultBean<Boolean> deleteAddress(@PathVariable Integer id){
        return ResultBean.success(addressService.deleteAddress(id));
    }

    @PostMapping("/setDefault")
    @SaCheckPermission("app:address:setDefault")
    @Operation(summary = "设置默认收货地址")
    public ResultBean setDefaultAddress(@RequestBody @Validated AddressSetDefaultReq params){
        return ResultBean.success(addressService.setDefaultAddress(params));
    }

    @GetMapping("/getDefault")
    @SaCheckPermission("app:address:getDefault")
    @Operation(summary = "查询默认收货地址")
    public ResultBean<AddressResp> getDefaultAddress(){
        return ResultBean.success(addressService.getDefaultAddress());
    }

}
