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
		)
		VALUES ($1, 'evt_demo_001', 'city-park-classic', '领秀城公园顺序赛', '顺序赛联调样例活动', 'active')
		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
		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
		)
		VALUES (
			'rel_demo_001',
			$1,
			1,
			'顺序赛联调配置 v1',
			'https://oss-mbh5.colormaprun.com/gotomars/event/releases/evt_demo_001/rel_e7dd953743c5c0d2/manifest.json',
			'demo-checksum-001',
			'route-demo-001',
			'published'
		)
		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
		RETURNING id, release_public_id
	`, eventID).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",
			},
			"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",
			},
			"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",
			},
			"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://oss-mbh5.colormaprun.com/gotomars/event/releases/evt_demo_001/rel_e7dd953743c5c0d2/manifest.json",
			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/10/c02.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 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)
	}

	var manualEventID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO events (
			tenant_id, event_public_id, slug, display_name, summary, status
		)
		VALUES ($1, 'evt_demo_variant_manual_001', 'city-park-manual-variant', '领秀城公园多赛道挑战', '手动多赛道联调样例活动', 'active')
		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
		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://oss-mbh5.colormaprun.com/gotomars/event/releases/evt_demo_001/rel_e7dd953743c5c0d2/manifest.json',
			'demo-variant-checksum-001',
			'route-variant-a',
			'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, `{
  "play": {
    "assignmentMode": "manual",
    "courseVariants": [
      {
        "id": "variant_a",
        "name": "A 线",
        "description": "短线体验版（c01.kml）",
        "routeCode": "route-variant-a",
        "selectable": true
      },
      {
        "id": "variant_b",
        "name": "B 线",
        "description": "长线挑战版（c02.kml）",
        "routeCode": "route-variant-b",
        "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",
			},
			"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":        "A 线",
						"description": "短线体验版（c01.kml）",
						"routeCode":   "route-variant-a",
						"selectable":  true,
					},
					{
						"id":          "variant_b",
						"name":        "B 线",
						"description": "长线挑战版（c02.kml）",
						"routeCode":   "route-variant-b",
						"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",
			},
			"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",
			},
			"play": map[string]any{
				"assignmentMode": "manual",
				"courseVariants": []map[string]any{
					{
						"id":          "variant_a",
						"name":        "A 线",
						"description": "短线体验版（c01.kml）",
						"routeCode":   "route-variant-a",
						"selectable":  true,
					},
					{
						"id":          "variant_b",
						"name":        "B 线",
						"description": "长线挑战版（c02.kml）",
						"routeCode":   "route-variant-b",
						"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 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, '多赛道 A 线', 'route-variant-a', 'classic-sequential', 8, '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, courseSourceID).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, '多赛道 B 线', 'route-variant-b', '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, courseSourceVariantBID).Scan(&manualVariantBID, &manualVariantBPublicID); err != nil {
		return nil, fmt.Errorf("ensure variant manual demo variant b: %w", err)
	}

	if _, err := tx.Exec(ctx, `
		UPDATE course_sets
		SET current_variant_id = $2
		WHERE id = $1
	`, manualCourseSetID, manualVariantBID); 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, manualVariantBID).Scan(&manualRuntimeBindingID, &manualRuntimeBindingPublicID); err != nil {
		return nil, fmt.Errorf("ensure variant manual demo runtime binding: %w", err)
	}

	var scoreOEventID string
	if err := tx.QueryRow(ctx, `
		INSERT INTO events (
			tenant_id, event_public_id, slug, display_name, summary, status
		)
		VALUES ($1, 'evt_demo_score_o_001', 'city-park-score-o', '领秀城公园积分赛', '积分赛联调样例活动', 'active')
		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
		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
		)
		VALUES (
			'rel_demo_score_o_001',
			$1,
			1,
			'积分赛联调配置 v1',
			'https://oss-mbh5.colormaprun.com/gotomars/event/score-o.json',
			'demo-score-o-checksum-001',
			'route-score-o-001',
			'published'
		)
		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
		RETURNING id, release_public_id
	`, scoreOEventID).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",
			},
			"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",
			},
			"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)
	}

	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: manualVariantBPublicID,
		VariantManualRuntimeID: manualRuntimeBindingPublicID,
		CleanedSessionCount:    cleanedSessionCount,
	}, nil
}
