-- Add email verification fields to users table
ALTER TABLE `users` ADD COLUMN `emailVerified` tinyint(1) DEFAULT 0 NOT NULL;
ALTER TABLE `users` ADD COLUMN `emailVerificationToken` varchar(255);
ALTER TABLE `users` ADD COLUMN `emailVerificationTokenExpiry` timestamp;

-- Create email verifications table
CREATE TABLE `email_verifications` (
  `id` int AUTO_INCREMENT NOT NULL,
  `userId` int NOT NULL,
  `email` varchar(255) NOT NULL,
  `token` varchar(255) NOT NULL,
  `expiresAt` timestamp NOT NULL,
  `verifiedAt` timestamp,
  `createdAt` timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email_verifications_token_unique` (`token`),
  KEY `idx_email_verifications_userId` (`userId`),
  KEY `idx_email_verifications_email` (`email`),
  FOREIGN KEY (`userId`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
