package com.jeesharp.business.member.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.jeesharp.business.modules.finance.api.MemberInvoiceTitleApi;
import com.jeesharp.business.modules.finance.invoice.dto.MemberInvoiceTitleSaveDTO;
import com.jeesharp.business.modules.finance.invoice.vo.MemberInvoiceTitleInfoVO;
import com.jeesharp.framework.memberauth.utils.MemberAuthUtils;
import com.jeesharp.framework.memberauth.web.BaseMobileController;
import com.jeesharp.framework.pojo.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 移动端发票抬头管理接口。
 *
 * @author jeesharp
 * @since 2026-06-12
 */
@RestController
@RequestMapping("${jeesharp.web.mobilePath}/member/invoiceTitle")
@Tag(name = "移动端-发票抬头")
@AllArgsConstructor
public class InvoiceTitleMobileController extends BaseMobileController {

    private final MemberInvoiceTitleApi memberInvoiceTitleApi;

    @Operation(summary = "发票抬头列表")
    @GetMapping("/list")
    @SaCheckLogin
    public AjaxResult<List<MemberInvoiceTitleInfoVO>> list() {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberInvoiceTitleApi.listByMember(memberId));
    }

    @Operation(summary = "新增发票抬头")
    @PostMapping("/create")
    @SaCheckLogin
    public AjaxResult<Map<String, String>> create(@RequestBody @Valid MemberInvoiceTitleSaveDTO dto) {
        String memberId = MemberAuthUtils.getLoginUserId();
        String newId = memberInvoiceTitleApi.create(memberId, dto);
        return AjaxResult.ok("保存成功", Map.of("id", newId == null ? "" : newId));
    }

    @Operation(summary = "编辑发票抬头")
    @PutMapping("/update/{id}")
    @SaCheckLogin
    public AjaxResult<Void> update(@PathVariable("id") String id, @RequestBody @Valid MemberInvoiceTitleSaveDTO dto) {
        String memberId = MemberAuthUtils.getLoginUserId();
        dto.setId(id);
        memberInvoiceTitleApi.update(memberId, dto);
        return AjaxResult.ok("保存成功");
    }

    @Operation(summary = "删除发票抬头")
    @DeleteMapping("/delete/{id}")
    @SaCheckLogin
    public AjaxResult<Void> delete(@PathVariable("id") String id) {
        String memberId = MemberAuthUtils.getLoginUserId();
        memberInvoiceTitleApi.delete(memberId, id);
        return AjaxResult.ok("删除成功");
    }

    @Operation(summary = "设为默认抬头")
    @PostMapping("/default/{id}")
    @SaCheckLogin
    public AjaxResult<Void> setDefault(@PathVariable("id") String id) {
        String memberId = MemberAuthUtils.getLoginUserId();
        memberInvoiceTitleApi.setDefault(memberId, id);
        return AjaxResult.ok("设置成功");
    }
}
