-- 1. 添加 auth 相关字段到 users 表
CREATE TABLE `users_new` (
    `id` integer PRIMARY KEY NOT NULL,
    `username` text NOT NULL,
    `email` text,
    `nickname` text,
    `password` text NOT NULL,
    `avatar` text,
    `role` text DEFAULT 'user' NOT NULL,
    `status` text DEFAULT 'active' NOT NULL,
    `public_slug` text,
    `bio_markdown` text,
    `bio_visibility` text DEFAULT 'private' NOT NULL,
    `social_links_json` text DEFAULT '[]' NOT NULL,
    `avatar_visibility` text DEFAULT 'private' NOT NULL,
    `discover_visible` integer DEFAULT true NOT NULL,
    `discover_location` text,
    `discover_show_location` integer DEFAULT false NOT NULL,
    `created_at` integer DEFAULT (cast((julianday('now') - 2440587.5)*86400000 as integer)) NOT NULL,
    `updated_at` integer DEFAULT (cast((julianday('now') - 2440587.5)*86400000 as integer)) NOT NULL,
    `email_verified` integer DEFAULT false NOT NULL,
    `password_history` text DEFAULT '[]' NOT NULL,
    `failed_login_attempts` integer DEFAULT 0 NOT NULL,
    `lockout_until` integer,
    `last_login_at` integer,
    `last_login_ip` text
);
--> statement-breakpoint
INSERT INTO `users_new` (`id`, `username`, `email`, `nickname`, `password`, `avatar`, `role`, `status`, `public_slug`, `bio_markdown`, `bio_visibility`, `social_links_json`, `avatar_visibility`, `discover_visible`, `discover_location`, `discover_show_location`, `created_at`, `updated_at`) SELECT `id`, `username`, `email`, `nickname`, `password`, `avatar`, `role`, `status`, `public_slug`, `bio_markdown`, `bio_visibility`, `social_links_json`, `avatar_visibility`, `discover_visible`, `discover_location`, `discover_show_location`, `created_at`, `updated_at` FROM `users`;
--> statement-breakpoint
DROP TABLE `users`;
--> statement-breakpoint
ALTER TABLE `users_new` RENAME TO `users`;
--> statement-breakpoint
CREATE UNIQUE INDEX `users_email_unique` ON `users` (`email`);
--> statement-breakpoint
CREATE INDEX `users_email_index` ON `users` (`email`);

-- 2. 创建 user_sessions 表
CREATE TABLE `user_sessions` (
    `id` text PRIMARY KEY NOT NULL,
    `user_id` integer NOT NULL,
    `refresh_token_hash` text NOT NULL,
    `user_agent` text,
    `ip` text,
    `created_at` integer NOT NULL,
    `expires_at` integer NOT NULL,
    `revoked_at` integer,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
);
--> statement-breakpoint
CREATE INDEX `user_sessions_user_id_index` ON `user_sessions` (`user_id`);