package com.nanri.aiimage.modules.ziniao.controller;

import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.modules.ziniao.model.cache.ZiniaoShopIndexRefreshCursorDto;
import com.nanri.aiimage.modules.ziniao.model.dto.ZiniaoOpenShopRequest;
import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoOpenShopVo;
import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoSessionVo;
import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoShopListVo;
import com.nanri.aiimage.modules.ziniao.model.vo.ZiniaoStaffListVo;
import com.nanri.aiimage.modules.ziniao.service.ZiniaoAuthService;
import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopIndexService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/ziniao")
@Tag(name = "紫鸟接入", description = "基于 API Key 直连紫鸟接口，获取 appToken、员工、店铺并生成打开店铺链接")
public class ZiniaoAuthController {

    private final ZiniaoAuthService ziniaoAuthService;
    private final ZiniaoShopIndexService ziniaoShopIndexService;

    @GetMapping("/session")
    @Operation(summary = "获取紫鸟会话状态", description = "自动获取或复用 appToken，并返回 companyId、当前员工 userId、脱敏 token 和当前店铺信息。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "获取成功", content = @Content(schema = @Schema(implementation = ZiniaoSessionVo.class)))
    })
    public ApiResponse<ZiniaoSessionVo> getSession(@RequestParam(required = false) String sessionId, @RequestParam(required = false) Long userId) {
        return ApiResponse.success(ziniaoAuthService.getSession(sessionId, userId));
    }

    @GetMapping("/staff")
    @Operation(summary = "获取紫鸟员工列表", description = "使用 API Key 直连紫鸟接口，查询当前 companyId 下的员工列表，供前端绑定 employee userId。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "获取成功", content = @Content(schema = @Schema(implementation = ZiniaoStaffListVo.class)))
    })
    public ApiResponse<ZiniaoStaffListVo> listStaff() {
        return ApiResponse.success(ziniaoAuthService.listStaff());
    }

    @GetMapping("/index-refresh")
    @Operation(summary = "获取紫鸟店铺索引刷新状态", description = "返回后台定时刷新紫鸟店铺索引的最近一次执行状态、时间和无效 userId 样本。")
    public ApiResponse<ZiniaoShopIndexRefreshCursorDto> getIndexRefreshStatus() {
        return ApiResponse.success(ziniaoShopIndexService.getRefreshCursor());
    }

    @GetMapping("/shops")
    @Operation(summary = "获取员工可见店铺列表", description = "先通过 API Key 获取 companyId，再按员工 userId 查询该员工有权限的店铺列表，并缓存到当前 session。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "获取成功", content = @Content(schema = @Schema(implementation = ZiniaoShopListVo.class)))
    })
    public ApiResponse<ZiniaoShopListVo> listShops(@RequestParam String sessionId, @RequestParam Long userId) {
        return ApiResponse.success(ziniaoAuthService.listShops(sessionId, userId));
    }

    @PostMapping("/shops/open")
    @Operation(summary = "生成店铺打开链接", description = "按指定员工 userId 和 shopId 获取员工登录 token，并返回可直接拉起紫鸟客户端的 openStoreUrl。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "打开链接生成成功", content = @Content(schema = @Schema(implementation = ZiniaoOpenShopVo.class)))
    })
    public ApiResponse<ZiniaoOpenShopVo> openShop(@Valid @RequestBody ZiniaoOpenShopRequest request) {
        return ApiResponse.success(ziniaoAuthService.openShop(request));
    }
}
