/**
 * Demo community seeder for the r* ecosystem
 *
 * Creates/ensures a "demo" community with visibility: "public" (anon read+write)
 * and populates it with the "Alpine Explorer 2026" cross-app scenario.
 *
 * Shape IDs are deterministic so re-running is idempotent.
 */

import {
	addShapes,
	communityExists,
	createCommunity,
	getDocumentData,
	loadCommunity,
} from "./community-store";

// ── Alpine Explorer 2026 — Demo Scenario ──────────────────────────

const DEMO_SHAPES: Record<string, unknown>[] = [
	// ─── rTrips: Itinerary ──────────────────────────────────────
	{
		id: "demo-itinerary-alpine",
		type: "folk-itinerary",
		x: 50, y: 50, width: 400, height: 300, rotation: 0,
		tripTitle: "Alpine Explorer 2026",
		startDate: "2026-07-06",
		endDate: "2026-07-20",
		travelers: ["Maya", "Liam", "Priya", "Omar"],
		items: [
			{ date: "Jul 6", activity: "Fly Geneva → Chamonix shuttle", category: "travel" },
			{ date: "Jul 7", activity: "Acclimatization hike — Lac Blanc", category: "hike" },
			{ date: "Jul 8", activity: "Via Ferrata — Aiguille du Midi", category: "adventure" },
			{ date: "Jul 9", activity: "Rest day / Chamonix town", category: "rest" },
			{ date: "Jul 10", activity: "Train to Zermatt", category: "travel" },
			{ date: "Jul 11", activity: "Gornergrat sunrise hike", category: "hike" },
			{ date: "Jul 12", activity: "Matterhorn base camp trek", category: "hike" },
			{ date: "Jul 13", activity: "Paragliding over Zermatt", category: "adventure" },
			{ date: "Jul 14", activity: "Transfer to Dolomites", category: "travel" },
			{ date: "Jul 15", activity: "Tre Cime di Lavaredo loop", category: "hike" },
			{ date: "Jul 16", activity: "Lago di Braies kayaking", category: "adventure" },
			{ date: "Jul 17", activity: "Seceda ridgeline hike", category: "hike" },
			{ date: "Jul 18", activity: "Cooking class in Bolzano", category: "culture" },
			{ date: "Jul 19", activity: "Free day — shopping & packing", category: "rest" },
			{ date: "Jul 20", activity: "Fly home from Innsbruck", category: "travel" },
		],
	},

	// ─── rTrips: Destinations ───────────────────────────────────
	{
		id: "demo-dest-chamonix",
		type: "folk-destination",
		x: 500, y: 50, width: 300, height: 200, rotation: 0,
		destName: "Chamonix",
		country: "France",
		lat: 45.9237,
		lng: 6.8694,
		arrivalDate: "2026-07-06",
		departureDate: "2026-07-10",
		notes: "Base for Mont Blanc region. Book Aiguille du Midi tickets in advance.",
	},
	{
		id: "demo-dest-zermatt",
		type: "folk-destination",
		x: 500, y: 280, width: 300, height: 200, rotation: 0,
		destName: "Zermatt",
		country: "Switzerland",
		lat: 46.0207,
		lng: 7.7491,
		arrivalDate: "2026-07-10",
		departureDate: "2026-07-14",
		notes: "Car-free village. Take the Glacier Express for the scenic route.",
	},
	{
		id: "demo-dest-dolomites",
		type: "folk-destination",
		x: 500, y: 510, width: 300, height: 200, rotation: 0,
		destName: "Dolomites",
		country: "Italy",
		lat: 46.4102,
		lng: 11.8440,
		arrivalDate: "2026-07-14",
		departureDate: "2026-07-20",
		notes: "Stay in Val Gardena. Rifugio Locatelli for Tre Cime views.",
	},

	// ─── rNotes: Notebook ───────────────────────────────────────
	{
		id: "demo-notebook-trip",
		type: "folk-notebook",
		x: 850, y: 50, width: 300, height: 180, rotation: 0,
		notebookTitle: "Alpine Explorer Planning",
		description: "Shared knowledge base for our July 2026 trip across France, Switzerland, and Italy",
		noteCount: 5,
		collaborators: ["Maya", "Liam", "Priya", "Omar"],
	},

	// ─── rNotes: Notes ──────────────────────────────────────────
	{
		id: "demo-note-packing",
		type: "folk-note",
		x: 850, y: 260, width: 300, height: 250, rotation: 0,
		noteTitle: "Packing Checklist",
		content: "## Essential Gear\n- [ ] Hiking boots (broken in!)\n- [x] Rain jacket\n- [x] Headlamp + batteries\n- [ ] Trekking poles\n- [x] First aid kit\n- [ ] Sunscreen SPF 50\n- [ ] Water filter\n\n## Documents\n- [x] Passports\n- [ ] Travel insurance\n- [x] Hut reservations printout",
		tags: ["packing", "gear", "checklist"],
		editor: "Maya",
		editedAt: "2 hours ago",
	},
	{
		id: "demo-note-gear",
		type: "folk-note",
		x: 1180, y: 50, width: 300, height: 200, rotation: 0,
		noteTitle: "Gear Research",
		content: "## Via Ferrata Kit\nNeed harness + lanyard + helmet. Can rent in Chamonix for ~€25/day.\n\n## Paragliding\nTandem flights in Zermatt: ~€180pp. Book at Paragliding Zermatt (best reviews).\n\n## Camera\nBring the drone for Tre Cime. Check Italian drone regulations!",
		tags: ["gear", "research", "equipment"],
		editor: "Liam",
		editedAt: "5 hours ago",
	},
	{
		id: "demo-note-huts",
		type: "folk-note",
		x: 1180, y: 280, width: 300, height: 200, rotation: 0,
		noteTitle: "Mountain Hut Reservations",
		content: "## Confirmed\n- **Rifugio Locatelli** (Jul 15): 4 beds, conf #TRE2026-089\n- **Refuge du Lac Blanc** (Jul 7): 4 beds, conf #LB2026-234\n\n## Pending\n- Hörnlihütte (Matterhorn base): Waitlisted for Jul 12",
		tags: ["accommodation", "reservations", "logistics"],
		editor: "Priya",
		editedAt: "Yesterday",
	},
	{
		id: "demo-note-emergency",
		type: "folk-note",
		x: 1180, y: 510, width: 300, height: 180, rotation: 0,
		noteTitle: "Emergency Contacts",
		content: "## Emergency Numbers\n- **France**: 112 (EU), PGHM: +33 4 50 53 16 89\n- **Switzerland**: 1414 (REGA air rescue)\n- **Italy**: 118 (medical), 112 (general)\n\n## Insurance\nPolicy #: WA-2026-7891\nEmergency line: +1-800-555-0199",
		tags: ["safety", "emergency", "contacts"],
		editor: "Omar",
		editedAt: "3 days ago",
	},
	{
		id: "demo-note-photos",
		type: "folk-note",
		x: 850, y: 540, width: 300, height: 180, rotation: 0,
		noteTitle: "Photo Spots",
		content: "## Must-Capture Locations\n1. Lac Blanc reflection of Mont Blanc (sunrise)\n2. Gornergrat panorama with Matterhorn\n3. Tre Cime from Rifugio Locatelli (golden hour)\n4. Seceda ridgeline drone shots\n5. Lago di Braies turquoise water\n\nBring ND filters for long exposure water shots.",
		tags: ["photography", "planning", "creative"],
		editor: "Liam",
		editedAt: "2 days ago",
	},

	// ─── rVote: Polls ───────────────────────────────────────────
	{
		id: "demo-poll-day5",
		type: "demo-poll",
		x: 50, y: 400, width: 350, height: 200, rotation: 0,
		question: "Day 5 Activity — What should we do in Chamonix?",
		options: [
			{ label: "Via Ferrata at Aiguille du Midi", votes: 7 },
			{ label: "Kayaking on Lac d'Annecy", votes: 3 },
			{ label: "Rest day in town", votes: 2 },
		],
		totalVoters: 4,
		status: "active",
		endsAt: "2026-07-01",
	},
	{
		id: "demo-poll-dinner",
		type: "demo-poll",
		x: 50, y: 630, width: 350, height: 200, rotation: 0,
		question: "First night dinner in Zermatt?",
		options: [
			{ label: "Traditional fondue at Chez Vrony", votes: 5 },
			{ label: "Pizza at Grampi's", votes: 4 },
			{ label: "Cook at the Airbnb", votes: 1 },
		],
		totalVoters: 4,
		status: "active",
		endsAt: "2026-07-05",
	},

	// ─── rCart: Group Shopping Items ─────────────────────────────
	{
		id: "demo-cart-firstaid",
		type: "demo-cart-item",
		x: 50, y: 870, width: 320, height: 60, rotation: 0,
		name: "Adventure First-Aid Kit",
		price: 85.00,
		funded: 85.00,
		status: "Funded",
		requestedBy: "Omar",
		store: "REI",
	},
	{
		id: "demo-cart-waterfilter",
		type: "demo-cart-item",
		x: 50, y: 940, width: 320, height: 60, rotation: 0,
		name: "Portable Water Filter (Sawyer Squeeze)",
		price: 45.00,
		funded: 45.00,
		status: "Funded",
		requestedBy: "Maya",
		store: "Amazon",
	},
	{
		id: "demo-cart-bearcan",
		type: "demo-cart-item",
		x: 50, y: 1010, width: 320, height: 60, rotation: 0,
		name: "Bear Canister 2x (BV500)",
		price: 120.00,
		funded: 80.00,
		status: "In Cart",
		requestedBy: "Liam",
		store: "REI",
	},
	{
		id: "demo-cart-stove",
		type: "demo-cart-item",
		x: 50, y: 1080, width: 320, height: 60, rotation: 0,
		name: "Camp Stove + Fuel Canister",
		price: 65.00,
		funded: 65.00,
		status: "Funded",
		requestedBy: "Priya",
		store: "Decathlon",
	},
	{
		id: "demo-cart-drone",
		type: "demo-cart-item",
		x: 50, y: 1150, width: 320, height: 60, rotation: 0,
		name: "DJI Mini 4 Pro Rental (2 weeks)",
		price: 350.00,
		funded: 100.00,
		status: "Needs Funding",
		requestedBy: "Liam",
		store: "LensRentals",
	},
	{
		id: "demo-cart-starlink",
		type: "demo-cart-item",
		x: 50, y: 1220, width: 320, height: 60, rotation: 0,
		name: "Starlink Mini Rental (2 weeks)",
		price: 200.00,
		funded: 50.00,
		status: "Needs Funding",
		requestedBy: "Omar",
		store: "StarRent.eu",
	},

	// ─── rFlows: Expenses ───────────────────────────────────────
	{
		id: "demo-expense-shuttle",
		type: "demo-expense",
		x: 400, y: 870, width: 320, height: 60, rotation: 0,
		description: "Geneva → Chamonix shuttle (4 pax)",
		amount: 186.00,
		currency: "EUR",
		paidBy: "Maya",
		split: "equal",
		category: "transport",
		date: "2026-07-06",
	},
	{
		id: "demo-expense-hut",
		type: "demo-expense",
		x: 400, y: 940, width: 320, height: 60, rotation: 0,
		description: "Mountain hut reservations (3 nights total)",
		amount: 420.00,
		currency: "EUR",
		paidBy: "Priya",
		split: "equal",
		category: "accommodation",
		date: "2026-07-07",
	},
	{
		id: "demo-expense-viaferrata",
		type: "demo-expense",
		x: 400, y: 1010, width: 320, height: 60, rotation: 0,
		description: "Via Ferrata gear rental (4 sets)",
		amount: 144.00,
		currency: "EUR",
		paidBy: "Liam",
		split: "equal",
		category: "activity",
		date: "2026-07-08",
	},
	{
		id: "demo-expense-groceries",
		type: "demo-expense",
		x: 400, y: 1080, width: 320, height: 60, rotation: 0,
		description: "Groceries — Chamonix Carrefour",
		amount: 93.00,
		currency: "EUR",
		paidBy: "Omar",
		split: "equal",
		category: "food",
		date: "2026-07-06",
	},
	{
		id: "demo-expense-paragliding",
		type: "demo-expense",
		x: 400, y: 1150, width: 320, height: 60, rotation: 0,
		description: "Paragliding deposit (2 of 4 booked)",
		amount: 360.00,
		currency: "CHF",
		paidBy: "Maya",
		split: "custom",
		category: "activity",
		date: "2026-07-13",
	},

	// ─── rFlows: Budget ─────────────────────────────────────────
	{
		id: "demo-budget-trip",
		type: "folk-budget",
		x: 400, y: 400, width: 350, height: 250, rotation: 0,
		budgetTitle: "Trip Budget",
		currency: "EUR",
		budgetTotal: 4000,
		spent: 1203,
		categories: [
			{ name: "Transport", budget: 800, spent: 186 },
			{ name: "Accommodation", budget: 1200, spent: 420 },
			{ name: "Activities", budget: 1000, spent: 504 },
			{ name: "Food", budget: 600, spent: 93 },
			{ name: "Gear", budget: 400, spent: 0 },
		],
	},

	// ─── rMaps: Location Markers ────────────────────────────────
	{
		id: "demo-map-chamonix",
		type: "demo-map-marker",
		x: 750, y: 750, width: 40, height: 40, rotation: 0,
		name: "Chamonix",
		lat: 45.9237,
		lng: 6.8694,
		emoji: "🏔️",
		category: "destination",
		status: "Jul 6-10",
	},
	{
		id: "demo-map-zermatt",
		type: "demo-map-marker",
		x: 800, y: 750, width: 40, height: 40, rotation: 0,
		name: "Zermatt",
		lat: 46.0207,
		lng: 7.7491,
		emoji: "⛷️",
		category: "destination",
		status: "Jul 10-14",
	},
	{
		id: "demo-map-dolomites",
		type: "demo-map-marker",
		x: 850, y: 750, width: 40, height: 40, rotation: 0,
		name: "Dolomites",
		lat: 46.4102,
		lng: 11.8440,
		emoji: "🏞️",
		category: "destination",
		status: "Jul 14-20",
	},
	{
		id: "demo-map-lacblanc",
		type: "demo-map-marker",
		x: 900, y: 750, width: 40, height: 40, rotation: 0,
		name: "Lac Blanc Hike",
		lat: 45.9785,
		lng: 6.8891,
		emoji: "🥾",
		category: "activity",
		status: "Jul 7",
	},
	{
		id: "demo-map-viaferrata",
		type: "demo-map-marker",
		x: 950, y: 750, width: 40, height: 40, rotation: 0,
		name: "Via Ferrata",
		lat: 45.8786,
		lng: 6.8874,
		emoji: "🧗",
		category: "activity",
		status: "Jul 8",
	},
	{
		id: "demo-map-trecime",
		type: "demo-map-marker",
		x: 1000, y: 750, width: 40, height: 40, rotation: 0,
		name: "Tre Cime Loop",
		lat: 46.6190,
		lng: 12.3018,
		emoji: "🏔️",
		category: "activity",
		status: "Jul 15",
	},

	// ─── rNotes: Packing List (shared with rTrips) ──────────────
	{
		id: "demo-packing-alpine",
		type: "folk-packing-list",
		x: 1200, y: 750, width: 300, height: 300, rotation: 0,
		listTitle: "Alpine Explorer Packing",
		items: [
			{ name: "Hiking boots (broken in)", packed: true, category: "footwear" },
			{ name: "Rain jacket", packed: true, category: "clothing" },
			{ name: "Trekking poles", packed: false, category: "gear" },
			{ name: "Headlamp + batteries", packed: true, category: "gear" },
			{ name: "Sunscreen SPF 50", packed: false, category: "personal" },
			{ name: "Water filter", packed: false, category: "gear" },
			{ name: "First aid kit", packed: true, category: "safety" },
			{ name: "Passport + insurance", packed: true, category: "documents" },
		],
	},

	// ─── rTokens: Governance Token ─────────────────────────────
	{
		id: "demo-mint-gov",
		type: "folk-token-mint",
		x: 1550, y: 50, width: 320, height: 280, rotation: 0,
		tokenName: "Governance Token",
		tokenSymbol: "GOV",
		description: "Equal voting weight for group decisions. Each holder gets one share of governance power.",
		totalSupply: 100,
		issuedSupply: 100,
		tokenColor: "#6d28d9",
		tokenIcon: "🗳️",
		createdBy: "Maya",
		createdAt: "2026-06-15T10:00:00.000Z",
	},
	{
		id: "demo-ledger-gov",
		type: "folk-token-ledger",
		x: 1940, y: 50, width: 380, height: 400, rotation: 0,
		mintId: "demo-mint-gov",
		entries: [
			{ id: "gov-1", holder: "did:key:maya", holderLabel: "Maya", amount: 25, issuedAt: "2026-06-15T10:00:00.000Z", issuedBy: "Maya", memo: "Founding member" },
			{ id: "gov-2", holder: "did:key:liam", holderLabel: "Liam", amount: 25, issuedAt: "2026-06-15T10:00:00.000Z", issuedBy: "Maya", memo: "Founding member" },
			{ id: "gov-3", holder: "did:key:priya", holderLabel: "Priya", amount: 25, issuedAt: "2026-06-15T10:00:00.000Z", issuedBy: "Maya", memo: "Founding member" },
			{ id: "gov-4", holder: "did:key:omar", holderLabel: "Omar", amount: 25, issuedAt: "2026-06-15T10:00:00.000Z", issuedBy: "Maya", memo: "Founding member" },
		],
	},
	{
		id: "demo-arrow-gov",
		type: "folk-arrow",
		x: 0, y: 0, width: 0, height: 0, rotation: 0,
		sourceId: "demo-mint-gov",
		targetId: "demo-ledger-gov",
		color: "#6d28d9",
	},

	// ─── rTokens: Contribution Credits ─────────────────────────
	{
		id: "demo-mint-cred",
		type: "folk-token-mint",
		x: 1550, y: 500, width: 320, height: 280, rotation: 0,
		tokenName: "Contribution Credits",
		tokenSymbol: "CRED",
		description: "Earned through contributions to trip planning. Redeemable for activity priority picks.",
		totalSupply: 500,
		issuedSupply: 155,
		tokenColor: "#059669",
		tokenIcon: "⭐",
		createdBy: "Maya",
		createdAt: "2026-06-20T14:00:00.000Z",
	},
	{
		id: "demo-ledger-cred",
		type: "folk-token-ledger",
		x: 1940, y: 500, width: 380, height: 400, rotation: 0,
		mintId: "demo-mint-cred",
		entries: [
			{ id: "cred-1", holder: "did:key:maya", holderLabel: "Maya", amount: 50, issuedAt: "2026-06-20T14:00:00.000Z", issuedBy: "Maya", memo: "Organized itinerary" },
			{ id: "cred-2", holder: "did:key:liam", holderLabel: "Liam", amount: 40, issuedAt: "2026-06-22T09:00:00.000Z", issuedBy: "Maya", memo: "Gear research & drone rental" },
			{ id: "cred-3", holder: "did:key:priya", holderLabel: "Priya", amount: 35, issuedAt: "2026-06-25T11:00:00.000Z", issuedBy: "Maya", memo: "Hut reservations" },
			{ id: "cred-4", holder: "omar@example.com", holderLabel: "omar", amount: 30, issuedAt: "2026-06-28T16:00:00.000Z", issuedBy: "Maya", memo: "Emergency planning (escrow)" },
		],
	},
	{
		id: "demo-arrow-cred",
		type: "folk-arrow",
		x: 0, y: 0, width: 0, height: 0, rotation: 0,
		sourceId: "demo-mint-cred",
		targetId: "demo-ledger-cred",
		color: "#059669",
	},

	// ─── rFiles: Shared Documents ──────────────────────────────
	{
		id: "demo-file-permits",
		type: "folk-file",
		x: 1550, y: 900, width: 280, height: 80, rotation: 0,
		fileName: "Swiss Hiking Permits.pdf",
		fileSize: "2.4 MB",
		mimeType: "application/pdf",
		uploadedBy: "Priya",
		uploadedAt: "2026-06-28",
		tags: ["permits", "switzerland", "logistics"],
	},
	{
		id: "demo-file-insurance",
		type: "folk-file",
		x: 1550, y: 1000, width: 280, height: 80, rotation: 0,
		fileName: "Travel Insurance Policy.pdf",
		fileSize: "1.1 MB",
		mimeType: "application/pdf",
		uploadedBy: "Omar",
		uploadedAt: "2026-06-25",
		tags: ["insurance", "emergency", "documents"],
	},
	{
		id: "demo-file-topo",
		type: "folk-file",
		x: 1550, y: 1100, width: 280, height: 80, rotation: 0,
		fileName: "Tre Cime Topo Map.gpx",
		fileSize: "340 KB",
		mimeType: "application/gpx+xml",
		uploadedBy: "Liam",
		uploadedAt: "2026-07-01",
		tags: ["maps", "hiking", "dolomites"],
	},

	// ─── rForum: Discussion Threads ────────────────────────────
	{
		id: "demo-forum-weather",
		type: "folk-forum-thread",
		x: 1870, y: 900, width: 320, height: 160, rotation: 0,
		threadTitle: "Weather Contingency Plan",
		author: "Omar",
		createdAt: "2026-06-30",
		replyCount: 8,
		lastReply: "Maya — 2 hours ago",
		preview: "What's our plan if the Matterhorn trek gets rained out? I found a cheese museum in Zermatt as backup...",
		tags: ["planning", "weather"],
	},
	{
		id: "demo-forum-transport",
		type: "folk-forum-thread",
		x: 1870, y: 1080, width: 320, height: 140, rotation: 0,
		threadTitle: "Train vs Rental Car — Dolomites Transfer",
		author: "Liam",
		createdAt: "2026-06-28",
		replyCount: 5,
		lastReply: "Priya — yesterday",
		preview: "The Bernina Express is scenic but 6 hours. Rental car is 3.5 hours. Thoughts?",
		tags: ["transport", "logistics"],
	},

	// ─── rBooks: Shared Reading ────────────────────────────────
	{
		id: "demo-book-alpine",
		type: "folk-book",
		x: 2350, y: 50, width: 280, height: 200, rotation: 0,
		bookTitle: "The Alps: A Human History",
		author: "Stephen O'Shea",
		coverColor: "#7c3aed",
		pageCount: 320,
		currentPage: 145,
		readers: ["Maya", "Liam"],
		status: "reading",
	},
	{
		id: "demo-book-wild",
		type: "folk-book",
		x: 2350, y: 280, width: 280, height: 200, rotation: 0,
		bookTitle: "Wild: A Journey from Lost to Found",
		author: "Cheryl Strayed",
		coverColor: "#059669",
		pageCount: 315,
		currentPage: 315,
		readers: ["Priya"],
		status: "finished",
	},

	// ─── rPubs: Published Artifacts ────────────────────────────
	{
		id: "demo-pub-zine",
		type: "folk-pub",
		x: 2350, y: 520, width: 300, height: 180, rotation: 0,
		pubTitle: "Alpine Explorer Zine",
		pubType: "zine",
		creator: "Maya",
		format: "A5 risograph",
		status: "in-production",
		copies: 50,
		price: 12.00,
		currency: "EUR",
		description: "Photo zine documenting the Alpine Explorer 2026 trip. Risograph printed on recycled paper.",
	},

	// ─── rSwag: Merchandise ────────────────────────────────────
	{
		id: "demo-swag-tee",
		type: "folk-swag",
		x: 2350, y: 730, width: 280, height: 120, rotation: 0,
		swagTitle: "Alpine Explorer 2026 Tee",
		swagType: "t-shirt",
		designer: "Liam",
		sizes: ["S", "M", "L", "XL"],
		price: 28.00,
		currency: "EUR",
		status: "available",
		orderCount: 12,
	},

	// ─── rProviders: Local Production ──────────────────────────
	{
		id: "demo-provider-risograph",
		type: "folk-provider",
		x: 2350, y: 880, width: 300, height: 160, rotation: 0,
		providerName: "Chamonix Print Collective",
		location: "Chamonix, France",
		capabilities: ["risograph", "screen-print", "letterpress"],
		substrates: ["recycled paper", "card stock", "cotton"],
		turnaround: "5-7 days",
		rating: 4.8,
		ordersFulfilled: 127,
	},

	// ─── rTasks: Task Board ─────────────────────────────────────
	{
		id: "demo-tasks-board",
		type: "folk-tasks-board",
		x: 750, y: 1350, width: 500, height: 280, rotation: 0,
		boardTitle: "Trip Preparation Tasks",
		columns: [
			{
				name: "To Do",
				tasks: [
					{ title: "Book paragliding (2 remaining spots)", assignee: "Liam", priority: "high" },
					{ title: "Buy trekking poles", assignee: "Maya", priority: "medium" },
				],
			},
			{
				name: "In Progress",
				tasks: [
					{ title: "Research Italian drone regulations", assignee: "Liam", priority: "high" },
					{ title: "First aid training refresher", assignee: "Omar", priority: "medium" },
				],
			},
			{
				name: "Done",
				tasks: [
					{ title: "Book hut reservations", assignee: "Priya", priority: "high" },
					{ title: "Get travel insurance", assignee: "Omar", priority: "high" },
					{ title: "Break in hiking boots", assignee: "Maya", priority: "medium" },
				],
			},
		],
	},

	// ─── rCal: Shared Calendar ─────────────────────────────────
	{
		id: "demo-cal-events",
		type: "folk-calendar",
		x: 50, y: 1350, width: 350, height: 250, rotation: 0,
		calTitle: "Alpine Explorer 2026",
		month: "July 2026",
		events: [
			{ date: "Jul 6", title: "Fly to Geneva", color: "#3b82f6" },
			{ date: "Jul 7", title: "Lac Blanc Hike", color: "#22c55e" },
			{ date: "Jul 8", title: "Via Ferrata", color: "#ef4444" },
			{ date: "Jul 10", title: "Train to Zermatt", color: "#3b82f6" },
			{ date: "Jul 13", title: "Paragliding", color: "#ef4444" },
			{ date: "Jul 14", title: "Transfer to Dolomites", color: "#3b82f6" },
			{ date: "Jul 15", title: "Tre Cime Loop", color: "#22c55e" },
			{ date: "Jul 18", title: "Cooking Class", color: "#f59e0b" },
			{ date: "Jul 20", title: "Fly Home", color: "#3b82f6" },
		],
	},

	// ─── rNetwork: Contact Graph ───────────────────────────────
	{
		id: "demo-network-graph",
		type: "folk-network",
		x: 1300, y: 1350, width: 400, height: 280, rotation: 0,
		networkTitle: "Trip Contacts",
		nodes: [
			{ id: "maya", label: "Maya", role: "organizer" },
			{ id: "liam", label: "Liam", role: "photographer" },
			{ id: "priya", label: "Priya", role: "logistics" },
			{ id: "omar", label: "Omar", role: "safety" },
			{ id: "vrony", label: "Chez Vrony", role: "restaurant" },
			{ id: "rega", label: "REGA Rescue", role: "emergency" },
			{ id: "locatelli", label: "Rif. Locatelli", role: "hut" },
		],
		edges: [
			{ from: "maya", to: "liam" },
			{ from: "maya", to: "priya" },
			{ from: "maya", to: "omar" },
			{ from: "priya", to: "locatelli" },
			{ from: "omar", to: "rega" },
			{ from: "liam", to: "vrony" },
		],
	},

	// ─── rTube: Shared Videos ──────────────────────────────────
	{
		id: "demo-tube-vlog",
		type: "folk-video",
		x: 2680, y: 50, width: 300, height: 180, rotation: 0,
		videoTitle: "Lac Blanc Sunrise — Test Footage",
		duration: "3:42",
		creator: "Liam",
		uploadedAt: "2026-07-08",
		views: 24,
		thumbnail: "sunrise-lacblanc",
	},

	// ─── rInbox: Group Messages ────────────────────────────────
	{
		id: "demo-inbox-msg",
		type: "folk-inbox",
		x: 2680, y: 260, width: 300, height: 160, rotation: 0,
		inboxTitle: "Trip Group Chat",
		messages: [
			{ from: "Maya", text: "Don't forget passports tomorrow!", time: "10:32 AM" },
			{ from: "Liam", text: "Drone batteries charging. All 3 ready.", time: "10:45 AM" },
			{ from: "Omar", text: "First aid kit packed. Added altitude sickness meds.", time: "11:02 AM" },
			{ from: "Priya", text: "Locatelli confirmed our reservation!", time: "11:15 AM" },
		],
	},

	// ─── rData: Trip Analytics ─────────────────────────────────
	{
		id: "demo-data-dashboard",
		type: "folk-dashboard",
		x: 2680, y: 450, width: 320, height: 220, rotation: 0,
		dashTitle: "Trip Statistics",
		metrics: [
			{ label: "Total Budget", value: "€4,000", trend: "neutral" },
			{ label: "Spent", value: "€1,203", trend: "up" },
			{ label: "Remaining", value: "€2,797", trend: "down" },
			{ label: "Tasks Done", value: "3/7", trend: "up" },
			{ label: "Hike Distance", value: "~85 km", trend: "neutral" },
			{ label: "Peak Altitude", value: "3,842m", trend: "neutral" },
		],
	},

	// ─── rChoices: Decision Matrix ─────────────────────────────
	{
		id: "demo-choices-camera",
		type: "folk-choice-matrix",
		x: 2680, y: 700, width: 320, height: 200, rotation: 0,
		choiceTitle: "Camera Gear Decision",
		options: [
			{ name: "DJI Mini 4 Pro", score: 8.5, criteria: { weight: 9, quality: 8, price: 7, battery: 10 } },
			{ name: "GoPro Hero 12", score: 7.2, criteria: { weight: 10, quality: 6, price: 8, battery: 5 } },
			{ name: "Sony A7C II", score: 7.8, criteria: { weight: 4, quality: 10, price: 5, battery: 8 } },
		],
		decidedBy: "Liam",
		status: "decided",
		winner: "DJI Mini 4 Pro",
	},

	// ─── rSplat: 3D Captures ───────────────────────────────────
	{
		id: "demo-splat-matterhorn",
		type: "folk-splat",
		x: 2680, y: 930, width: 300, height: 160, rotation: 0,
		splatTitle: "Matterhorn Base Camp — 3D Scan",
		pointCount: "2.4M",
		capturedBy: "Liam",
		capturedAt: "2026-07-12",
		fileSize: "48 MB",
		status: "processing",
	},
];

/**
 * Ensure the demo community exists and is seeded with data.
 * Called on server startup and after demo reset.
 */
export async function ensureDemoCommunity(): Promise<void> {
	const exists = await communityExists("demo");

	if (!exists) {
		await createCommunity("r* Ecosystem Demo", "demo", null, "public", {
			description: "Public demo space showcasing all rStack apps with the Alpine Explorer 2026 scenario",
		});
		console.log("[Demo] Created demo community with visibility: public");
	} else {
		await loadCommunity("demo");
	}

	// Check if already seeded (has shapes)
	const data = getDocumentData("demo");
	const shapeCount = data ? Object.keys(data.shapes || {}).length : 0;

	if (shapeCount === 0) {
		addShapes("demo", DEMO_SHAPES);
		console.log(`[Demo] Seeded ${DEMO_SHAPES.length} shapes into demo community`);
	} else {
		console.log(`[Demo] Demo community already has ${shapeCount} shapes`);
	}
}
