package com.jeesharp.business.modules.marketing.memberTenant.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jeesharp.business.modules.marketing.memberTenant.application.MemberTenantApplicationService;
import com.jeesharp.business.modules.marketing.memberTenant.dto.MemberTenantAssignRoleDTO;
import com.jeesharp.business.modules.marketing.memberTenant.dto.MemberTenantBindDTO;
import com.jeesharp.business.modules.marketing.memberTenant.entity.MemberTenant;
import com.jeesharp.business.modules.marketing.memberTenant.vo.MemberTenantExportVO;
import com.jeesharp.business.modules.marketing.memberTenant.vo.MemberTenantInfoVO;
import com.jeesharp.business.modules.marketing.memberTenant.vo.MemberTenantListVO;
import com.jeesharp.business.modules.marketing.memberTenant.vo.MemberTenantQueryVO;
import com.jeesharp.framework.office.excel.util.ExcelExportUtil;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * 店铺会员管理 REST 接口，提供分页列表、详情与导出。
 *
 * @author jeesharp
 * @since 2026-06-25
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/marketing/memberTenant/v1")
@Tag(name = "店铺会员", description = "会员-店铺租户关系管理")
@AllArgsConstructor
public class MemberTenantController extends BaseController {

    private final MemberTenantApplicationService memberTenantApplicationService;

    @ApiOperationSupport(order = 1)
    @Operation(summary = "分页列表")
    @PreAuthorize("@ss.hasPermission('marketing:memberTenant:list')")
    @PostMapping("list")
    public AjaxResult<PageRespVO<MemberTenantListVO>> list(@RequestBody MemberTenantQueryVO query) {
        Page<MemberTenant> page = new Page<>(query);
        return AjaxResult.okData(memberTenantApplicationService.findPage(page, query));
    }

    @ApiOperationSupport(order = 2)
    @Operation(summary = "详情")
    @PreAuthorize("@ss.hasPermission('marketing:memberTenant:info') or @ss.hasPermission('marketing:memberTenant:list')")
    @GetMapping("info")
    public AjaxResult<MemberTenantInfoVO> info(@RequestParam("id") String id) {
        return AjaxResult.okData(memberTenantApplicationService.get(id));
    }

    @ApiOperationSupport(order = 5)
    @Operation(summary = "绑定会员到店铺")
    @PreAuthorize("@ss.hasPermission('marketing:memberTenant:create')")
    @PostMapping("create")
    public AjaxResult<MemberTenantInfoVO> create(@RequestBody @Valid MemberTenantBindDTO dto) {
        return AjaxResult.okData(memberTenantApplicationService.bindMember(dto));
    }

    @ApiOperationSupport(order = 4)
    @Operation(summary = "设置店铺角色")
    @PreAuthorize("@ss.hasPermission('marketing:memberTenant:assignRole')")
    @PostMapping("assignRole")
    public AjaxResult<Void> assignRole(@RequestBody @Valid MemberTenantAssignRoleDTO dto) {
        memberTenantApplicationService.assignRole(dto);
        return AjaxResult.ok("设置成功");
    }

    @ApiOperationSupport(order = 3)
    @Operation(summary = "导出")
    @PreAuthorize("@ss.hasPermission('marketing:memberTenant:export')")
    @PostMapping("export")
    public void export(@RequestBody MemberTenantQueryVO query, HttpServletResponse response) {
        ExcelExportUtil.export(response, MemberTenantExportVO.class,
                memberTenantApplicationService.findList(query), "店铺会员数据");
    }
}
