package postgres

import (
	"context"
	"fmt"
)

type DemoBootstrapSummary struct {
	TenantCode             string `json:"tenantCode"`
	ChannelCode            string `json:"channelCode"`
	EventID                string `json:"eventId"`
	ReleaseID              string `json:"releaseId"`
	SourceID               string `json:"sourceId"`
	BuildID                string `json:"buildId"`
	CardID                 string `json:"cardId"`
	PlaceID                string `json:"placeId"`
	MapAssetID             string `json:"mapAssetId"`
	TileReleaseID          string `json:"tileReleaseId"`
	CourseSourceID         string `json:"courseSourceId"`
	CourseSetID            string `json:"courseSetId"`
	CourseVariantID        string `json:"courseVariantId"`
	RuntimeBindingID       string `json:"runtimeBindingId"`
	ScoreOEventID          string `json:"scoreOEventId"`
	ScoreOReleaseID        string `json:"scoreOReleaseId"`
	ScoreOCardID           string `json:"scoreOCardId"`
	ScoreOSourceID         string `json:"scoreOSourceId"`
	ScoreOBuildID          string `json:"scoreOBuildId"`
	ScoreOCourseSetID      string `json:"scoreOCourseSetId"`
	ScoreOCourseVariantID  string `json:"scoreOCourseVariantId"`
	ScoreORuntimeBindingID string `json:"scoreORuntimeBindingId"`
	VariantManualEventID   string `json:"variantManualEventId"`
	VariantManualRelease   string `json:"variantManualReleaseId"`
	VariantManualCardID    string `json:"variantManualCardId"`
	VariantManualSourceID  string `json:"variantManualSourceId"`
	VariantManualBuildID   string `json:"variantManualBuildId"`
	VariantManualCourseSet string `json:"variantManualCourseSetId"`
	VariantManualVariantID string `json:"variantManualCourseVariantId"`
	VariantManualRuntimeID string `json:"variantManualRuntimeBindingId"`
	CleanedSessionCount    int64  `json:"cleanedSessionCount"`
}

