package com.snack.server.module.address.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.snack.server.common.Result;
import com.snack.server.module.address.dto.req.AddressSaveReq;
import com.snack.server.module.address.service.AddressService;
import com.snack.server.module.address.vo.AddressVO;
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.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收货地址（用户端）
 */
@Tag(name = "收货地址")
@RestController
@RequestMapping("/api/address")
@RequiredArgsConstructor
public class AddressController {

    private final AddressService addressService;

    @Operation(summary = "获取当前用户的所有地址")
    @SaCheckLogin
    @GetMapping
    public Result<List<AddressVO>> list() {
        return Result.ok(addressService.listByCurrentUser());
    }

    @Operation(summary = "获取默认地址")
    @SaCheckLogin
    @GetMapping("/default")
    public Result<AddressVO> getDefault() {
        return Result.ok(addressService.getDefault());
    }

    @Operation(summary = "获取地址详情")
    @SaCheckLogin
    @GetMapping("/{id}")
    public Result<AddressVO> detail(@Parameter(description = "地址 ID") @PathVariable Long id) {
        return Result.ok(addressService.getById(id));
    }

    @Operation(summary = "新增地址")
    @SaCheckLogin
    @PostMapping
    public Result<Long> create(@Valid @RequestBody AddressSaveReq req) {
        return Result.ok(addressService.create(req));
    }

    @Operation(summary = "更新地址")
    @SaCheckLogin
    @PutMapping
    public Result<Void> update(@Valid @RequestBody AddressSaveReq req) {
        addressService.update(req);
        return Result.ok();
    }

    @Operation(summary = "删除地址")
    @SaCheckLogin
    @DeleteMapping("/{id}")
    public Result<Void> delete(@Parameter(description = "地址 ID") @PathVariable Long id) {
        addressService.delete(id);
        return Result.ok();
    }

    @Operation(summary = "设为默认地址")
    @SaCheckLogin
    @PutMapping("/{id}/default")
    public Result<Void> setDefault(@Parameter(description = "地址 ID") @PathVariable Long id) {
        addressService.setDefault(id);
        return Result.ok();
    }
}
