package workspace

import (
	"github.com/tidbcloud/AgentTeam/internal/api"
	"github.com/tidbcloud/AgentTeam/internal/api/dto"
	workspaceservice "github.com/tidbcloud/AgentTeam/internal/service/workspace"
)

// ChannelSidebarPreference handles GET /api/v1/workspaces/:workspaceId/sidebar/channel-sections.
func (handler *WorkspaceHandler) ChannelSidebarPreference(r api.WorkspaceScopedRequest) (api.Response, error) {
	preference, exists, err := handler.workspaceService.GetChannelSidebarPreferenceForUser(
		r.Context(),
		r.UserID(),
		r.WorkspaceID(),
	)
	if err != nil {
		return api.Response{}, err
	}
	if !exists {
		return api.OK(dto.WorkspaceChannelSidebarPreferenceResponse{}), nil
	}
	state := channelSidebarPreferenceStateResponse(preference.State)
	revision := preference.Revision
	updatedAt := preference.UpdatedAt
	return api.OK(dto.WorkspaceChannelSidebarPreferenceResponse{
		Preference: &state,
		Revision:   &revision,
		UpdatedAt:  &updatedAt,
	}), nil
}

// PutChannelSidebarPreference handles PUT /api/v1/workspaces/:workspaceId/sidebar/channel-sections.
func (handler *WorkspaceHandler) PutChannelSidebarPreference(r api.WorkspaceScopedJSON[dto.WorkspaceChannelSidebarPreferenceUpdateRequest]) (api.Response, error) {
	preference, err := handler.workspaceService.PutChannelSidebarPreferenceForUser(
		r.Context(),
		r.UserID(),
		r.WorkspaceID(),
		channelSidebarPreferenceStateInput(r.Body.Preference),
		r.Body.ExpectedRevision,
	)
	if err != nil {
		return api.Response{}, err
	}
	state := channelSidebarPreferenceStateResponse(preference.State)
	revision := preference.Revision
	updatedAt := preference.UpdatedAt
	return api.OK(dto.WorkspaceChannelSidebarPreferenceResponse{
		Preference: &state,
		Revision:   &revision,
		UpdatedAt:  &updatedAt,
	}), nil
}

func channelSidebarPreferenceStateInput(input dto.WorkspaceChannelSidebarPreferenceState) workspaceservice.ChannelSidebarPreferenceState {
	sections := make([]workspaceservice.ChannelSidebarSection, 0, len(input.Sections))
	for _, section := range input.Sections {
		sections = append(sections, workspaceservice.ChannelSidebarSection{
			ID:         section.ID,
			Name:       section.Name,
			Emoji:      section.Emoji,
			ChannelIDs: append([]string(nil), section.ChannelIDs...),
		})
	}
	return workspaceservice.ChannelSidebarPreferenceState{
		Sections:  sections,
		Collapsed: input.Collapsed,
	}
}

func channelSidebarPreferenceStateResponse(input workspaceservice.ChannelSidebarPreferenceState) dto.WorkspaceChannelSidebarPreferenceState {
	sections := make([]dto.WorkspaceChannelSidebarSection, 0, len(input.Sections))
	for _, section := range input.Sections {
		sections = append(sections, dto.WorkspaceChannelSidebarSection{
			ID:         section.ID,
			Name:       section.Name,
			Emoji:      section.Emoji,
			ChannelIDs: append(make([]string, 0, len(section.ChannelIDs)), section.ChannelIDs...),
		})
	}
	return dto.WorkspaceChannelSidebarPreferenceState{
		Sections:  sections,
		Collapsed: input.Collapsed,
	}
}
