package com.ruoyi.web.modules.account.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.modules.account.domain.BizMemberAddress;
import com.ruoyi.web.modules.account.dto.MemberChangePasswordDTO;
import com.ruoyi.web.modules.account.dto.MemberLoginDTO;
import com.ruoyi.web.modules.account.dto.MemberProfileUpdateDTO;
import com.ruoyi.web.modules.account.dto.MemberRegisterDTO;
import com.ruoyi.web.modules.account.dto.MemberSmsSendDTO;
import com.ruoyi.web.modules.account.dto.MemberWxBindDTO;
import com.ruoyi.web.modules.account.service.IMemberAddressService;
import com.ruoyi.web.modules.account.service.IMemberAppService;
import com.ruoyi.web.modules.account.support.MemberContext;

/**
 * C 端会员接口
 */
@RestController
@RequestMapping("/api/member")
public class MemberAppController extends BaseController
{
    @Autowired
    private IMemberAppService memberAppService;

    @Autowired
    private IMemberAddressService memberAddressService;

    @Anonymous
    @PostMapping("/sms/send")
    public AjaxResult sendSms(@RequestBody MemberSmsSendDTO dto)
    {
        memberAppService.sendSms(dto.getMobile());
        return success();
    }

    @Anonymous
    @PostMapping("/register")
    public AjaxResult register(@RequestBody MemberRegisterDTO dto)
    {
        Long memberId = memberAppService.register(dto);
        return success(memberId);
    }

    @Anonymous
    @PostMapping("/login")
    public AjaxResult login(@RequestBody MemberLoginDTO dto)
    {
        Map<String, Object> loginResult = memberAppService.login(dto);
        AjaxResult ajax = AjaxResult.success();
        ajax.put(Constants.TOKEN, loginResult.get("token"));
        ajax.put("memberId", loginResult.get("memberId"));
        return ajax;
    }

    @GetMapping("/profile")
    public AjaxResult profile()
    {
        return success(memberAppService.getProfile(requireMemberId()));
    }

    @PutMapping("/profile")
    public AjaxResult updateProfile(@RequestBody MemberProfileUpdateDTO dto)
    {
        memberAppService.updateProfile(requireMemberId(), dto);
        return success();
    }

    @PutMapping("/password")
    public AjaxResult changePassword(@RequestBody MemberChangePasswordDTO dto)
    {
        memberAppService.changePassword(requireMemberId(), dto);
        return success();
    }

    @PostMapping("/wx/bind")
    public AjaxResult bindWx(@RequestBody MemberWxBindDTO dto)
    {
        memberAppService.bindWxOpenid(requireMemberId(), dto.getCode());
        return success();
    }

    @GetMapping("/address/list")
    public AjaxResult addressList()
    {
        return success(memberAddressService.listByMember(requireMemberId()));
    }

    @PostMapping("/address")
    public AjaxResult addAddress(@RequestBody BizMemberAddress address)
    {
        return success(memberAddressService.addAddress(requireMemberId(), address));
    }

    @PutMapping("/address")
    public AjaxResult updateAddress(@RequestBody BizMemberAddress address)
    {
        memberAddressService.updateAddress(requireMemberId(), address);
        return success();
    }

    @DeleteMapping("/address/{addressId}")
    public AjaxResult deleteAddress(@PathVariable("addressId") Long addressId)
    {
        memberAddressService.deleteAddress(requireMemberId(), addressId);
        return success();
    }

    @PutMapping("/address/{addressId}/default")
    public AjaxResult setDefault(@PathVariable("addressId") Long addressId)
    {
        memberAddressService.setDefaultAddress(requireMemberId(), addressId);
        return success();
    }

    private Long requireMemberId()
    {
        Long memberId = MemberContext.getMemberId();
        if (memberId == null)
        {
            throw new com.ruoyi.common.exception.ServiceException("请先登录", 401);
        }
        return memberId;
    }
}
