-- PicHost 完整数据库结构
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `users`, `images`, `tags`, `image_tags`, `password_resets`, `feedbacks`, `image_feedbacks`, `user_settings`, `notification_types`, `user_notification_settings`, `notifications`, `file_operations`;

-- 用户表
CREATE TABLE `users` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `username` VARCHAR(50) UNIQUE NOT NULL,
    `email` VARCHAR(100) UNIQUE NOT NULL,
    `password` VARCHAR(255) NOT NULL,
    `role` ENUM('user', 'admin') DEFAULT 'user',
    `is_verified` BOOLEAN DEFAULT FALSE,
    `verification_code` VARCHAR(255),
    `api_key` VARCHAR(255),
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 图片表
CREATE TABLE `images` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_id` INT NOT NULL,
    `title` VARCHAR(255),
    `description` TEXT,
    `filename` VARCHAR(255) NOT NULL,
    `is_public` BOOLEAN DEFAULT TRUE,
    `file_size` INT,
    `mime_type` VARCHAR(100),
    `views` INT DEFAULT 0,
    `uploaded_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 标签表
CREATE TABLE `tags` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(50) UNIQUE NOT NULL,
    `color` VARCHAR(7) DEFAULT '#3498db',
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 图片标签关联表
CREATE TABLE `image_tags` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `image_id` INT NOT NULL,
    `tag_id` INT NOT NULL,
    FOREIGN KEY (`image_id`) REFERENCES `images`(`id`) ON DELETE CASCADE,
    FOREIGN KEY (`tag_id`) REFERENCES `tags`(`id`) ON DELETE CASCADE,
    UNIQUE KEY `unique_image_tag` (`image_id`, `tag_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 密码重置表
CREATE TABLE `password_resets` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `email` VARCHAR(100) NOT NULL,
    `token` VARCHAR(255) NOT NULL,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    `expires_at` TIMESTAMP NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 反馈表
CREATE TABLE `feedbacks` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_id` INT NULL,
    `type` ENUM('bug', 'feature', 'suggestion', 'other') NOT NULL,
    `subject` VARCHAR(255) NOT NULL,
    `message` TEXT NOT NULL,
    `status` ENUM('pending', 'reviewed', 'resolved') DEFAULT 'pending',
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 图片反馈表
CREATE TABLE `image_feedbacks` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `image_id` INT NOT NULL,
    `user_id` INT NULL,
    `type` ENUM('like', 'report') NOT NULL,
    `comment` TEXT NULL,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`image_id`) REFERENCES `images`(`id`) ON DELETE CASCADE,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 用户设置表
CREATE TABLE `user_settings` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_id` INT NOT NULL UNIQUE,
    `dark_mode` BOOLEAN DEFAULT FALSE,
    `language` VARCHAR(10) DEFAULT 'zh-CN',
    `items_per_page` INT DEFAULT 20,
    `email_notifications` BOOLEAN DEFAULT TRUE,
    `browser_notifications` BOOLEAN DEFAULT TRUE,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 通知类型表
CREATE TABLE `notification_types` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(50) NOT NULL,
    `description` TEXT,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 用户通知设置表
CREATE TABLE `user_notification_settings` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_id` INT NOT NULL,
    `notification_type_id` INT NOT NULL,
    `enabled` BOOLEAN DEFAULT TRUE,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY `unique_user_notification` (`user_id`, `notification_type_id`),
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
    FOREIGN KEY (`notification_type_id`) REFERENCES `notification_types`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 通知表
CREATE TABLE `notifications` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_id` INT NOT NULL,
    `type_id` INT NOT NULL,
    `title` VARCHAR(255) NOT NULL,
    `message` TEXT NOT NULL,
    `related_url` VARCHAR(500) NULL,
    `is_read` BOOLEAN DEFAULT FALSE,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
    FOREIGN KEY (`type_id`) REFERENCES `notification_types`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 文件操作记录表
CREATE TABLE `file_operations` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `user_id` INT NULL,
    `operation` ENUM('upload', 'delete', 'rename', 'move') NOT NULL,
    `file_path` VARCHAR(500) NOT NULL,
    `old_path` VARCHAR(500) NULL,
    `file_size` INT NULL,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 插入默认数据
INSERT IGNORE INTO `tags` (`id`, `name`, `color`) VALUES 
(1, '风景', '#27ae60'),
(2, '人物', '#e74c3c'),
(3, '动物', '#f39c12'),
(4, '建筑', '#9b59b6'),
(5, '美食', '#e67e22'),
(6, '艺术', '#1abc9c'),
(7, '科技', '#3498db'),
(8, '运动', '#2ecc71');

INSERT IGNORE INTO `notification_types` (`id`, `name`, `description`) VALUES 
(1, 'announcement', '系统公告和更新通知'),
(2, 'tips', '使用技巧和最佳实践'),
(3, 'feedback_result', '反馈处理结果'),
(4, 'image_feedback', '图片反馈通知');

-- 创建管理员用户 (密码: admin123)
INSERT IGNORE INTO `users` (`id`, `username`, `email`, `password`, `role`, `is_verified`) VALUES 
(1, 'admin', 'admin@66ghz.com', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'admin', 1);

SET FOREIGN_KEY_CHECKS=1;