package dto

import (
	"time"

	"github.com/tidbcloud/AgentTeam/desktop/internal/types"
)

type (
	// SessionsResponse is the JSON response for the sessions listing endpoint.
	SessionsResponse struct {
		Active []SessionSnapshotResponse `json:"active"`
	}

	// SessionSnapshotResponse is the desktop-web HTTP representation of one
	// running interactive agent session. It intentionally mirrors
	// types.SessionSnapshot without changing the core runtime/persistence tags.
	SessionSnapshotResponse struct {
		SessionKey       string                 `json:"sessionKey,omitempty"`
		AgentID          string                 `json:"agentId"`
		AgentConfigID    string                 `json:"agentConfigId"`
		AgentName        string                 `json:"agentName,omitempty"`
		Title            string                 `json:"title,omitempty"`
		ChannelID        string                 `json:"channelId"`
		ConversationKey  string                 `json:"conversationKey"`
		WorkDir          string                 `json:"workDir"`
		Status           string                 `json:"status"`
		DeliveryStatus   string                 `json:"deliveryStatus,omitempty"`
		Activity         string                 `json:"activity,omitempty"`
		ActivityDetail   string                 `json:"activityDetail,omitempty"`
		MessageCount     int                    `json:"messageCount"`
		StartedAt        time.Time              `json:"startedAt"`
		ServerAgentID    string                 `json:"serverAgentId,omitempty"`
		ActiveTaskTarget types.ActiveTaskTarget `json:"activeTaskTarget,omitempty,omitzero"`
	}
)

// SessionSnapshotResponseFromTypes converts core session state into the
// desktop-web HTTP DTO without mutating the core runtime/session JSON schema.
func SessionSnapshotResponseFromTypes(snapshot types.SessionSnapshot) SessionSnapshotResponse {
	return SessionSnapshotResponse{
		SessionKey:       snapshot.SessionKey,
		AgentID:          snapshot.AgentID,
		AgentConfigID:    snapshot.AgentConfigID,
		AgentName:        snapshot.AgentName,
		Title:            snapshot.Title,
		ChannelID:        snapshot.ChannelID,
		ConversationKey:  snapshot.ConversationKey,
		WorkDir:          snapshot.WorkDir,
		Status:           snapshot.Status,
		DeliveryStatus:   snapshot.DeliveryStatus,
		Activity:         snapshot.Activity,
		ActivityDetail:   snapshot.ActivityDetail,
		MessageCount:     snapshot.MessageCount,
		StartedAt:        snapshot.StartedAt,
		ServerAgentID:    snapshot.ServerAgentID,
		ActiveTaskTarget: snapshot.ActiveTaskTarget,
	}
}

// SessionSnapshotResponsesFromTypes converts a slice of core session
// snapshots to the desktop-web HTTP DTO shape.
func SessionSnapshotResponsesFromTypes(snapshots []types.SessionSnapshot) []SessionSnapshotResponse {
	if len(snapshots) == 0 {
		return nil
	}
	resp := make([]SessionSnapshotResponse, 0, len(snapshots))
	for _, snapshot := range snapshots {
		resp = append(resp, SessionSnapshotResponseFromTypes(snapshot))
	}
	return resp
}
