package agent_test

import (
	"testing"
	"time"

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

func seedAgentTestChannelScope(t *testing.T, pool coredb.QueryExecer, workspaceID string, channelID string, slug string, createdByMemberID string, now time.Time) {
	t.Helper()

	mustExec(t, pool, `
INSERT IGNORE INTO scopes (
    id, workspace_id, type, identity_key, latest_seq, latest_global_seq,
    latest_message_at, archived_until_seq, status, created_by_member_id,
    created_at, updated_at
)
VALUES ($1, $2, 'channel', $3, 0, 0, NULL, 0, 'active', NULLIF($4, ''), $5, $5)
`, channelID, workspaceID, "channel:"+channelID, createdByMemberID, now)
	mustExec(t, pool, `
INSERT IGNORE INTO scope_channel_profiles (
    scope_id, workspace_id, slug, display_name, description, visibility,
    owner_member_id, is_all_channel, created_at, updated_at
)
VALUES ($1, $2, $3, $3, '', 'public', NULL, CASE WHEN $3 = 'all' THEN 1 ELSE 0 END, $4, $4)
`, channelID, workspaceID, slug, now)
}

func seedAgentTestPublicScopeMembership(t *testing.T, pool coredb.QueryExecer, scopeID string, memberID string, now time.Time) {
	t.Helper()

	seedAgentTestPublicScopeMembershipWithState(t, pool, scopeID, memberID, "active", now)
}

func seedAgentTestPublicScopeMembershipWithState(t *testing.T, pool coredb.QueryExecer, scopeID string, memberID string, notificationState string, now time.Time) {
	t.Helper()

	mustExec(t, pool, `
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)
ON DUPLICATE KEY UPDATE removed_at = NULL, archived_at = NULL, updated_at = VALUES(updated_at)
`, scopeID, memberID, now)
	mustExec(t, pool, `
INSERT IGNORE INTO scope_notification_settings (
    scope_id, member_id, state, created_at, updated_at
)
VALUES ($1, $2, $3, $4, $4)
`, scopeID, memberID, notificationState, now)
}

func seedAgentTestPublicScopeMemberships(t *testing.T, pool coredb.QueryExecer, scopeID string, now time.Time, memberIDs ...string) {
	t.Helper()

	for _, memberID := range memberIDs {
		seedAgentTestPublicScopeMembership(t, pool, scopeID, memberID, now)
	}
}

func seedAgentTestPrivateScopeMembership(t *testing.T, pool coredb.QueryExecer, scopeID string, agentMemberID string, ownerMemberID string, now time.Time) {
	t.Helper()

	mustExec(t, pool, `
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, 'private', $3, 0, 0, 0, 0, 0, NULL, $4, $4, NULL, NULL)
ON DUPLICATE KEY UPDATE
    visibility = VALUES(visibility),
    owner_member_id = VALUES(owner_member_id),
    removed_at = NULL,
    archived_at = NULL,
    updated_at = VALUES(updated_at)
`, scopeID, agentMemberID, ownerMemberID, now)
	mustExec(t, pool, `
INSERT INTO scope_notification_settings (
    scope_id, member_id, state, created_at, updated_at
)
VALUES ($1, $2, 'auto-wake', $3, $3)
ON DUPLICATE KEY UPDATE state = VALUES(state), updated_at = VALUES(updated_at)
`, scopeID, agentMemberID, now)
}