func (s *Store) EnsureDemoData(ctx context.Context) (*DemoBootstrapSummary, error) {
	tx, err := s.Begin(ctx)
	if err != nil {
		return nil, err
	}
	defer tx.Rollback(ctx)

	var tenantID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO tenants (tenant_code, name, status)
		VALUES ('tenant_demo', '联调租户', 'active')
		ON CONFLICT (tenant_code) DO UPDATE SET
			name = EXCLUDED.name,
			status = EXCLUDED.status
		RETURNING id
	`).Scan(&tenantID); err != nil {
		return nil, fmt.Errorf("ensure demo tenant: %w", err)
	}

	var channelID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO entry_channels (
			tenant_id, channel_code, channel_type, platform_app_id, display_name, status, is_default
		)
		VALUES ($1, 'mini-demo', 'wechat_mini', 'wx-demo-appid', '小程序联调入口', 'active', true)
		ON CONFLICT (tenant_id, channel_code) DO UPDATE SET
			channel_type = EXCLUDED.channel_type,
			platform_app_id = EXCLUDED.platform_app_id,
			display_name = EXCLUDED.display_name,
			status = EXCLUDED.status,
			is_default = EXCLUDED.is_default
		RETURNING id
	`, tenantID).Scan(&channelID); err != nil {
		return nil, fmt.Errorf("ensure demo entry channel: %w", err)
	}

	var eventID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO events (
			tenant_id, event_public_id, slug, display_name, summary, status, is_default_experience, show_in_event_list
		)
		VALUES ($1, 'evt_demo_001', 'city-park-classic', '领秀城公园顺序赛', '顺序赛联调样例活动', 'active', true, true)
		ON CONFLICT (event_public_id) DO UPDATE SET
			tenant_id = EXCLUDED.tenant_id,
			slug = EXCLUDED.slug,
			display_name = EXCLUDED.display_name,
			summary = EXCLUDED.summary,
			status = EXCLUDED.status,
			is_default_experience = EXCLUDED.is_default_experience,
			show_in_event_list = EXCLUDED.show_in_event_list
		RETURNING id
	`, tenantID).Scan(&eventID); err != nil {
		return nil, fmt.Errorf("ensure demo event: %w", err)
	}

	var releaseRow struct {
		ID       string
		PublicID string
	}
	if err := tx.QueryRow(ctx, `
		INSERT INTO event_releases (
			release_public_id,
			event_id,
			release_no,
			config_label,
			manifest_url,
			manifest_checksum_sha256,
			route_code,
			status,
			payload_jsonb
		)
		VALUES (
			'rel_demo_001',
			$1,
			1,
			'顺序赛联调配置 v1',
			'https://api.gotomars.xyz/dev/demo-assets/manifests/classic',
			'demo-checksum-001',
			'route-demo-001',
			'published',
			$2::jsonb
		)
		ON CONFLICT (release_public_id) DO UPDATE SET
			event_id = EXCLUDED.event_id,
			config_label = EXCLUDED.config_label,
			manifest_url = EXCLUDED.manifest_url,
			manifest_checksum_sha256 = EXCLUDED.manifest_checksum_sha256,
			route_code = EXCLUDED.route_code,
			status = EXCLUDED.status,
			payload_jsonb = EXCLUDED.payload_jsonb
		RETURNING id, release_public_id
	`, eventID, `{
  "schemaVersion": "1",
  "preview": {
    "mode": "course-preview",
    "baseTiles": {
      "tileBaseUrl": "https://oss-mbh5.colormaprun.com/gotomars/map/lxcb-001/tiles/",
      "zoom": 16,
      "tileSize": 256
    },
    "viewport": {
      "width": 375,
      "height": 220,
      "minLon": 117.1001,
      "minLat": 36.6501,
      "maxLon": 117.1089,
      "maxLat": 36.6578
    },
    "selectedVariantId": "classic_main",
    "variants": [
      {
        "id": "classic_main",
        "name": "顺序赛主路线",
        "controls": [
          {"id": "31", "code": "31", "lon": 117.1012, "lat": 36.6512},
          {"id": "32", "code": "32", "lon": 117.1034, "lat": 36.6529},
          {"id": "33", "code": "33", "lon": 117.1052, "lat": 36.6541},
          {"id": "34", "code": "34", "lon": 117.1072, "lat": 36.6561}
        ],
        "legs": [
          {"from": "31", "to": "32"},
          {"from": "32", "to": "33"},
          {"from": "33", "to": "34"}
        ]
      }
    ]
  },
  "playfield": {
    "kind": "course"
  },
  "game": {
    "mode": "classic-sequential"
  }
}`).Scan(&releaseRow.ID, &releaseRow.PublicID); err != nil {
		return nil, fmt.Errorf("ensure demo release: %w", err)
	}

	if _, err := tx.Exec(ctx, `
		UPDATE events
		SET current_release_id = $2
		WHERE id = $1
	`, eventID, releaseRow.ID); err != nil {
		return nil, fmt.Errorf("attach demo release: %w", err)
	}

	sourceNotes := "顺序赛联调 source 配置"
	source, err := s.UpsertEventConfigSource(ctx, tx, UpsertEventConfigSourceParams{
		EventID:         eventID,
		SourceVersionNo: 1,
		SourceKind:      "event_bundle",
		SchemaID:        "event-source",
		SchemaVersion:   "1",
		Status:          "active",
		Notes:           &sourceNotes,
		Source: map[string]any{
			"app": map[string]any{
				"id":    "sample-classic-001",
				"title": "领秀城公园顺序赛",
			},
			"branding": map[string]any{
				"tenantCode":   "tenant_demo",
				"entryChannel": "mini-demo",
			},
			"map": map[string]any{
				"tiles":   "../map/lxcb-001/tiles/",
				"mapmeta": "../map/lxcb-001/tiles/meta.json",
			},
			"preview": demoPreviewManifest("classic"),
			"playfield": map[string]any{
				"kind": "course",
				"source": map[string]any{
					"type": "kml",
					"url":  "https://oss-mbh5.colormaprun.com/gotomars/kml/lxcb-001/2026-04-07/route04.kml",
				},
			},
			"game": map[string]any{
				"mode": "classic-sequential",
			},
			"content": map[string]any{
				"h5Template": "content-h5-test-template.html",
			},
		},
	})
	if err != nil {
		return nil, fmt.Errorf("ensure demo event config source: %w", err)
	}

	buildLog := "顺序赛联调 build 产物"
	build, err := s.UpsertEventConfigBuild(ctx, tx, UpsertEventConfigBuildParams{
		EventID:     eventID,
		SourceID:    source.ID,
		BuildNo:     1,
		BuildStatus: "success",
		BuildLog:    &buildLog,
		Manifest: map[string]any{
			"schemaVersion": "1",
			"releaseId":     "rel_demo_001",
			"version":       "2026.04.01",
			"app": map[string]any{
				"id":    "sample-classic-001",
				"title": "领秀城公园顺序赛",
			},
			"map": map[string]any{
				"tiles":   "https://oss-mbh5.colormaprun.com/gotomars/map/lxcb-001/tiles/",
				"mapmeta": "https://oss-mbh5.colormaprun.com/gotomars/map/lxcb-001/tiles/meta.json",
			},
			"preview": demoPreviewManifest("classic"),
			"playfield": map[string]any{
				"kind": "course",
				"source": map[string]any{
					"type": "kml",
					"url":  "https://oss-mbh5.colormaprun.com/gotomars/kml/lxcb-001/10/c01.kml",
				},
			},
			"game": map[string]any{
				"mode": "classic-sequential",
			},
			"assets": map[string]any{
				"contentHtml": "https://oss-mbh5.colormaprun.com/gotomars/event/content-h5-test-template.html",
			},
		},
		AssetIndex: []map[string]any{
			{
				"assetType": "manifest",
				"assetKey":  "manifest",
			},
			{
				"assetType": "mapmeta",
				"assetKey":  "mapmeta",
			},
			{
				"assetType": "playfield",
				"assetKey":  "playfield-kml",
			},
			{
				"assetType": "content_html",
				"assetKey":  "content-html",
			},
		},
	})
	if err != nil {
		return nil, fmt.Errorf("ensure demo event config build: %w", err)
	}

	if err := s.AttachBuildToRelease(ctx, tx, releaseRow.ID, build.ID); err != nil {
		return nil, fmt.Errorf("attach demo build to release: %w", err)
	}

	tilesPath := "map/lxcb-001/tiles/"
	mapmetaPath := "map/lxcb-001/tiles/meta.json"
	playfieldPath := "kml/lxcb-001/10/c01.kml"
	contentPath := "event/content-h5-test-template.html"
	manifestChecksum := "demo-checksum-001"
	if err := s.ReplaceEventReleaseAssets(ctx, tx, releaseRow.ID, []UpsertEventReleaseAssetParams{
		{
			EventReleaseID: releaseRow.ID,
			AssetType:      "manifest",
			AssetKey:       "manifest",
			AssetURL:       "https://api.gotomars.xyz/dev/demo-assets/manifests/classic",
			Checksum:       &manifestChecksum,
			Meta:           map[string]any{"source": "release-manifest"},
		},
		{
			EventReleaseID: releaseRow.ID,
			AssetType:      "tiles",
			AssetKey:       "tiles-root",
			AssetPath:      &tilesPath,
			AssetURL:       "https://oss-mbh5.colormaprun.com/gotomars/map/lxcb-001/tiles/",
			Meta:           map[string]any{"kind": "directory"},
		},
		{
			EventReleaseID: releaseRow.ID,
			AssetType:      "mapmeta",
			AssetKey:       "mapmeta",
			AssetPath:      &mapmetaPath,
			AssetURL:       "https://oss-mbh5.colormaprun.com/gotomars/map/lxcb-001/tiles/meta.json",
			Meta:           map[string]any{"format": "json"},
		},
		{
			EventReleaseID: releaseRow.ID,
			AssetType:      "playfield",
			AssetKey:       "course-kml",
			AssetPath:      &playfieldPath,
			AssetURL:       "https://oss-mbh5.colormaprun.com/gotomars/kml/lxcb-001/10/c01.kml",
			Meta:           map[string]any{"format": "kml"},
		},
		{
			EventReleaseID: releaseRow.ID,
			AssetType:      "content_html",
			AssetKey:       "content-html",
			AssetPath:      &contentPath,
			AssetURL:       "https://oss-mbh5.colormaprun.com/gotomars/event/content-h5-test-template.html",
			Meta:           map[string]any{"kind": "content-page"},
		},
	}); err != nil {
		return nil, fmt.Errorf("ensure demo event release assets: %w", err)
	}

	var cardPublicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO cards (
			card_public_id,
			tenant_id,
			entry_channel_id,
			card_type,
			title,
			subtitle,
			cover_url,
			event_id,
			display_slot,
			display_priority,
			status,
			is_default_experience,
			starts_at,
			ends_at
		)
		VALUES (
			'card_demo_001',
			$1,
			$2,
			'event',
			'领秀城公园顺序赛',
			'顺序赛推荐入口',
			'https://oss-mbh5.colormaprun.com/gotomars/assets/demo-cover.jpg',
			$3,
			'home_primary',
			100,
			'active',
			true,
			NOW() - INTERVAL '1 day',
			NOW() + INTERVAL '30 day'
		)
		ON CONFLICT (card_public_id) DO UPDATE SET
			tenant_id = EXCLUDED.tenant_id,
			entry_channel_id = EXCLUDED.entry_channel_id,
			card_type = EXCLUDED.card_type,
			title = EXCLUDED.title,
			subtitle = EXCLUDED.subtitle,
			cover_url = EXCLUDED.cover_url,
			event_id = EXCLUDED.event_id,
			display_slot = EXCLUDED.display_slot,
			display_priority = EXCLUDED.display_priority,
			status = EXCLUDED.status,
			is_default_experience = EXCLUDED.is_default_experience,
			starts_at = EXCLUDED.starts_at,
			ends_at = EXCLUDED.ends_at
		RETURNING card_public_id
	`, tenantID, channelID, eventID).Scan(&cardPublicID); err != nil {
		return nil, fmt.Errorf("ensure demo card: %w", err)
	}

	var placeID, placePublicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO places (
			place_public_id, code, name, region, status
		)
		VALUES (
			'place_demo_001', 'place-demo-001', '领秀城公园', 'Shanghai', 'active'
		)
		ON CONFLICT (code) DO UPDATE SET
			name = EXCLUDED.name,
			region = EXCLUDED.region,
			status = EXCLUDED.status
		RETURNING id, place_public_id
	`).Scan(&placeID, &placePublicID); err != nil {
		return nil, fmt.Errorf("ensure demo place: %w", err)
	}

	var mapAssetID, mapAssetPublicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO map_assets (
			map_asset_public_id, place_id, code, name, map_type, status
		)
		VALUES (
			'mapasset_demo_001', $1, 'mapasset-demo-001', '领秀城公园基础底图', 'standard', 'active'
		)
		ON CONFLICT (code) DO UPDATE SET
			place_id = EXCLUDED.place_id,
			name = EXCLUDED.name,
			map_type = EXCLUDED.map_type,
			status = EXCLUDED.status
		RETURNING id, map_asset_public_id
	`, placeID).Scan(&mapAssetID, &mapAssetPublicID); err != nil {
		return nil, fmt.Errorf("ensure demo map asset: %w", err)
	}

	var tileReleaseID, tileReleasePublicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO tile_releases (
			tile_release_public_id, map_asset_id, version_code, status, tile_base_url, meta_url, published_at
		)
		VALUES (
			'tile_demo_001', $1, 'v2026-04-03', 'published',
			'https://oss-mbh5.colormaprun.com/gotomars/map/lxcb-001/tiles/', 'https://oss-mbh5.colormaprun.com/gotomars/map/lxcb-001/tiles/meta.json', NOW()
		)
		ON CONFLICT (map_asset_id, version_code) DO UPDATE SET
			status = EXCLUDED.status,
			tile_base_url = EXCLUDED.tile_base_url,
			meta_url = EXCLUDED.meta_url,
			published_at = EXCLUDED.published_at
		RETURNING id, tile_release_public_id
	`, mapAssetID).Scan(&tileReleaseID, &tileReleasePublicID); err != nil {
		return nil, fmt.Errorf("ensure demo tile release: %w", err)
	}

	if _, err := tx.Exec(ctx, `
		UPDATE map_assets
		SET current_tile_release_id = $2
		WHERE id = $1
	`, mapAssetID, tileReleaseID); err != nil {
		return nil, fmt.Errorf("attach demo tile release: %w", err)
	}

	var courseSourceID, courseSourcePublicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO course_sources (
			course_source_public_id, source_type, file_url, import_status
		)
		VALUES (
			'csource_demo_001', 'kml', 'https://oss-mbh5.colormaprun.com/gotomars/kml/lxcb-001/10/c01.kml', 'imported'
		)
		ON CONFLICT (course_source_public_id) DO UPDATE SET
			source_type = EXCLUDED.source_type,
			file_url = EXCLUDED.file_url,
			import_status = EXCLUDED.import_status
		RETURNING id, course_source_public_id
	`).Scan(&courseSourceID, &courseSourcePublicID); err != nil {
		return nil, fmt.Errorf("ensure demo course source: %w", err)
	}

	var courseSourceVariantBID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO course_sources (
			course_source_public_id, source_type, file_url, import_status
		)
		VALUES (
			'csource_demo_002', 'kml', 'https://oss-mbh5.colormaprun.com/gotomars/kml/lxcb-001/2026-04-07/route02.kml', 'imported'
		)
		ON CONFLICT (course_source_public_id) DO UPDATE SET
			source_type = EXCLUDED.source_type,
			file_url = EXCLUDED.file_url,
			import_status = EXCLUDED.import_status
		RETURNING id, course_source_public_id
	`).Scan(&courseSourceVariantBID, new(string)); err != nil {
		return nil, fmt.Errorf("ensure demo course source variant b: %w", err)
	}

	var courseSourceVariantCID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO course_sources (
			course_source_public_id, source_type, file_url, import_status
		)
		VALUES (
			'csource_demo_003', 'kml', 'https://oss-mbh5.colormaprun.com/gotomars/kml/lxcb-001/2026-04-07/route03.kml', 'imported'
		)
		ON CONFLICT (course_source_public_id) DO UPDATE SET
			source_type = EXCLUDED.source_type,
			file_url = EXCLUDED.file_url,
			import_status = EXCLUDED.import_status
		RETURNING id, course_source_public_id
	`).Scan(&courseSourceVariantCID, new(string)); err != nil {
		return nil, fmt.Errorf("ensure demo course source variant c: %w", err)
	}

	var courseSourceVariantDID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO course_sources (
			course_source_public_id, source_type, file_url, import_status
		)
		VALUES (
			'csource_demo_004', 'kml', 'https://oss-mbh5.colormaprun.com/gotomars/kml/lxcb-001/2026-04-07/route04.kml', 'imported'
		)
		ON CONFLICT (course_source_public_id) DO UPDATE SET
			source_type = EXCLUDED.source_type,
			file_url = EXCLUDED.file_url,
			import_status = EXCLUDED.import_status
		RETURNING id, course_source_public_id
	`).Scan(&courseSourceVariantDID, new(string)); err != nil {
		return nil, fmt.Errorf("ensure demo course source variant d: %w", err)
	}

	var courseSetID, courseSetPublicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO course_sets (
			course_set_public_id, place_id, map_asset_id, code, mode, name, status
		)
		VALUES (
			'cset_demo_001', $1, $2, 'cset-demo-001', 'classic-sequential', '顺序赛标准赛道', 'active'
		)
		ON CONFLICT (code) DO UPDATE SET
			place_id = EXCLUDED.place_id,
			map_asset_id = EXCLUDED.map_asset_id,
			mode = EXCLUDED.mode,
			name = EXCLUDED.name,
			status = EXCLUDED.status
		RETURNING id, course_set_public_id
	`, placeID, mapAssetID).Scan(&courseSetID, &courseSetPublicID); err != nil {
		return nil, fmt.Errorf("ensure demo course set: %w", err)
	}

	var courseVariantID, courseVariantPublicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO course_variants (
			course_variant_public_id, course_set_id, source_id, name, route_code, mode, control_count, status, is_default
		)
		VALUES (
			'cvariant_demo_001', $1, $2, '顺序赛 A 线', 'route-demo-a', 'classic-sequential', 8, 'active', true
		)
		ON CONFLICT (course_variant_public_id) DO UPDATE SET
			course_set_id = EXCLUDED.course_set_id,
			source_id = EXCLUDED.source_id,
			name = EXCLUDED.name,
			route_code = EXCLUDED.route_code,
			mode = EXCLUDED.mode,
			control_count = EXCLUDED.control_count,
			status = EXCLUDED.status,
			is_default = EXCLUDED.is_default
		RETURNING id, course_variant_public_id
	`, courseSetID, courseSourceID).Scan(&courseVariantID, &courseVariantPublicID); err != nil {
		return nil, fmt.Errorf("ensure demo course variant: %w", err)
	}

	var courseVariantBID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO course_variants (
			course_variant_public_id, course_set_id, source_id, name, route_code, mode, control_count, status, is_default
		)
		VALUES (
			'cvariant_demo_002', $1, $2, '顺序赛 B 线', 'route-demo-b', 'classic-sequential', 10, 'active', false
		)
		ON CONFLICT (course_variant_public_id) DO UPDATE SET
			course_set_id = EXCLUDED.course_set_id,
			source_id = EXCLUDED.source_id,
			name = EXCLUDED.name,
			route_code = EXCLUDED.route_code,
			mode = EXCLUDED.mode,
			control_count = EXCLUDED.control_count,
			status = EXCLUDED.status,
			is_default = EXCLUDED.is_default
		RETURNING id, course_variant_public_id
	`, courseSetID, courseSourceVariantBID).Scan(&courseVariantBID, new(string)); err != nil {
		return nil, fmt.Errorf("ensure demo course variant b: %w", err)
	}

	if _, err := tx.Exec(ctx, `
		UPDATE course_sets
		SET current_variant_id = $2
		WHERE id = $1
	`, courseSetID, courseVariantID); err != nil {
		return nil, fmt.Errorf("attach demo course variant: %w", err)
	}

	var runtimeBindingID, runtimeBindingPublicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO map_runtime_bindings (
			runtime_binding_public_id, event_id, place_id, map_asset_id, tile_release_id, course_set_id, course_variant_id, status, notes
		)
		VALUES (
			'runtime_demo_001', $1, $2, $3, $4, $5, $6, 'active', '顺序赛联调运行绑定'
		)
		ON CONFLICT (runtime_binding_public_id) DO UPDATE SET
			event_id = EXCLUDED.event_id,
			place_id = EXCLUDED.place_id,
			map_asset_id = EXCLUDED.map_asset_id,
			tile_release_id = EXCLUDED.tile_release_id,
			course_set_id = EXCLUDED.course_set_id,
			course_variant_id = EXCLUDED.course_variant_id,
			status = EXCLUDED.status,
			notes = EXCLUDED.notes
		RETURNING id, runtime_binding_public_id
	`, eventID, placeID, mapAssetID, tileReleaseID, courseSetID, courseVariantID).Scan(&runtimeBindingID, &runtimeBindingPublicID); err != nil {
		return nil, fmt.Errorf("ensure demo runtime binding: %w", err)
	}

	classicPresentationID, _, err := s.upsertDemoEventPresentation(ctx, tx, eventID, upsertDemoEventPresentationParams{
		PublicID:         "pres_demo_001",
		Code:             "event-detail-classic",
		Name:             "顺序赛详情展示",
		PresentationType: "detail",
		TemplateKey:      "event.detail.classic",
		Version:          "v2026-04-07-classic",
		Title:            "领秀城公园顺序赛展示定义",
	})
	if err != nil {
		return nil, fmt.Errorf("ensure demo presentation: %w", err)
	}
	classicBundleID, _, err := s.upsertDemoContentBundle(ctx, tx, eventID, upsertDemoContentBundleParams{
		PublicID:     "bundle_demo_001",
		Code:         "result-media-classic",
		Name:         "顺序赛结果内容包",
		BundleType:   "result_media",
		Version:      "v2026-04-07-classic",
		ManifestURL:  "https://api.gotomars.xyz/dev/demo-assets/content-manifests/classic",
		AssetRootURL: "https://oss-mbh5.colormaprun.com/gotomars/event/",
	})
	if err != nil {
		return nil, fmt.Errorf("ensure demo content bundle: %w", err)
	}
	if err := s.SetEventDefaultBindings(ctx, tx, SetEventDefaultBindingsParams{
		EventID:            eventID,
		PresentationID:     &classicPresentationID,
		ContentBundleID:    &classicBundleID,
		RuntimeBindingID:   &runtimeBindingID,
		UpdatePresentation: true,
		UpdateContent:      true,
		UpdateRuntime:      true,
	}); err != nil {
		return nil, fmt.Errorf("set demo default bindings: %w", err)
	}
	if err := s.SetEventReleaseBindings(ctx, tx, releaseRow.ID, &runtimeBindingID, &classicPresentationID, &classicBundleID); err != nil {
		return nil, fmt.Errorf("bind demo release defaults: %w", err)
	}

	var manualEventID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO events (
			tenant_id, event_public_id, slug, display_name, summary, status, is_default_experience, show_in_event_list
		)
		VALUES ($1, 'evt_demo_variant_manual_001', 'city-park-manual-variant', '领秀城公园多赛道挑战', '手动多赛道联调样例活动', 'active', false, true)
		ON CONFLICT (event_public_id) DO UPDATE SET
			tenant_id = EXCLUDED.tenant_id,
			slug = EXCLUDED.slug,
			display_name = EXCLUDED.display_name,
			summary = EXCLUDED.summary,
			status = EXCLUDED.status,
			is_default_experience = EXCLUDED.is_default_experience,
			show_in_event_list = EXCLUDED.show_in_event_list
		RETURNING id
	`, tenantID).Scan(&manualEventID); err != nil {
		return nil, fmt.Errorf("ensure variant manual demo event: %w", err)
	}

	var manualReleaseRow struct {
		ID       string
		PublicID string
	}
	if err := tx.QueryRow(ctx, `
		INSERT INTO event_releases (
			release_public_id,
			event_id,
			release_no,
			config_label,
			manifest_url,
			manifest_checksum_sha256,
			route_code,
			status,
			payload_jsonb
		)
		VALUES (
			'rel_demo_variant_manual_001',
			$1,
			1,
			'多赛道联调配置 v1',
			'https://api.gotomars.xyz/dev/demo-assets/manifests/manual-variant',
			'demo-variant-checksum-001',
			'route-variant-d',
			'published',
			$2::jsonb
		)
		ON CONFLICT (release_public_id) DO UPDATE SET
			event_id = EXCLUDED.event_id,
			config_label = EXCLUDED.config_label,
			manifest_url = EXCLUDED.manifest_url,
			manifest_checksum_sha256 = EXCLUDED.manifest_checksum_sha256,
			route_code = EXCLUDED.route_code,
			status = EXCLUDED.status,
			payload_jsonb = EXCLUDED.payload_jsonb
		RETURNING id, release_public_id
	`, manualEventID, `{
  "schemaVersion": "1",
  "preview": {
    "mode": "manual-variant-preview",
    "baseTiles": {
      "tileBaseUrl": "https://oss-mbh5.colormaprun.com/gotomars/map/lxcb-001/tiles/",
      "zoom": 16,
      "tileSize": 256
    },
    "viewport": {
      "width": 375,
      "height": 220,
      "minLon": 117.1001,
      "minLat": 36.6501,
      "maxLon": 117.1112,
      "maxLat": 36.6593
    },
    "selectedVariantId": "variant_d",
    "variants": [
      {
        "id": "variant_a",
        "name": "路线 01",
        "routeCode": "route-variant-a",
        "controls": [
          {"id": "1", "code": "1", "lon": 117.000649296107, "lat": 36.5921631022497},
          {"id": "2", "code": "2", "lon": 116.999689737459, "lat": 36.5922740961347},
          {"id": "3", "code": "3", "lon": 116.999108309973, "lat": 36.5919019395375},
          {"id": "4", "code": "4", "lon": 116.999823913032, "lat": 36.591572220351},
          {"id": "5", "code": "5", "lon": 116.999860506371, "lat": 36.5912131186443},
          {"id": "6", "code": "6", "lon": 117.000340285695, "lat": 36.5909356298175},
          {"id": "7", "code": "7", "lon": 117.000441933857, "lat": 36.5915004001434},
          {"id": "8", "code": "8", "lon": 117.001397426578, "lat": 36.5915983367736},
          {"id": "9", "code": "9", "lon": 117.000665559813, "lat": 36.5919574366878},
          {"id": "10", "code": "10", "lon": 117.000649296107, "lat": 36.5921631022497}
        ],
        "legs": [
          {"from": "1", "to": "2"},
          {"from": "2", "to": "3"},
          {"from": "3", "to": "4"},
          {"from": "4", "to": "5"},
          {"from": "5", "to": "6"},
          {"from": "6", "to": "7"},
          {"from": "7", "to": "8"},
          {"from": "8", "to": "9"},
          {"from": "9", "to": "10"}
        ]
      },
      {
        "id": "variant_b",
        "name": "路线 02",
        "routeCode": "route-variant-b",
        "controls": [
          {"id": "1", "code": "1", "lon": 117.000649296107, "lat": 36.5921631022497},
          {"id": "2", "code": "2", "lon": 116.999766990062, "lat": 36.5921141343085},
          {"id": "3", "code": "3", "lon": 116.999710067091, "lat": 36.5917615642144},
          {"id": "4", "code": "4", "lon": 116.998774904002, "lat": 36.5913306430232},
          {"id": "5", "code": "5", "lon": 116.998941606987, "lat": 36.5908278985923},
          {"id": "6", "code": "6", "lon": 117.00058830721, "lat": 36.5905340853955},
          {"id": "7", "code": "7", "lon": 117.000238637533, "lat": 36.5914742836876},
          {"id": "8", "code": "8", "lon": 117.000937976887, "lat": 36.5916113949816},
          {"id": "9", "code": "9", "lon": 117.000803801313, "lat": 36.5919411140006},
          {"id": "10", "code": "10", "lon": 117.000649296107, "lat": 36.5921631022497}
        ],
        "legs": [
          {"from": "1", "to": "2"},
          {"from": "2", "to": "3"},
          {"from": "3", "to": "4"},
          {"from": "4", "to": "5"},
          {"from": "5", "to": "6"},
          {"from": "6", "to": "7"},
          {"from": "7", "to": "8"},
          {"from": "8", "to": "9"},
          {"from": "9", "to": "10"}
        ]
      },
      {
        "id": "variant_c",
        "name": "路线 03",
        "routeCode": "route-variant-c",
        "controls": [
          {"id": "1", "code": "1", "lon": 117.000649296107, "lat": 36.5921631022497},
          {"id": "2", "code": "2", "lon": 117.000665559813, "lat": 36.5919574366878},
          {"id": "3", "code": "3", "lon": 117.001397426578, "lat": 36.5915983367736},
          {"id": "4", "code": "4", "lon": 117.000441933857, "lat": 36.5915004001434},
          {"id": "5", "code": "5", "lon": 117.000340285695, "lat": 36.5909356298175},
          {"id": "6", "code": "6", "lon": 116.999860506371, "lat": 36.5912131186443},
          {"id": "7", "code": "7", "lon": 116.999823913032, "lat": 36.591572220351},
          {"id": "8", "code": "8", "lon": 116.999108309973, "lat": 36.5919019395375},
          {"id": "9", "code": "9", "lon": 116.999689737459, "lat": 36.5922740961347},
          {"id": "10", "code": "10", "lon": 117.000649296107, "lat": 36.5921631022497}
        ],
        "legs": [
          {"from": "1", "to": "2"},
          {"from": "2", "to": "3"},
          {"from": "3", "to": "4"},
          {"from": "4", "to": "5"},
          {"from": "5", "to": "6"},
          {"from": "6", "to": "7"},
          {"from": "7", "to": "8"},
          {"from": "8", "to": "9"},
          {"from": "9", "to": "10"}
        ]
      },
      {
        "id": "variant_d",
        "name": "路线 04",
        "routeCode": "route-variant-d",
        "controls": [
          {"id": "1", "code": "1", "lon": 117.000649296107, "lat": 36.5921631022497},
          {"id": "2", "code": "2", "lon": 117.000803801313, "lat": 36.5919411140006},
          {"id": "3", "code": "3", "lon": 117.000937976887, "lat": 36.5916113949816},
          {"id": "4", "code": "4", "lon": 117.000238637533, "lat": 36.5914742836876},
          {"id": "5", "code": "5", "lon": 117.00058830721, "lat": 36.5905340853955},
          {"id": "6", "code": "6", "lon": 116.998941606987, "lat": 36.5908278985923},
          {"id": "7", "code": "7", "lon": 116.998774904002, "lat": 36.5913306430232},
          {"id": "8", "code": "8", "lon": 116.999710067091, "lat": 36.5917615642144},
          {"id": "9", "code": "9", "lon": 116.999766990062, "lat": 36.5921141343085},
          {"id": "10", "code": "10", "lon": 117.000649296107, "lat": 36.5921631022497}
        ],
        "legs": [
          {"from": "1", "to": "2"},
          {"from": "2", "to": "3"},
          {"from": "3", "to": "4"},
          {"from": "4", "to": "5"},
          {"from": "5", "to": "6"},
          {"from": "6", "to": "7"},
          {"from": "7", "to": "8"},
          {"from": "8", "to": "9"},
          {"from": "9", "to": "10"}
        ]
      }
    ]
  },
  "playfield": {
    "kind": "course-set"
  },
  "game": {
    "mode": "classic-sequential"
  },
  "play": {
    "assignmentMode": "manual",
    "courseVariants": [
      {
        "id": "variant_a",
        "name": "路线 01",
        "description": "route01.kml",
        "routeCode": "route-variant-a",
        "selectable": true
      },
      {
        "id": "variant_b",
        "name": "路线 02",
        "description": "route02.kml",
        "routeCode": "route-variant-b",
        "selectable": true
      },
      {
        "id": "variant_c",
        "name": "路线 03",
        "description": "route03.kml",
        "routeCode": "route-variant-c",
        "selectable": true
      },
      {
        "id": "variant_d",
        "name": "路线 04",
        "description": "route04.kml",
        "routeCode": "route-variant-d",
        "selectable": true
      }
    ]
  }
}`).Scan(&manualReleaseRow.ID, &manualReleaseRow.PublicID); err != nil {
		return nil, fmt.Errorf("ensure variant manual demo release: %w", err)
	}

	if _, err := tx.Exec(ctx, `
		UPDATE events
		SET current_release_id = $2
		WHERE id = $1
	`, manualEventID, manualReleaseRow.ID); err != nil {
		return nil, fmt.Errorf("attach variant manual demo release: %w", err)
	}

	var manualCardPublicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO cards (
			card_public_id,
			tenant_id,
			entry_channel_id,
			card_type,
			title,
			subtitle,
			cover_url,
			event_id,
			display_slot,
			display_priority,
			status,
			is_default_experience,
			starts_at,
			ends_at
		)
		VALUES (
			'card_demo_variant_manual_001',
			$1,
			$2,
			'event',
			'领秀城公园多赛道挑战',
			'手动选择赛道入口',
			'https://oss-mbh5.colormaprun.com/gotomars/assets/demo-cover.jpg',
			$3,
			'home_primary',
			95,
			'active',
			false,
			NOW() - INTERVAL '1 day',
			NOW() + INTERVAL '30 day'
		)
		ON CONFLICT (card_public_id) DO UPDATE SET
			tenant_id = EXCLUDED.tenant_id,
			entry_channel_id = EXCLUDED.entry_channel_id,
			card_type = EXCLUDED.card_type,
			title = EXCLUDED.title,
			subtitle = EXCLUDED.subtitle,
			cover_url = EXCLUDED.cover_url,
			event_id = EXCLUDED.event_id,
			display_slot = EXCLUDED.display_slot,
			display_priority = EXCLUDED.display_priority,
			status = EXCLUDED.status,
			is_default_experience = EXCLUDED.is_default_experience,
			starts_at = EXCLUDED.starts_at,
			ends_at = EXCLUDED.ends_at
		RETURNING card_public_id
	`, tenantID, channelID, manualEventID).Scan(&manualCardPublicID); err != nil {
		return nil, fmt.Errorf("ensure variant manual demo card: %w", err)
	}

	manualSourceNotes := "多赛道联调 source 配置"
	manualSource, err := s.UpsertEventConfigSource(ctx, tx, UpsertEventConfigSourceParams{
		EventID:         manualEventID,
		SourceVersionNo: 1,
		SourceKind:      "event_bundle",
		SchemaID:        "event-source",
		SchemaVersion:   "1",
		Status:          "active",
		Notes:           &manualSourceNotes,
		Source: map[string]any{
			"schemaVersion": "1",
			"app": map[string]any{
				"id":    "sample-variant-manual-001",
				"title": "领秀城公园多赛道挑战",
			},
			"branding": map[string]any{
				"tenantCode":   "tenant_demo",
				"entryChannel": "mini-demo",
			},
			"map": map[string]any{
				"tiles":   "../map/lxcb-001/tiles/",
				"mapmeta": "../map/lxcb-001/tiles/meta.json",
			},
			"preview": demoPreviewManifest("manual-variant"),
			"playfield": map[string]any{
				"kind": "course",
				"source": map[string]any{
					"type": "kml",
					"url":  "../kml/lxcb-001/10/c01.kml",
				},
			},
			"game": map[string]any{
				"mode": "classic-sequential",
			},
			"play": map[string]any{
				"assignmentMode": "manual",
				"courseVariants": []map[string]any{
					{"id": "variant_a", "name": "路线 01", "description": "route01.kml", "routeCode": "route-variant-a", "selectable": true},
					{"id": "variant_b", "name": "路线 02", "description": "route02.kml", "routeCode": "route-variant-b", "selectable": true},
					{"id": "variant_c", "name": "路线 03", "description": "route03.kml", "routeCode": "route-variant-c", "selectable": true},
					{"id": "variant_d", "name": "路线 04", "description": "route04.kml", "routeCode": "route-variant-d", "selectable": true},
				},
			},
			"content": map[string]any{
				"h5Template": "content-h5-test-template.html",
			},
		},
	})
	if err != nil {
		return nil, fmt.Errorf("ensure variant manual demo event config source: %w", err)
	}

	manualBuildLog := "多赛道联调 build 产物"
	manualBuild, err := s.UpsertEventConfigBuild(ctx, tx, UpsertEventConfigBuildParams{
		EventID:     manualEventID,
		SourceID:    manualSource.ID,
		BuildNo:     1,
		BuildStatus: "success",
		BuildLog:    &manualBuildLog,
		Manifest: map[string]any{
			"schemaVersion": "1",
			"releaseId":     "rel_demo_variant_manual_001",
			"version":       "2026.04.01",
			"app": map[string]any{
				"id":    "sample-variant-manual-001",
				"title": "领秀城公园多赛道挑战",
			},
			"map": map[string]any{
				"tiles":   "https://oss-mbh5.colormaprun.com/gotomars/map/lxcb-001/tiles/",
				"mapmeta": "https://oss-mbh5.colormaprun.com/gotomars/map/lxcb-001/tiles/meta.json",
			},
			"preview": demoPreviewManifest("manual-variant"),
			"playfield": map[string]any{
				"kind": "course",
				"source": map[string]any{
					"type": "kml",
					"url":  "https://oss-mbh5.colormaprun.com/gotomars/kml/lxcb-001/2026-04-07/route04.kml",
				},
			},
			"game": map[string]any{
				"mode": "classic-sequential",
			},
			"play": map[string]any{
				"assignmentMode": "manual",
				"courseVariants": []map[string]any{
					{"id": "variant_a", "name": "路线 01", "description": "route01.kml", "routeCode": "route-variant-a", "selectable": true},
					{"id": "variant_b", "name": "路线 02", "description": "route02.kml", "routeCode": "route-variant-b", "selectable": true},
					{"id": "variant_c", "name": "路线 03", "description": "route03.kml", "routeCode": "route-variant-c", "selectable": true},
					{"id": "variant_d", "name": "路线 04", "description": "route04.kml", "routeCode": "route-variant-d", "selectable": true},
				},
			},
			"assets": map[string]any{
				"contentHtml": "https://oss-mbh5.colormaprun.com/gotomars/event/content-h5-test-template.html",
			},
		},
		AssetIndex: []map[string]any{
			{"assetType": "manifest", "assetKey": "manifest"},
			{"assetType": "mapmeta", "assetKey": "mapmeta"},
			{"assetType": "playfield", "assetKey": "playfield-kml"},
			{"assetType": "content_html", "assetKey": "content-html"},
		},
	})
	if err != nil {
		return nil, fmt.Errorf("ensure variant manual demo event config build: %w", err)
	}

	if err := s.AttachBuildToRelease(ctx, tx, manualReleaseRow.ID, manualBuild.ID); err != nil {
		return nil, fmt.Errorf("attach variant manual demo build to release: %w", err)
	}

	var manualCourseSetID, manualCourseSetPublicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO course_sets (
			course_set_public_id, place_id, map_asset_id, code, mode, name, status
		)
		VALUES (
			'cset_demo_variant_manual_001', $1, $2, 'cset-demo-variant-manual-001', 'classic-sequential', '多赛道挑战赛道集', 'active'
		)
		ON CONFLICT (code) DO UPDATE SET
			place_id = EXCLUDED.place_id,
			map_asset_id = EXCLUDED.map_asset_id,
			mode = EXCLUDED.mode,
			name = EXCLUDED.name,
			status = EXCLUDED.status
		RETURNING id, course_set_public_id
	`, placeID, mapAssetID).Scan(&manualCourseSetID, &manualCourseSetPublicID); err != nil {
		return nil, fmt.Errorf("ensure variant manual demo course set: %w", err)
	}

	var manualCourseSourceAID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO course_sources (
			course_source_public_id, source_type, file_url, import_status
		)
		VALUES (
			'csource_demo_variant_manual_a', 'kml', 'https://oss-mbh5.colormaprun.com/gotomars/kml/lxcb-001/2026-04-07/route01.kml', 'imported'
		)
		ON CONFLICT (course_source_public_id) DO UPDATE SET
			source_type = EXCLUDED.source_type,
			file_url = EXCLUDED.file_url,
			import_status = EXCLUDED.import_status
		RETURNING id
	`).Scan(&manualCourseSourceAID); err != nil {
		return nil, fmt.Errorf("ensure variant manual demo source a: %w", err)
	}

	var manualCourseSourceBID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO course_sources (
			course_source_public_id, source_type, file_url, import_status
		)
		VALUES (
			'csource_demo_variant_manual_b', 'kml', 'https://oss-mbh5.colormaprun.com/gotomars/kml/lxcb-001/2026-04-07/route02.kml', 'imported'
		)
		ON CONFLICT (course_source_public_id) DO UPDATE SET
			source_type = EXCLUDED.source_type,
			file_url = EXCLUDED.file_url,
			import_status = EXCLUDED.import_status
		RETURNING id
	`).Scan(&manualCourseSourceBID); err != nil {
		return nil, fmt.Errorf("ensure variant manual demo source b: %w", err)
	}

	var manualCourseSourceCID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO course_sources (
			course_source_public_id, source_type, file_url, import_status
		)
		VALUES (
			'csource_demo_variant_manual_c', 'kml', 'https://oss-mbh5.colormaprun.com/gotomars/kml/lxcb-001/2026-04-07/route03.kml', 'imported'
		)
		ON CONFLICT (course_source_public_id) DO UPDATE SET
			source_type = EXCLUDED.source_type,
			file_url = EXCLUDED.file_url,
			import_status = EXCLUDED.import_status
		RETURNING id
	`).Scan(&manualCourseSourceCID); err != nil {
		return nil, fmt.Errorf("ensure variant manual demo source c: %w", err)
	}

	var manualCourseSourceDID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO course_sources (
			course_source_public_id, source_type, file_url, import_status
		)
		VALUES (
			'csource_demo_variant_manual_d', 'kml', 'https://oss-mbh5.colormaprun.com/gotomars/kml/lxcb-001/2026-04-07/route04.kml', 'imported'
		)
		ON CONFLICT (course_source_public_id) DO UPDATE SET
			source_type = EXCLUDED.source_type,
			file_url = EXCLUDED.file_url,
			import_status = EXCLUDED.import_status
		RETURNING id
	`).Scan(&manualCourseSourceDID); err != nil {
		return nil, fmt.Errorf("ensure variant manual demo source d: %w", err)
	}

	var manualVariantAID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO course_variants (
			course_variant_public_id, course_set_id, source_id, name, route_code, mode, control_count, status, is_default
		)
		VALUES (
			'cvariant_demo_variant_manual_a', $1, $2, '多赛道 路线 01', 'route-variant-a', 'classic-sequential', 10, 'active', false
		)
		ON CONFLICT (course_variant_public_id) DO UPDATE SET
			course_set_id = EXCLUDED.course_set_id,
			source_id = EXCLUDED.source_id,
			name = EXCLUDED.name,
			route_code = EXCLUDED.route_code,
			mode = EXCLUDED.mode,
			control_count = EXCLUDED.control_count,
			status = EXCLUDED.status,
			is_default = EXCLUDED.is_default
		RETURNING id
	`, manualCourseSetID, manualCourseSourceAID).Scan(&manualVariantAID); err != nil {
		return nil, fmt.Errorf("ensure variant manual demo variant a: %w", err)
	}

	var manualVariantBID, manualVariantBPublicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO course_variants (
			course_variant_public_id, course_set_id, source_id, name, route_code, mode, control_count, status, is_default
		)
		VALUES (
			'cvariant_demo_variant_manual_b', $1, $2, '多赛道 路线 02', 'route-variant-b', 'classic-sequential', 10, 'active', false
		)
		ON CONFLICT (course_variant_public_id) DO UPDATE SET
			course_set_id = EXCLUDED.course_set_id,
			source_id = EXCLUDED.source_id,
			name = EXCLUDED.name,
			route_code = EXCLUDED.route_code,
			mode = EXCLUDED.mode,
			control_count = EXCLUDED.control_count,
			status = EXCLUDED.status,
			is_default = EXCLUDED.is_default
		RETURNING id, course_variant_public_id
	`, manualCourseSetID, manualCourseSourceBID).Scan(&manualVariantBID, &manualVariantBPublicID); err != nil {
		return nil, fmt.Errorf("ensure variant manual demo variant b: %w", err)
	}

	var manualVariantCID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO course_variants (
			course_variant_public_id, course_set_id, source_id, name, route_code, mode, control_count, status, is_default
		)
		VALUES (
			'cvariant_demo_variant_manual_c', $1, $2, '多赛道 路线 03', 'route-variant-c', 'classic-sequential', 10, 'active', false
		)
		ON CONFLICT (course_variant_public_id) DO UPDATE SET
			course_set_id = EXCLUDED.course_set_id,
			source_id = EXCLUDED.source_id,
			name = EXCLUDED.name,
			route_code = EXCLUDED.route_code,
			mode = EXCLUDED.mode,
			control_count = EXCLUDED.control_count,
			status = EXCLUDED.status,
			is_default = EXCLUDED.is_default
		RETURNING id
	`, manualCourseSetID, manualCourseSourceCID).Scan(&manualVariantCID); err != nil {
		return nil, fmt.Errorf("ensure variant manual demo variant c: %w", err)
	}

	var manualVariantDID, manualVariantDPublicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO course_variants (
			course_variant_public_id, course_set_id, source_id, name, route_code, mode, control_count, status, is_default
		)
		VALUES (
			'cvariant_demo_variant_manual_d', $1, $2, '多赛道 路线 04', 'route-variant-d', 'classic-sequential', 10, 'active', true
		)
		ON CONFLICT (course_variant_public_id) DO UPDATE SET
			course_set_id = EXCLUDED.course_set_id,
			source_id = EXCLUDED.source_id,
			name = EXCLUDED.name,
			route_code = EXCLUDED.route_code,
			mode = EXCLUDED.mode,
			control_count = EXCLUDED.control_count,
			status = EXCLUDED.status,
			is_default = EXCLUDED.is_default
		RETURNING id, course_variant_public_id
	`, manualCourseSetID, manualCourseSourceDID).Scan(&manualVariantDID, &manualVariantDPublicID); err != nil {
		return nil, fmt.Errorf("ensure variant manual demo variant d: %w", err)
	}

	if _, err := tx.Exec(ctx, `
		UPDATE course_sets
		SET current_variant_id = $2
		WHERE id = $1
	`, manualCourseSetID, manualVariantDID); err != nil {
		return nil, fmt.Errorf("attach variant manual demo course variant: %w", err)
	}

	var manualRuntimeBindingID, manualRuntimeBindingPublicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO map_runtime_bindings (
			runtime_binding_public_id, event_id, place_id, map_asset_id, tile_release_id, course_set_id, course_variant_id, status, notes
		)
		VALUES (
			'runtime_demo_variant_manual_001', $1, $2, $3, $4, $5, $6, 'active', '多赛道联调运行绑定'
		)
		ON CONFLICT (runtime_binding_public_id) DO UPDATE SET
			event_id = EXCLUDED.event_id,
			place_id = EXCLUDED.place_id,
			map_asset_id = EXCLUDED.map_asset_id,
			tile_release_id = EXCLUDED.tile_release_id,
			course_set_id = EXCLUDED.course_set_id,
			course_variant_id = EXCLUDED.course_variant_id,
			status = EXCLUDED.status,
			notes = EXCLUDED.notes
		RETURNING id, runtime_binding_public_id
	`, manualEventID, placeID, mapAssetID, tileReleaseID, manualCourseSetID, manualVariantDID).Scan(&manualRuntimeBindingID, &manualRuntimeBindingPublicID); err != nil {
		return nil, fmt.Errorf("ensure variant manual demo runtime binding: %w", err)
	}

	manualPresentationID, _, err := s.upsertDemoEventPresentation(ctx, tx, manualEventID, upsertDemoEventPresentationParams{
		PublicID:         "pres_demo_variant_manual_001",
		Code:             "event-detail-manual-variant",
		Name:             "多赛道详情展示",
		PresentationType: "detail",
		TemplateKey:      "event.detail.multi-variant",
		Version:          "v2026-04-07-manual",
		Title:            "领秀城公园多赛道挑战展示定义",
	})
	if err != nil {
		return nil, fmt.Errorf("ensure variant manual presentation: %w", err)
	}
	manualBundleID, _, err := s.upsertDemoContentBundle(ctx, tx, manualEventID, upsertDemoContentBundleParams{
		PublicID:     "bundle_demo_variant_manual_001",
		Code:         "result-media-manual",
		Name:         "多赛道结果内容包",
		BundleType:   "result_media",
		Version:      "v2026-04-07-manual",
		ManifestURL:  "https://api.gotomars.xyz/dev/demo-assets/content-manifests/manual",
		AssetRootURL: "https://oss-mbh5.colormaprun.com/gotomars/event/",
	})
	if err != nil {
		return nil, fmt.Errorf("ensure variant manual content bundle: %w", err)
	}
	if err := s.SetEventDefaultBindings(ctx, tx, SetEventDefaultBindingsParams{
		EventID:            manualEventID,
		PresentationID:     &manualPresentationID,
		ContentBundleID:    &manualBundleID,
		RuntimeBindingID:   &manualRuntimeBindingID,
		UpdatePresentation: true,
		UpdateContent:      true,
		UpdateRuntime:      true,
	}); err != nil {
		return nil, fmt.Errorf("set variant manual default bindings: %w", err)
	}
	if err := s.SetEventReleaseBindings(ctx, tx, manualReleaseRow.ID, &manualRuntimeBindingID, &manualPresentationID, &manualBundleID); err != nil {
		return nil, fmt.Errorf("bind variant manual release defaults: %w", err)
	}

	var scoreOEventID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO events (
			tenant_id, event_public_id, slug, display_name, summary, status, is_default_experience, show_in_event_list
		)
		VALUES ($1, 'evt_demo_score_o_001', 'city-park-score-o', '领秀城公园积分赛', '积分赛联调样例活动', 'active', false, true)
		ON CONFLICT (event_public_id) DO UPDATE SET
			tenant_id = EXCLUDED.tenant_id,
			slug = EXCLUDED.slug,
			display_name = EXCLUDED.display_name,
			summary = EXCLUDED.summary,
			status = EXCLUDED.status,
			is_default_experience = EXCLUDED.is_default_experience,
			show_in_event_list = EXCLUDED.show_in_event_list
		RETURNING id
	`, tenantID).Scan(&scoreOEventID); err != nil {
		return nil, fmt.Errorf("ensure score-o demo event: %w", err)
	}

	var scoreOReleaseRow struct {
		ID       string
		PublicID string
	}
	if err := tx.QueryRow(ctx, `
		INSERT INTO event_releases (
			release_public_id,
			event_id,
			release_no,
			config_label,
			manifest_url,
			manifest_checksum_sha256,
			route_code,
			status,
			payload_jsonb
		)
		VALUES (
			'rel_demo_score_o_001',
			$1,
			1,
			'积分赛联调配置 v1',
			'https://api.gotomars.xyz/dev/demo-assets/manifests/score-o',
			'demo-score-o-checksum-001',
			'route-score-o-001',
			'published',
			$2::jsonb
		)
		ON CONFLICT (release_public_id) DO UPDATE SET
			event_id = EXCLUDED.event_id,
			config_label = EXCLUDED.config_label,
			manifest_url = EXCLUDED.manifest_url,
			manifest_checksum_sha256 = EXCLUDED.manifest_checksum_sha256,
			route_code = EXCLUDED.route_code,
			status = EXCLUDED.status,
			payload_jsonb = EXCLUDED.payload_jsonb
		RETURNING id, release_public_id
	`, scoreOEventID, `{
  "schemaVersion": "1",
  "preview": {
    "mode": "control-set-preview",
    "baseTiles": {
      "tileBaseUrl": "https://oss-mbh5.colormaprun.com/gotomars/map/lxcb-001/tiles/",
      "zoom": 16,
      "tileSize": 256
    },
    "viewport": {
      "width": 375,
      "height": 220,
      "minLon": 117.1001,
      "minLat": 36.6501,
      "maxLon": 117.1098,
      "maxLat": 36.6584
    },
    "selectedVariantId": "score_main",
    "variants": [
      {
        "id": "score_main",
        "name": "积分赛标准赛道",
        "controls": [
          {"id": "41", "code": "41", "lon": 117.1015, "lat": 36.6516},
          {"id": "42", "code": "42", "lon": 117.1042, "lat": 36.6527},
          {"id": "43", "code": "43", "lon": 117.1065, "lat": 36.6548},
          {"id": "44", "code": "44", "lon": 117.1082, "lat": 36.6569}
        ],
        "legs": [
          {"from": "41", "to": "42"},
          {"from": "42", "to": "43"},
          {"from": "43", "to": "44"}
        ]
      }
    ]
  },
  "playfield": {
    "kind": "control-set"
  },
  "game": {
    "mode": "score-o"
  }
}`).Scan(&scoreOReleaseRow.ID, &scoreOReleaseRow.PublicID); err != nil {
		return nil, fmt.Errorf("ensure score-o demo release: %w", err)
	}

	if _, err := tx.Exec(ctx, `
		UPDATE events
		SET current_release_id = $2
		WHERE id = $1
	`, scoreOEventID, scoreOReleaseRow.ID); err != nil {
		return nil, fmt.Errorf("attach score-o demo release: %w", err)
	}

	scoreOSourceNotes := "积分赛联调 source 配置"
	scoreOSource, err := s.UpsertEventConfigSource(ctx, tx, UpsertEventConfigSourceParams{
		EventID:         scoreOEventID,
		SourceVersionNo: 1,
		SourceKind:      "event_bundle",
		SchemaID:        "event-source",
		SchemaVersion:   "1",
		Status:          "active",
		Notes:           &scoreOSourceNotes,
		Source: map[string]any{
			"schemaVersion": "1",
			"app": map[string]any{
				"id":    "sample-score-o-001",
				"title": "领秀城公园积分赛",
			},
			"branding": map[string]any{
				"tenantCode":   "tenant_demo",
				"entryChannel": "mini-demo",
			},
			"map": map[string]any{
				"tiles":   "../map/lxcb-001/tiles/",
				"mapmeta": "../map/lxcb-001/tiles/meta.json",
			},
			"preview": demoPreviewManifest("score-o"),
			"playfield": map[string]any{
				"kind": "control-set",
				"source": map[string]any{
					"type": "kml",
					"url":  "../kml/lxcb-001/10/c01.kml",
				},
			},
			"game": map[string]any{
				"mode": "score-o",
			},
			"content": map[string]any{
				"h5Template": "content-h5-test-template.html",
			},
		},
	})
	if err != nil {
		return nil, fmt.Errorf("ensure score-o demo event config source: %w", err)
	}

	scoreOBuildLog := "积分赛联调 build 产物"
	scoreOBuild, err := s.UpsertEventConfigBuild(ctx, tx, UpsertEventConfigBuildParams{
		EventID:     scoreOEventID,
		SourceID:    scoreOSource.ID,
		BuildNo:     1,
		BuildStatus: "success",
		BuildLog:    &scoreOBuildLog,
		Manifest: map[string]any{
			"schemaVersion": "1",
			"releaseId":     "rel_demo_score_o_001",
			"version":       "2026.04.01",
			"app": map[string]any{
				"id":    "sample-score-o-001",
				"title": "领秀城公园积分赛",
			},
			"map": map[string]any{
				"tiles":   "https://oss-mbh5.colormaprun.com/gotomars/map/lxcb-001/tiles/",
				"mapmeta": "https://oss-mbh5.colormaprun.com/gotomars/map/lxcb-001/tiles/meta.json",
			},
			"preview": demoPreviewManifest("score-o"),
			"playfield": map[string]any{
				"kind": "control-set",
				"source": map[string]any{
					"type": "kml",
					"url":  "https://oss-mbh5.colormaprun.com/gotomars/kml/lxcb-001/10/c01.kml",
				},
			},
			"game": map[string]any{
				"mode": "score-o",
			},
			"assets": map[string]any{
				"contentHtml": "https://oss-mbh5.colormaprun.com/gotomars/event/content-h5-test-template.html",
			},
		},
		AssetIndex: []map[string]any{
			{"assetType": "manifest", "assetKey": "manifest"},
			{"assetType": "mapmeta", "assetKey": "mapmeta"},
			{"assetType": "playfield", "assetKey": "playfield-kml"},
			{"assetType": "content_html", "assetKey": "content-html"},
		},
	})
	if err != nil {
		return nil, fmt.Errorf("ensure score-o demo event config build: %w", err)
	}

	if err := s.AttachBuildToRelease(ctx, tx, scoreOReleaseRow.ID, scoreOBuild.ID); err != nil {
		return nil, fmt.Errorf("attach score-o demo build to release: %w", err)
	}

	var scoreOCardPublicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO cards (
			card_public_id,
			tenant_id,
			entry_channel_id,
			card_type,
			title,
			subtitle,
			cover_url,
			event_id,
			display_slot,
			display_priority,
			status,
			is_default_experience,
			starts_at,
			ends_at
		)
		VALUES (
			'card_demo_score_o_001',
			$1,
			$2,
			'event',
			'领秀城公园积分赛',
			'积分赛推荐入口',
			'https://oss-mbh5.colormaprun.com/gotomars/assets/demo-cover.jpg',
			$3,
			'home_primary',
			98,
			'active',
			false,
			NOW() - INTERVAL '1 day',
			NOW() + INTERVAL '30 day'
		)
		ON CONFLICT (card_public_id) DO UPDATE SET
			tenant_id = EXCLUDED.tenant_id,
			entry_channel_id = EXCLUDED.entry_channel_id,
			card_type = EXCLUDED.card_type,
			title = EXCLUDED.title,
			subtitle = EXCLUDED.subtitle,
			cover_url = EXCLUDED.cover_url,
			event_id = EXCLUDED.event_id,
			display_slot = EXCLUDED.display_slot,
			display_priority = EXCLUDED.display_priority,
			status = EXCLUDED.status,
			is_default_experience = EXCLUDED.is_default_experience,
			starts_at = EXCLUDED.starts_at,
			ends_at = EXCLUDED.ends_at
		RETURNING card_public_id
	`, tenantID, channelID, scoreOEventID).Scan(&scoreOCardPublicID); err != nil {
		return nil, fmt.Errorf("ensure score-o demo card: %w", err)
	}

	var scoreOCourseSetID, scoreOCourseSetPublicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO course_sets (
			course_set_public_id, place_id, map_asset_id, code, mode, name, status
		)
		VALUES (
			'cset_demo_score_o_001', $1, $2, 'cset-demo-score-o-001', 'score-o', '积分赛标准赛道', 'active'
		)
		ON CONFLICT (code) DO UPDATE SET
			place_id = EXCLUDED.place_id,
			map_asset_id = EXCLUDED.map_asset_id,
			mode = EXCLUDED.mode,
			name = EXCLUDED.name,
			status = EXCLUDED.status
		RETURNING id, course_set_public_id
	`, placeID, mapAssetID).Scan(&scoreOCourseSetID, &scoreOCourseSetPublicID); err != nil {
		return nil, fmt.Errorf("ensure score-o demo course set: %w", err)
	}

	var scoreOCourseVariantID, scoreOCourseVariantPublicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO course_variants (
			course_variant_public_id, course_set_id, source_id, name, route_code, mode, control_count, status, is_default
		)
		VALUES (
			'cvariant_demo_score_o_001', $1, $2, '积分赛主赛道', 'route-score-o-001', 'score-o', 10, 'active', true
		)
		ON CONFLICT (course_variant_public_id) DO UPDATE SET
			course_set_id = EXCLUDED.course_set_id,
			source_id = EXCLUDED.source_id,
			name = EXCLUDED.name,
			route_code = EXCLUDED.route_code,
			mode = EXCLUDED.mode,
			control_count = EXCLUDED.control_count,
			status = EXCLUDED.status,
			is_default = EXCLUDED.is_default
		RETURNING id, course_variant_public_id
	`, scoreOCourseSetID, courseSourceID).Scan(&scoreOCourseVariantID, &scoreOCourseVariantPublicID); err != nil {
		return nil, fmt.Errorf("ensure score-o demo course variant: %w", err)
	}

	if _, err := tx.Exec(ctx, `
		UPDATE course_sets
		SET current_variant_id = $2
		WHERE id = $1
	`, scoreOCourseSetID, scoreOCourseVariantID); err != nil {
		return nil, fmt.Errorf("attach score-o demo course variant: %w", err)
	}

	var scoreORuntimeBindingID, scoreORuntimeBindingPublicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO map_runtime_bindings (
			runtime_binding_public_id, event_id, place_id, map_asset_id, tile_release_id, course_set_id, course_variant_id, status, notes
		)
		VALUES (
			'runtime_demo_score_o_001', $1, $2, $3, $4, $5, $6, 'active', '积分赛联调运行绑定'
		)
		ON CONFLICT (runtime_binding_public_id) DO UPDATE SET
			event_id = EXCLUDED.event_id,
			place_id = EXCLUDED.place_id,
			map_asset_id = EXCLUDED.map_asset_id,
			tile_release_id = EXCLUDED.tile_release_id,
			course_set_id = EXCLUDED.course_set_id,
			course_variant_id = EXCLUDED.course_variant_id,
			status = EXCLUDED.status,
			notes = EXCLUDED.notes
		RETURNING id, runtime_binding_public_id
	`, scoreOEventID, placeID, mapAssetID, tileReleaseID, scoreOCourseSetID, scoreOCourseVariantID).Scan(&scoreORuntimeBindingID, &scoreORuntimeBindingPublicID); err != nil {
		return nil, fmt.Errorf("ensure score-o demo runtime binding: %w", err)
	}

	scoreOPresentationID, _, err := s.upsertDemoEventPresentation(ctx, tx, scoreOEventID, upsertDemoEventPresentationParams{
		PublicID:         "pres_demo_score_o_001",
		Code:             "event-detail-score-o",
		Name:             "积分赛详情展示",
		PresentationType: "detail",
		TemplateKey:      "event.detail.score-o",
		Version:          "v2026-04-07-score-o",
		Title:            "领秀城公园积分赛展示定义",
	})
	if err != nil {
		return nil, fmt.Errorf("ensure score-o presentation: %w", err)
	}
	scoreOBundleID, _, err := s.upsertDemoContentBundle(ctx, tx, scoreOEventID, upsertDemoContentBundleParams{
		PublicID:     "bundle_demo_score_o_001",
		Code:         "result-media-score-o",
		Name:         "积分赛结果内容包",
		BundleType:   "result_media",
		Version:      "v2026-04-07-score-o",
		ManifestURL:  "https://api.gotomars.xyz/dev/demo-assets/content-manifests/score-o",
		AssetRootURL: "https://oss-mbh5.colormaprun.com/gotomars/event/",
	})
	if err != nil {
		return nil, fmt.Errorf("ensure score-o content bundle: %w", err)
	}
	if err := s.SetEventDefaultBindings(ctx, tx, SetEventDefaultBindingsParams{
		EventID:            scoreOEventID,
		PresentationID:     &scoreOPresentationID,
		ContentBundleID:    &scoreOBundleID,
		RuntimeBindingID:   &scoreORuntimeBindingID,
		UpdatePresentation: true,
		UpdateContent:      true,
		UpdateRuntime:      true,
	}); err != nil {
		return nil, fmt.Errorf("set score-o default bindings: %w", err)
	}
	if err := s.SetEventReleaseBindings(ctx, tx, scoreOReleaseRow.ID, &scoreORuntimeBindingID, &scoreOPresentationID, &scoreOBundleID); err != nil {
		return nil, fmt.Errorf("bind score-o release defaults: %w", err)
	}

	var cleanedSessionCount int64
	if err := tx.QueryRow(ctx, `
		WITH cleaned AS (
			UPDATE game_sessions
			SET
				status = 'cancelled',
				ended_at = NOW(),
				updated_at = NOW()
			WHERE event_id = ANY($1::uuid[])
			  AND status IN ('launched', 'running')
			RETURNING 1
		)
		SELECT COUNT(*) FROM cleaned
	`, []string{eventID, scoreOEventID, manualEventID}).Scan(&cleanedSessionCount); err != nil {
		return nil, fmt.Errorf("cleanup demo ongoing sessions: %w", err)
	}

	if err := tx.Commit(ctx); err != nil {
		return nil, err
	}

	return &DemoBootstrapSummary{
		TenantCode:             "tenant_demo",
		ChannelCode:            "mini-demo",
		EventID:                "evt_demo_001",
		ReleaseID:              releaseRow.PublicID,
		SourceID:               source.ID,
		BuildID:                build.ID,
		CardID:                 cardPublicID,
		PlaceID:                placePublicID,
		MapAssetID:             mapAssetPublicID,
		TileReleaseID:          tileReleasePublicID,
		CourseSourceID:         courseSourcePublicID,
		CourseSetID:            courseSetPublicID,
		CourseVariantID:        courseVariantPublicID,
		RuntimeBindingID:       runtimeBindingPublicID,
		ScoreOEventID:          "evt_demo_score_o_001",
		ScoreOReleaseID:        scoreOReleaseRow.PublicID,
		ScoreOCardID:           scoreOCardPublicID,
		ScoreOSourceID:         scoreOSource.ID,
		ScoreOBuildID:          scoreOBuild.ID,
		ScoreOCourseSetID:      scoreOCourseSetPublicID,
		ScoreOCourseVariantID:  scoreOCourseVariantPublicID,
		ScoreORuntimeBindingID: scoreORuntimeBindingPublicID,
		VariantManualEventID:   "evt_demo_variant_manual_001",
		VariantManualRelease:   manualReleaseRow.PublicID,
		VariantManualCardID:    manualCardPublicID,
		VariantManualSourceID:  manualSource.ID,
		VariantManualBuildID:   manualBuild.ID,
		VariantManualCourseSet: manualCourseSetPublicID,
		VariantManualVariantID: manualVariantDPublicID,
		VariantManualRuntimeID: manualRuntimeBindingPublicID,
		CleanedSessionCount:    cleanedSessionCount,
	}, nil
}

