ALTER TABLE `tutorial_videos`
  ADD `slug` varchar(128),
  ADD `topicArea` varchar(32) DEFAULT 'tennis_skill',
  ADD `contentFormat` varchar(16) DEFAULT 'video',
  ADD `sourcePlatform` varchar(16) DEFAULT 'none',
  ADD `heroSummary` text,
  ADD `externalUrl` text,
  ADD `platformVideoId` varchar(64),
  ADD `estimatedEffortMinutes` int,
  ADD `prerequisites` json,
  ADD `learningObjectives` json,
  ADD `stepSections` json,
  ADD `deliverables` json,
  ADD `relatedDocPaths` json,
  ADD `viewCount` int,
  ADD `commentCount` int,
  ADD `metricsFetchedAt` timestamp NULL,
  ADD `completionAchievementKey` varchar(64),
  ADD `isFeatured` int DEFAULT 0,
  ADD `featuredOrder` int DEFAULT 0;
--> statement-breakpoint
ALTER TABLE `tutorial_progress`
  ADD `completed` int DEFAULT 0,
  ADD `completedAt` timestamp NULL;
--> statement-breakpoint
UPDATE `tutorial_videos`
SET
  `topicArea` = COALESCE(`topicArea`, 'tennis_skill'),
  `contentFormat` = COALESCE(`contentFormat`, 'video'),
  `sourcePlatform` = COALESCE(`sourcePlatform`, 'none'),
  `heroSummary` = COALESCE(`heroSummary`, `description`),
  `estimatedEffortMinutes` = COALESCE(`estimatedEffortMinutes`, CASE WHEN `duration` IS NULL THEN NULL ELSE ROUND(`duration` / 60) END),
  `isFeatured` = COALESCE(`isFeatured`, 0),
  `featuredOrder` = COALESCE(`featuredOrder`, 0);
--> statement-breakpoint
UPDATE `tutorial_progress`
SET
  `completed` = CASE WHEN `watched` = 1 THEN 1 ELSE COALESCE(`completed`, 0) END,
  `completedAt` = CASE WHEN `watched` = 1 AND `completedAt` IS NULL THEN `updatedAt` ELSE `completedAt` END;
