CREATE TABLE `ad_recovery_sessions` (
	`id` char(36) NOT NULL,
	`user_id` char(36) NOT NULL,
	`type` enum('hearts','bonusAttempts','streakProtection') NOT NULL,
	`status` enum('pending','settling','completed','failed','expired') DEFAULT 'pending',
	`client_request_id` varchar(80) NOT NULL,
	`complete_request_id` varchar(80),
	`platform` enum('ios','android','harmony','web') NOT NULL,
	`ad_provider` varchar(50) NOT NULL,
	`ad_placement_id` varchar(120) NOT NULL,
	`provider_reward_token` varchar(500),
	`reward_snapshot` json,
	`progress_before` json,
	`progress_after` json,
	`failure_reason` varchar(80),
	`provider_error` varchar(500),
	`duplicate_count` int DEFAULT 0,
	`expires_at` datetime NOT NULL,
	`completed_at` datetime,
	`created_at` datetime DEFAULT CURRENT_TIMESTAMP,
	`updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `ad_recovery_sessions_id` PRIMARY KEY(`id`),
	CONSTRAINT `uk_ad_recovery_user_client_request` UNIQUE(`user_id`,`client_request_id`)
);
--> statement-breakpoint
ALTER TABLE `ad_recovery_sessions` ADD CONSTRAINT `ad_recovery_sessions_user_id_users_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE INDEX `idx_ad_recovery_user_type_status_created` ON `ad_recovery_sessions` (`user_id`,`type`,`status`,`created_at`);
