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"`
	VariantManualEventID string `json:"variantManualEventId"`
	VariantManualRelease string `json:"variantManualReleaseId"`
	VariantManualCardID  string `json:"variantManualCardId"`
	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', 'Demo Tenant', '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', 'Demo Mini Channel', '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', 'demo-city-run', 'Demo City Run', 'Launch flow demo event', '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,
			'Demo Config 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 := "demo source config imported from local event sample"
	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 := "demo build generated from sample classic-sequential.json"
	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
		)
		VALUES (
			'card_demo_001',
			$1,
			$2,
			'event',
			'Demo City Run',
			'今日推荐路线',
			'https://oss-mbh5.colormaprun.com/gotomars/assets/demo-cover.jpg',
			$3,
			'home_primary',
			100,
			'active'
		)
		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
		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', 'Demo Park', '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', 'Demo Asset Map', '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://example.com/tiles/demo/', 'https://example.com/tiles/demo/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://example.com/course/demo.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 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', 'Demo Course Set', '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, 'Demo Variant 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)
	}

	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', 'demo runtime binding'
		)
		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', 'demo-variant-manual-run', 'Demo Variant Manual Run', 'Manual 多赛道联调活动', '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,
			'Demo Variant Manual Config 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": "短线体验版",
        "routeCode": "route-variant-a",
        "selectable": true
      },
      {
        "id": "variant_b",
        "name": "B 线",
        "description": "长线挑战版",
        "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
		)
		VALUES (
			'card_demo_variant_manual_001',
			$1,
			$2,
			'event',
			'Demo Variant Manual Run',
			'多赛道手动选择联调',
			'https://oss-mbh5.colormaprun.com/gotomars/assets/demo-cover.jpg',
			$3,
			'home_primary',
			95,
			'active'
		)
		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
		RETURNING card_public_id
	`, tenantID, channelID, manualEventID).Scan(&manualCardPublicID); err != nil {
		return nil, fmt.Errorf("ensure variant manual demo card: %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, 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,
		VariantManualEventID: "evt_demo_variant_manual_001",
		VariantManualRelease: manualReleaseRow.PublicID,
		VariantManualCardID:  manualCardPublicID,
		CleanedSessionCount:  cleanedSessionCount,
	}, nil
}