type upsertDemoEventPresentationParams struct {
	PublicID         string
	Code             string
	Name             string
	PresentationType string
	TemplateKey      string
	Version          string
	Title            string
}

func (s *Store) upsertDemoEventPresentation(ctx context.Context, tx Tx, eventID string, params upsertDemoEventPresentationParams) (string, string, error) {
	var id string
	var publicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO event_presentations (
			presentation_public_id,
			event_id,
			code,
			name,
			presentation_type,
			status,
			is_default,
			schema_jsonb
		)
		VALUES ($1, $2, $3, $4, $5, 'active', true, $6::jsonb)
		ON CONFLICT (event_id, code) DO UPDATE SET
			presentation_public_id = EXCLUDED.presentation_public_id,
			name = EXCLUDED.name,
			presentation_type = EXCLUDED.presentation_type,
			status = EXCLUDED.status,
			is_default = EXCLUDED.is_default,
			schema_jsonb = EXCLUDED.schema_jsonb
		RETURNING id, presentation_public_id
	`, params.PublicID, eventID, params.Code, params.Name, params.PresentationType, fmt.Sprintf(`{"templateKey":"%s","version":"%s","title":"%s"}`, params.TemplateKey, params.Version, params.Title)).Scan(&id, &publicID); err != nil {
		return "", "", err
	}
	return id, publicID, nil
}

type upsertDemoContentBundleParams struct {
	PublicID     string
	Code         string
	Name         string
	BundleType   string
	Version      string
	ManifestURL  string
	AssetRootURL string
}

func (s *Store) upsertDemoContentBundle(ctx context.Context, tx Tx, eventID string, params upsertDemoContentBundleParams) (string, string, error) {
	var id string
	var publicID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO content_bundles (
			content_bundle_public_id,
			event_id,
			code,
			name,
			status,
			is_default,
			entry_url,
			asset_root_url,
			metadata_jsonb
		)
		VALUES ($1, $2, $3, $4, 'active', true, $5, $6, $7::jsonb)
		ON CONFLICT (event_id, code) DO UPDATE SET
			content_bundle_public_id = EXCLUDED.content_bundle_public_id,
			name = EXCLUDED.name,
			status = EXCLUDED.status,
			is_default = EXCLUDED.is_default,
			entry_url = EXCLUDED.entry_url,
			asset_root_url = EXCLUDED.asset_root_url,
			metadata_jsonb = EXCLUDED.metadata_jsonb
		RETURNING id, content_bundle_public_id
	`, params.PublicID, eventID, params.Code, params.Name, params.ManifestURL, params.AssetRootURL, fmt.Sprintf(`{"bundleType":"%s","version":"%s"}`, params.BundleType, params.Version)).Scan(&id, &publicID); err != nil {
		return "", "", err
	}
	return id, publicID, nil
}

