ALTER TABLE `training_records`
  ADD `exerciseType` varchar(64),
  ADD `sourceType` varchar(32) DEFAULT 'manual',
  ADD `sourceId` varchar(64),
  ADD `videoId` int,
  ADD `linkedPlanId` int,
  ADD `matchConfidence` float,
  ADD `actionCount` int DEFAULT 0,
  ADD `metadata` json;
--> statement-breakpoint
CREATE TABLE `live_analysis_sessions` (
  `id` int AUTO_INCREMENT NOT NULL,
  `userId` int NOT NULL,
  `title` varchar(256) NOT NULL,
  `sessionMode` enum('practice','pk') NOT NULL DEFAULT 'practice',
  `status` enum('active','completed','aborted') NOT NULL DEFAULT 'completed',
  `startedAt` timestamp NOT NULL DEFAULT (now()),
  `endedAt` timestamp,
  `durationMs` int NOT NULL DEFAULT 0,
  `dominantAction` varchar(64),
  `overallScore` float,
  `postureScore` float,
  `balanceScore` float,
  `techniqueScore` float,
  `footworkScore` float,
  `consistencyScore` float,
  `unknownActionRatio` float,
  `totalSegments` int NOT NULL DEFAULT 0,
  `effectiveSegments` int NOT NULL DEFAULT 0,
  `totalActionCount` int NOT NULL DEFAULT 0,
  `videoId` int,
  `videoUrl` text,
  `summary` text,
  `feedback` json,
  `metrics` json,
  `createdAt` timestamp NOT NULL DEFAULT (now()),
  `updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
  CONSTRAINT `live_analysis_sessions_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `live_action_segments` (
  `id` int AUTO_INCREMENT NOT NULL,
  `sessionId` int NOT NULL,
  `actionType` varchar(64) NOT NULL,
  `isUnknown` int NOT NULL DEFAULT 0,
  `startMs` int NOT NULL,
  `endMs` int NOT NULL,
  `durationMs` int NOT NULL,
  `confidenceAvg` float,
  `score` float,
  `peakScore` float,
  `frameCount` int NOT NULL DEFAULT 0,
  `issueSummary` json,
  `keyFrames` json,
  `clipLabel` varchar(128),
  `createdAt` timestamp NOT NULL DEFAULT (now()),
  CONSTRAINT `live_action_segments_id` PRIMARY KEY(`id`),
  CONSTRAINT `live_action_segments_session_start_idx` UNIQUE(`sessionId`,`startMs`)
);
--> statement-breakpoint
CREATE TABLE `daily_training_aggregates` (
  `id` int AUTO_INCREMENT NOT NULL,
  `dayKey` varchar(32) NOT NULL,
  `userId` int NOT NULL,
  `trainingDate` varchar(10) NOT NULL,
  `totalMinutes` int NOT NULL DEFAULT 0,
  `sessionCount` int NOT NULL DEFAULT 0,
  `analysisCount` int NOT NULL DEFAULT 0,
  `liveAnalysisCount` int NOT NULL DEFAULT 0,
  `recordingCount` int NOT NULL DEFAULT 0,
  `pkCount` int NOT NULL DEFAULT 0,
  `totalActions` int NOT NULL DEFAULT 0,
  `effectiveActions` int NOT NULL DEFAULT 0,
  `unknownActions` int NOT NULL DEFAULT 0,
  `totalScore` float NOT NULL DEFAULT 0,
  `averageScore` float NOT NULL DEFAULT 0,
  `metadata` json,
  `createdAt` timestamp NOT NULL DEFAULT (now()),
  `updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
  CONSTRAINT `daily_training_aggregates_id` PRIMARY KEY(`id`),
  CONSTRAINT `daily_training_aggregates_dayKey_unique` UNIQUE(`dayKey`)
);
--> statement-breakpoint
CREATE TABLE `ntrp_snapshots` (
  `id` int AUTO_INCREMENT NOT NULL,
  `snapshotKey` varchar(64) NOT NULL,
  `userId` int NOT NULL,
  `snapshotDate` varchar(10) NOT NULL,
  `rating` float NOT NULL,
  `triggerType` enum('analysis','daily','manual') NOT NULL DEFAULT 'daily',
  `taskId` varchar(64),
  `dimensionScores` json,
  `sourceSummary` json,
  `createdAt` timestamp NOT NULL DEFAULT (now()),
  CONSTRAINT `ntrp_snapshots_id` PRIMARY KEY(`id`),
  CONSTRAINT `ntrp_snapshots_snapshotKey_unique` UNIQUE(`snapshotKey`)
);
--> statement-breakpoint
CREATE TABLE `achievement_definitions` (
  `id` int AUTO_INCREMENT NOT NULL,
  `key` varchar(64) NOT NULL,
  `name` varchar(128) NOT NULL,
  `description` text,
  `category` varchar(32) NOT NULL,
  `rarity` varchar(16) NOT NULL DEFAULT 'common',
  `icon` varchar(16) NOT NULL DEFAULT '🎾',
  `metricKey` varchar(64) NOT NULL,
  `targetValue` float NOT NULL,
  `tier` int NOT NULL DEFAULT 1,
  `isHidden` int NOT NULL DEFAULT 0,
  `isActive` int NOT NULL DEFAULT 1,
  `sortOrder` int NOT NULL DEFAULT 0,
  `createdAt` timestamp NOT NULL DEFAULT (now()),
  `updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
  CONSTRAINT `achievement_definitions_id` PRIMARY KEY(`id`),
  CONSTRAINT `achievement_definitions_key_unique` UNIQUE(`key`)
);
--> statement-breakpoint
CREATE TABLE `user_achievements` (
  `id` int AUTO_INCREMENT NOT NULL,
  `progressKey` varchar(96) NOT NULL,
  `userId` int NOT NULL,
  `achievementKey` varchar(64) NOT NULL,
  `currentValue` float NOT NULL DEFAULT 0,
  `progressPct` float NOT NULL DEFAULT 0,
  `unlockedAt` timestamp,
  `lastEvaluatedAt` timestamp NOT NULL DEFAULT (now()),
  `createdAt` timestamp NOT NULL DEFAULT (now()),
  `updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
  CONSTRAINT `user_achievements_id` PRIMARY KEY(`id`),
  CONSTRAINT `user_achievements_progressKey_unique` UNIQUE(`progressKey`)
);
--> statement-breakpoint
ALTER TABLE `background_tasks`
  MODIFY COLUMN `type` enum('media_finalize','training_plan_generate','training_plan_adjust','analysis_corrections','pose_correction_multimodal','ntrp_refresh_user','ntrp_refresh_all') NOT NULL;
--> statement-breakpoint
CREATE TABLE `admin_audit_logs` (
  `id` int AUTO_INCREMENT NOT NULL,
  `adminUserId` int NOT NULL,
  `actionType` varchar(64) NOT NULL,
  `entityType` varchar(64) NOT NULL,
  `entityId` varchar(96),
  `targetUserId` int,
  `payload` json,
  `createdAt` timestamp NOT NULL DEFAULT (now()),
  CONSTRAINT `admin_audit_logs_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `app_settings` (
  `id` int AUTO_INCREMENT NOT NULL,
  `settingKey` varchar(64) NOT NULL,
  `label` varchar(128) NOT NULL,
  `description` text,
  `value` json,
  `createdAt` timestamp NOT NULL DEFAULT (now()),
  `updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
  CONSTRAINT `app_settings_id` PRIMARY KEY(`id`),
  CONSTRAINT `app_settings_settingKey_unique` UNIQUE(`settingKey`)
);
