-- Password Reset Table
-- Run this SQL to add password reset functionality

CREATE TABLE IF NOT EXISTS `password_resets` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_id` INT NOT NULL,
    `email` VARCHAR(255) NOT NULL,
    `token_hash` VARCHAR(64) NOT NULL,
    `expires_at` DATETIME NOT NULL,
    `used` TINYINT(1) DEFAULT 0,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
    INDEX `idx_token_hash` (`token_hash`),
    INDEX `idx_expires_at` (`expires_at`),
    INDEX `idx_user_email` (`user_id`, `email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Clean up old tokens (run periodically via cron or manually)
-- DELETE FROM password_resets WHERE expires_at < NOW() OR used = 1;
