package com.inspect.ivs.controller;

import com.inspect.base.core.domain.Response;
import com.inspect.base.core.utils.StringUtils;
import com.inspect.ivs.constant.IvsConst;
import com.inspect.ivs.service.IvsCommonService;
import com.inspect.ivs.service.IvsLoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping({"/session"})
public class IvsSessionController {
    private static final Logger log = LoggerFactory.getLogger(IvsSessionController.class);
    private final IvsCommonService ivsCommonService;
    private final IvsLoginService ivsLoginService;

    //qinyl
    @Value("${spring.profiles.active}")
    private String activeProfile;

    public IvsSessionController(IvsCommonService ivsCommonService, IvsLoginService ivsLoginService) {
        this.ivsCommonService = ivsCommonService;
        this.ivsLoginService = ivsLoginService;
    }

    @GetMapping({"fresh"})
    public Response fresh() {
        if (!"dev".equals(activeProfile)) {
            String label = UUID.randomUUID().toString().trim().replaceAll(StringUtils.DASH, StringUtils.EMPTY);
            log.debug("[IVS SESSION] fresh session start");
            ivsCommonService.get(label, IvsConst.URI_KEEP_ALIVE);
            log.debug("[IVS SESSION] fresh session end");
        } else {
            log.info("[IVS SESSION] test profile, no ivs to fresh session");
        }
        return Response.ok();
    }

    @GetMapping({"login"})
    public Response login() {
        ivsLoginService.run();
        return Response.ok();
    }
}
