package com.jeesharp.business.member.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.jeesharp.business.modules.member.application.MemberVisitorApplicationService;
import com.jeesharp.business.modules.member.dto.MemberVisitorBodyDTO;
import com.jeesharp.business.modules.member.vo.MemberVisitorItemVO;
import com.jeesharp.framework.memberauth.utils.MemberAuthUtils;
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.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.List;

/**
 * 小程序常用游客接口：列表、创建、更新、删除；数据持久化至 {@code app_member_visitor}。
 *
 * @author jeesharp
 * @since 2026-05-12
 */
@RestController
@RequestMapping("${jeesharp.web.mobilePath}/member")
@Tag(name = "移动端-常用游客")
@AllArgsConstructor
public class VisitorMobileController {

    private final MemberVisitorApplicationService memberVisitorApplicationService;

    @Operation(summary = "常用游客列表")
    @GetMapping("/visitors")
    @SaCheckLogin
    public AjaxResult<List<MemberVisitorItemVO>> list() {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberVisitorApplicationService.listForMobile(memberId));
    }

    @Operation(summary = "新增常用游客")
    @PostMapping("/visitor/create")
    @SaCheckLogin
    public AjaxResult<MemberVisitorItemVO> create(@RequestBody @Valid MemberVisitorBodyDTO dto) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberVisitorApplicationService.create(memberId, dto));
    }

    @Operation(summary = "编辑常用游客")
    @PutMapping("/visitor/update/{id}")
    @SaCheckLogin
    public AjaxResult<MemberVisitorItemVO> update(
            @PathVariable("id") String id,
            @RequestBody @Valid MemberVisitorBodyDTO dto) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(memberVisitorApplicationService.update(memberId, id, dto));
    }

    @Operation(summary = "删除常用游客")
    @DeleteMapping("/visitor/delete/{id}")
    @SaCheckLogin
    public AjaxResult<Void> delete(@PathVariable("id") String id) {
        String memberId = MemberAuthUtils.getLoginUserId();
        memberVisitorApplicationService.delete(memberId, id);
        return AjaxResult.ok("删除成功");
    }
}
