package message

import (
	"context"
	"testing"
	"time"

	"github.com/tidbcloud/AgentTeam/internal/coredb"
)

type conversationTaskFixture struct {
	WorkspaceID       string
	TaskID            string
	TaskScopeID       string
	BoardID           string
	ChannelID         string
	TaskNumber        int64
	Title             string
	Status            string
	CreatedByMemberID string
	OwnerMemberID     string
	CurrentAgentRef   string
	ClaimedByRef      string
	SourceThreadID    string
	ParentChannelID   string
	ParentMessageID   string
	Now               time.Time
}

func insertConversationTaskForTest(t *testing.T, ctx context.Context, db coredb.QueryExecer, fixture conversationTaskFixture) {
	t.Helper()

	mustExecMessaging(t, ctx, db, `
INSERT INTO scopes (
    id, workspace_id, type, identity_key, latest_seq, latest_global_seq,
    latest_activity_seq, latest_message_at, visible_message_count, visible_last_message_at,
    archived_until_seq, status, created_by_member_id, created_at, updated_at
)
VALUES ($1, $2, 'task', $3, 0, 0, 0, NULL, 0, NULL, 0, 'active', $4, $5, $5)
`, fixture.TaskScopeID, fixture.WorkspaceID, "scope/task/"+fixture.TaskScopeID, fixture.CreatedByMemberID, fixture.Now)
	mustExecMessaging(t, ctx, db, `
INSERT INTO task_boards (id, workspace_id, channel_id, next_task_number, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $5)
ON DUPLICATE KEY UPDATE updated_at = VALUES(updated_at)
`, fixture.BoardID, fixture.WorkspaceID, fixture.ChannelID, fixture.TaskNumber+1, fixture.Now)
	mustExecMessaging(t, ctx, db, `
INSERT INTO tasks (
    id, task_scope_id, board_id, channel_id, task_number, title, status,
    created_by_member_id, created_by_ref, current_agent_ref,
    claimed_by_member_id, claimed_by_ref, source_thread_channel_id,
    source_thread_parent_channel_id, source_thread_parent_message_id,
    created_at, updated_at, last_activity_at
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $8, NULLIF($9, ''),
        NULLIF($10, ''), NULLIF($10, ''), $11, $12, $13, $14, $14, $14)
`, fixture.TaskID, fixture.TaskScopeID, fixture.BoardID, fixture.ChannelID, fixture.TaskNumber, fixture.Title, fixture.Status, fixture.CreatedByMemberID, fixture.CurrentAgentRef, fixture.ClaimedByRef, fixture.SourceThreadID, fixture.ParentChannelID, fixture.ParentMessageID, fixture.Now)
	mustExecMessaging(t, ctx, db, `
INSERT INTO scope_memberships (
    scope_id, member_id, visibility, owner_member_id, last_read_seq,
    last_read_global_seq, last_activity_global_seq, owner_context_dismissed_through_global_seq,
    effective_from_seq, effective_to_seq, created_at, updated_at, removed_at, archived_at
)
VALUES ($1, $2, 'public', NULL, 0, 0, 0, 0, 0, NULL, $3, $3, NULL, NULL)
`, fixture.TaskScopeID, fixture.OwnerMemberID, fixture.Now)
	mustExecMessaging(t, ctx, db, `
INSERT INTO scope_notification_settings (scope_id, member_id, state, created_at, updated_at)
VALUES ($1, $2, 'quiet', $3, $3)
`, fixture.TaskScopeID, fixture.OwnerMemberID, fixture.Now)
}

func insertConversationTaskActivityForTest(t *testing.T, ctx context.Context, db coredb.QueryExecer, workspaceID string, taskScopeID string, eventID string, seq int64, activityType string, actorMemberID string, payload string, now time.Time) {
	t.Helper()

	mustExecMessaging(t, ctx, db, `
INSERT INTO scope_activity_events (
    id, workspace_id, scope_id, seq, activity_type, activity_ref_type,
    activity_ref_id, activity_global_seq, source_message_id, actor_member_id,
    reason, payload, created_at, suppressed_at
)
VALUES ($1, $2, $3, $4, $5, 'task_event', $1, $4, NULL, $6, 'task_event', $7, $8, NULL)
`, eventID, workspaceID, taskScopeID, seq, activityType, actorMemberID, `{"taskEvent":{"type":"`+activityType+`","payload":"`+payload+`"}}`, now)
	mustExecMessaging(t, ctx, db, `
UPDATE scopes
SET latest_activity_seq = GREATEST(latest_activity_seq, $2), updated_at = $3
WHERE id = $1
	`, taskScopeID, seq, now)
}