func demoPreviewManifest(kind string) map[string]any {
	baseTiles := map[string]any{
		"tileBaseUrl": "https://oss-mbh5.colormaprun.com/gotomars/map/lxcb-001/tiles/",
		"zoom":        15,
		"tileSize":    256,
	}
	viewport := map[string]any{
		"width":  800,
		"height": 450,
		"minLon": 117.0000,
		"minLat": 36.6000,
		"maxLon": 117.0800,
		"maxLat": 36.6600,
	}

	switch kind {
	case "score-o":
		return map[string]any{
			"mode":              "readonly",
			"baseTiles":         baseTiles,
			"viewport":          viewport,
			"selectedVariantId": "variant_score_main",
			"variants": []map[string]any{
				{
					"variantId": "variant_score_main",
					"name":      "积分赛主赛道",
					"routeCode": "route-score-o-001",
					"controls": []map[string]any{
						{"id": "start", "kind": "start", "lon": 117.012, "lat": 36.612, "label": "起点"},
						{"id": "s1", "kind": "control", "lon": 117.021, "lat": 36.618, "label": "10分点"},
						{"id": "s2", "kind": "control", "lon": 117.034, "lat": 36.624, "label": "20分点"},
						{"id": "s3", "kind": "control", "lon": 117.046, "lat": 36.616, "label": "15分点"},
						{"id": "finish", "kind": "finish", "lon": 117.025, "lat": 36.606, "label": "终点"},
					},
					"legs": []map[string]any{
						{"from": "start", "to": "s1"},
						{"from": "s1", "to": "s2"},
						{"from": "s2", "to": "s3"},
						{"from": "s3", "to": "finish"},
					},
				},
			},
		}
	case "manual-variant":
		return map[string]any{
			"mode":              "readonly",
			"baseTiles":         baseTiles,
			"viewport":          viewport,
			"selectedVariantId": "variant_d",
			"variants": []map[string]any{
				{
					"variantId": "variant_a",
					"name":      "路线 01",
					"routeCode": "route-variant-a",
					"controls": []map[string]any{
						{"id": "1", "kind": "control", "lon": 117.000649296107, "lat": 36.5921631022497, "label": "1"},
						{"id": "2", "kind": "control", "lon": 116.999689737459, "lat": 36.5922740961347, "label": "2"},
						{"id": "3", "kind": "control", "lon": 116.999108309973, "lat": 36.5919019395375, "label": "3"},
						{"id": "4", "kind": "control", "lon": 116.999823913032, "lat": 36.591572220351, "label": "4"},
						{"id": "5", "kind": "control", "lon": 116.999860506371, "lat": 36.5912131186443, "label": "5"},
						{"id": "6", "kind": "control", "lon": 117.000340285695, "lat": 36.5909356298175, "label": "6"},
						{"id": "7", "kind": "control", "lon": 117.000441933857, "lat": 36.5915004001434, "label": "7"},
						{"id": "8", "kind": "control", "lon": 117.001397426578, "lat": 36.5915983367736, "label": "8"},
						{"id": "9", "kind": "control", "lon": 117.000665559813, "lat": 36.5919574366878, "label": "9"},
						{"id": "10", "kind": "control", "lon": 117.000649296107, "lat": 36.5921631022497, "label": "10"},
					},
					"legs": []map[string]any{
						{"from": "1", "to": "2"},
						{"from": "2", "to": "3"},
						{"from": "3", "to": "4"},
						{"from": "4", "to": "5"},
						{"from": "5", "to": "6"},
						{"from": "6", "to": "7"},
						{"from": "7", "to": "8"},
						{"from": "8", "to": "9"},
						{"from": "9", "to": "10"},
					},
				},
				{
					"variantId": "variant_b",
					"name":      "路线 02",
					"routeCode": "route-variant-b",
					"controls": []map[string]any{
						{"id": "1", "kind": "control", "lon": 117.000649296107, "lat": 36.5921631022497, "label": "1"},
						{"id": "2", "kind": "control", "lon": 116.999766990062, "lat": 36.5921141343085, "label": "2"},
						{"id": "3", "kind": "control", "lon": 116.999710067091, "lat": 36.5917615642144, "label": "3"},
						{"id": "4", "kind": "control", "lon": 116.998774904002, "lat": 36.5913306430232, "label": "4"},
						{"id": "5", "kind": "control", "lon": 116.998941606987, "lat": 36.5908278985923, "label": "5"},
						{"id": "6", "kind": "control", "lon": 117.00058830721, "lat": 36.5905340853955, "label": "6"},
						{"id": "7", "kind": "control", "lon": 117.000238637533, "lat": 36.5914742836876, "label": "7"},
						{"id": "8", "kind": "control", "lon": 117.000937976887, "lat": 36.5916113949816, "label": "8"},
						{"id": "9", "kind": "control", "lon": 117.000803801313, "lat": 36.5919411140006, "label": "9"},
						{"id": "10", "kind": "control", "lon": 117.000649296107, "lat": 36.5921631022497, "label": "10"},
					},
					"legs": []map[string]any{
						{"from": "1", "to": "2"},
						{"from": "2", "to": "3"},
						{"from": "3", "to": "4"},
						{"from": "4", "to": "5"},
						{"from": "5", "to": "6"},
						{"from": "6", "to": "7"},
						{"from": "7", "to": "8"},
						{"from": "8", "to": "9"},
						{"from": "9", "to": "10"},
					},
				},
				{
					"variantId": "variant_c",
					"name":      "路线 03",
					"routeCode": "route-variant-c",
					"controls": []map[string]any{
						{"id": "1", "kind": "control", "lon": 117.000649296107, "lat": 36.5921631022497, "label": "1"},
						{"id": "2", "kind": "control", "lon": 117.000665559813, "lat": 36.5919574366878, "label": "2"},
						{"id": "3", "kind": "control", "lon": 117.001397426578, "lat": 36.5915983367736, "label": "3"},
						{"id": "4", "kind": "control", "lon": 117.000441933857, "lat": 36.5915004001434, "label": "4"},
						{"id": "5", "kind": "control", "lon": 117.000340285695, "lat": 36.5909356298175, "label": "5"},
						{"id": "6", "kind": "control", "lon": 116.999860506371, "lat": 36.5912131186443, "label": "6"},
						{"id": "7", "kind": "control", "lon": 116.999823913032, "lat": 36.591572220351, "label": "7"},
						{"id": "8", "kind": "control", "lon": 116.999108309973, "lat": 36.5919019395375, "label": "8"},
						{"id": "9", "kind": "control", "lon": 116.999689737459, "lat": 36.5922740961347, "label": "9"},
						{"id": "10", "kind": "control", "lon": 117.000649296107, "lat": 36.5921631022497, "label": "10"},
					},
					"legs": []map[string]any{
						{"from": "1", "to": "2"},
						{"from": "2", "to": "3"},
						{"from": "3", "to": "4"},
						{"from": "4", "to": "5"},
						{"from": "5", "to": "6"},
						{"from": "6", "to": "7"},
						{"from": "7", "to": "8"},
						{"from": "8", "to": "9"},
						{"from": "9", "to": "10"},
					},
				},
				{
					"variantId": "variant_d",
					"name":      "路线 04",
					"routeCode": "route-variant-d",
					"controls": []map[string]any{
						{"id": "1", "kind": "control", "lon": 117.000649296107, "lat": 36.5921631022497, "label": "1"},
						{"id": "2", "kind": "control", "lon": 117.000803801313, "lat": 36.5919411140006, "label": "2"},
						{"id": "3", "kind": "control", "lon": 117.000937976887, "lat": 36.5916113949816, "label": "3"},
						{"id": "4", "kind": "control", "lon": 117.000238637533, "lat": 36.5914742836876, "label": "4"},
						{"id": "5", "kind": "control", "lon": 117.00058830721, "lat": 36.5905340853955, "label": "5"},
						{"id": "6", "kind": "control", "lon": 116.998941606987, "lat": 36.5908278985923, "label": "6"},
						{"id": "7", "kind": "control", "lon": 116.998774904002, "lat": 36.5913306430232, "label": "7"},
						{"id": "8", "kind": "control", "lon": 116.999710067091, "lat": 36.5917615642144, "label": "8"},
						{"id": "9", "kind": "control", "lon": 116.999766990062, "lat": 36.5921141343085, "label": "9"},
						{"id": "10", "kind": "control", "lon": 117.000649296107, "lat": 36.5921631022497, "label": "10"},
					},
					"legs": []map[string]any{
						{"from": "1", "to": "2"},
						{"from": "2", "to": "3"},
						{"from": "3", "to": "4"},
						{"from": "4", "to": "5"},
						{"from": "5", "to": "6"},
						{"from": "6", "to": "7"},
						{"from": "7", "to": "8"},
						{"from": "8", "to": "9"},
						{"from": "9", "to": "10"},
					},
				},
			},
		}
	default:
		return map[string]any{
			"mode":              "readonly",
			"baseTiles":         baseTiles,
			"viewport":          viewport,
			"selectedVariantId": "variant_classic_main",
			"variants": []map[string]any{
				{
					"variantId": "variant_classic_main",
					"name":      "顺序赛主赛道",
					"routeCode": "route-demo-a",
					"controls": []map[string]any{
						{"id": "start", "kind": "start", "lon": 117.010, "lat": 36.610, "label": "起点"},
						{"id": "c1", "kind": "control", "lon": 117.018, "lat": 36.615, "label": "1"},
						{"id": "c2", "kind": "control", "lon": 117.027, "lat": 36.621, "label": "2"},
						{"id": "c3", "kind": "control", "lon": 117.036, "lat": 36.627, "label": "3"},
						{"id": "finish", "kind": "finish", "lon": 117.044, "lat": 36.632, "label": "终点"},
					},
					"legs": []map[string]any{
						{"from": "start", "to": "c1"},
						{"from": "c1", "to": "c2"},
						{"from": "c2", "to": "c3"},
						{"from": "c3", "to": "finish"},
					},
				},
			},
		}
	}
}
