package org.dromara.mall.trade.controller.app;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.web.core.BaseController;
import org.dromara.mall.trade.domain.bo.MallAddressBo;
import org.dromara.mall.trade.domain.vo.MallAddressVo;
import org.dromara.mall.trade.service.MallAddressService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 收货地址（移动端）
 *
 * @author pengles
 */
@Tag(name = "移动端-收货地址", description = "收货地址接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/mall/address")
public class AppMallAddressController extends BaseController {

    private final MallAddressService mallAddressService;

    /**
     * 查询收货地址列表
     */
    @Operation(summary = "查询收货地址列表", description = "查询用户的收货地址列表")
    @GetMapping("/list")
    public R<List<MallAddressVo>> list() {
        Long userId = org.dromara.common.satoken.utils.LoginHelper.getUserId();
        List<MallAddressVo> list = mallAddressService.queryListByUserId(userId);
        return R.ok(list);
    }

    /**
     * 获取默认收货地址
     */
    @Operation(summary = "获取默认收货地址", description = "查询用户的默认收货地址")
    @GetMapping("/default")
    public R<MallAddressVo> getDefault() {
        Long userId = org.dromara.common.satoken.utils.LoginHelper.getUserId();
        MallAddressVo address = mallAddressService.queryDefaultByUserId(userId);
        return R.ok(address);
    }

    /**
     * 获取收货地址详细信息
     */
    @Operation(summary = "获取收货地址详情", description = "根据地址ID获取详细信息")
    @GetMapping("/{id}")
    public R<MallAddressVo> getInfo(@Parameter(description = "地址ID", required = true)
                                    @NotNull(message = "主键不能为空")
                                    @PathVariable Long id) {
        return R.ok(mallAddressService.queryById(id));
    }

    /**
     * 新增收货地址
     */
    @Operation(summary = "新增收货地址", description = "创建新的收货地址")
    @PostMapping
    public R<Void> add(@Parameter(description = "收货地址信息") @Validated @RequestBody MallAddressBo bo) {
        return toAjax(mallAddressService.insertByBo(bo));
    }

    /**
     * 修改收货地址
     */
    @Operation(summary = "修改收货地址", description = "更新收货地址信息")
    @PutMapping
    public R<Void> edit(@Parameter(description = "收货地址信息") @Validated @RequestBody MallAddressBo bo) {
        return toAjax(mallAddressService.updateByBo(bo));
    }

    /**
     * 删除收货地址
     */
    @Operation(summary = "删除收货地址", description = "删除收货地址")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@Parameter(description = "地址ID数组", required = true)
                          @NotNull(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(mallAddressService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 设置默认地址
     */
    @Operation(summary = "设置默认地址", description = "将指定地址设置为默认收货地址")
    @PutMapping("/default/{id}")
    public R<Void> setDefault(@Parameter(description = "地址ID", required = true)
                              @NotNull(message = "主键不能为空")
                              @PathVariable Long id) {
        return toAjax(mallAddressService.setDefault(id));
    }

}
