package com.czg.controller.admin;

import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.http.server.HttpServerResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.czg.account.dto.SysLoginDTO;
import com.czg.account.entity.ShopInfo;
import com.czg.account.service.*;
import com.czg.account.vo.LoginVO;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.annotation.SaStaffCheckPermission;
import com.czg.config.RabbitPublisher;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 商户登录相关
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/auth")
public class AuthorizationController {
    @Resource
    private AuthorizationService authorizationService;
    @Resource
    private PermissionService permissionService;
    @Resource
    private ShopInfoService shopInfoService;
    @Resource
    private ShopStaffService shopStaffService;


    /**
     * 验证码获取
     *
     * @return 验证码信息
     */
    @GetMapping("captcha")
    public CzgResult<?> captcha() {
        return CzgResult.success(authorizationService.getCaptcha());
    }

    /**
     * 商户登录
     *
     * @param loginDTO 登录参数
     * @return token信息
     */
    @PostMapping("login")
    public CzgResult<LoginVO> login(@Validated @RequestBody SysLoginDTO loginDTO, @RequestHeader(defaultValue = "WEB") String platformType) {
        return CzgResult.success(authorizationService.login(loginDTO, platformType));
    }

    /**
     * 退出登录
     */
    @PostMapping("logout")
    public CzgResult<?> logout() {
        StpKit.USER.logout();
        return CzgResult.success();
    }

    @GetMapping("/permission")
    public CzgResult<?> getPermission() {
        return CzgResult.success();
    }

    @Resource
    RabbitPublisher rabbitPublisher;
    @Resource
    ShopTableService shopTableService;

    @GetMapping("test")
    public CzgResult<?> login(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        shopTableService.createQrCode(1L, 1, response, request);


//        rabbitPublisher.sendOrderPrintMsg("552");
//        printMqListener.orderPrint("1");
//        return CzgResult.success(Map.of("token", StpKit.USER.getShopId()));
        return CzgResult.success(shopInfoService.getById(1));
    }

    /**
     * 核销获取 信息使用
     * 通过token 获取登录信息
     */
    @GetMapping("/userInfo")
    public CzgResult<Map<String, Object>> getUserInfo() {
        String account = StpKit.USER.getAccount();
        ShopInfo shopInfo = shopInfoService.queryChain()
                .select(ShopInfo::getId, ShopInfo::getShopName)
                .eq(ShopInfo::getId, StpKit.USER.getShopId()).one();
        if (account.contains("@")) {
            account = account.split("@")[1];
        }
        Map<String, Object> map = Map.of("shopId", shopInfo.getId(), "shopName", shopInfo.getShopName(), "account", account);
        return CzgResult.success(map);
    }
}
