package com.buukle.agent.instance.service.converter;

import com.buukle.agent.instance.domain.AgentSession;
import com.buukle.agent.instance.dtvo.dto.CreateSessionDTO;
import com.buukle.agent.instance.dtvo.enums.SessionEnum;
import com.buukle.agent.instance.dtvo.vo.SessionVO;
import org.springframework.stereotype.Component;

import java.time.format.DateTimeFormatter;

@Component
public class SessionConverter {
    private static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public SessionVO toVO(AgentSession session) {
        if (session == null) return null;
        SessionVO vo = new SessionVO();
        vo.setId(session.getId());
        vo.setTitle(session.getTitle());
        vo.setAgentInstanceId(session.getAgentInstanceId());
        vo.setStatus(session.getStatus());
        vo.setSummary(session.getSummary());
        vo.setTitleAutoGenerated(session.getTitleAutoGenerated());
        vo.setSummaryUpdatedAt(session.getSummaryUpdatedAt() != null ? session.getSummaryUpdatedAt().format(DTF) : null);
        vo.setSummaryModelRouteId(session.getSummaryModelRouteId());
        vo.setCreatedAt(session.getCreatedAt() != null ? session.getCreatedAt().format(DTF) : null);
        vo.setCreatedBy(session.getCreatedBy());
        vo.setUpdatedBy(session.getUpdatedBy());
        vo.setUpdatedAt(session.getUpdatedAt() != null ? session.getUpdatedAt().format(DTF) : null);
        return vo;
    }

    public AgentSession toDO(CreateSessionDTO dto) {
        AgentSession session = new AgentSession();
        session.setTitle(dto.getTitle());
        session.setAgentInstanceId(dto.getAgentInstanceId());
        session.setStatus(SessionEnum.STATUS_ACTIVE);
        session.setCachedInRedis(false);
        return session;
    }
}
