package com.home4j.config;

import com.home4j.entity.User;
import com.home4j.entity.BookmarkGroup;
import com.home4j.entity.Bookmark;
import com.home4j.entity.Config;
import io.quarkus.elytron.security.common.BcryptUtil;
import io.quarkus.runtime.StartupEvent;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.transaction.Transactional;
import org.jboss.logging.Logger;

/**
 * 应用启动时初始化默认数据
 */
@ApplicationScoped
public class DataInitializer {

    private static final Logger LOG = Logger.getLogger(DataInitializer.class);

    @Transactional
    public void onStart(@Observes StartupEvent event) {
        LOG.info("Checking and initializing default data...");
        
        // 初始化默认管理员
        initDefaultAdmin();
        
        // 初始化默认配置
        initDefaultConfig();
        
        // 初始化默认分组和书签
        initDefaultBookmarks();
        
        LOG.info("Data initialization completed.");
    }

    private void initDefaultAdmin() {
        if (User.count() == 0) {
            LOG.info("Creating default admin user...");
            User admin = new User();
            admin.username = "admin";
            admin.password = BcryptUtil.bcryptHash("admin123");
            admin.role = "ADMIN";
            admin.persist();
            LOG.info("Default admin user created: admin / admin123");
        }
    }

    private void initDefaultConfig() {
        if (Config.count() == 0) {
            LOG.info("Creating default configurations...");
            
            // 基础设置
            createConfig("site_title", "Home4j", "网站标题");
            createConfig("site_subtitle", "我的个人导航页", "网站副标题");
            createConfig("footer_text", "Powered by Home4j", "页脚文本");
            
            // 主题设置
            createConfig("theme", "light", "主题 (light/dark/cupcake/dracula)");
            
            // 搜索设置
            createConfig("show_search", "true", "是否显示搜索框");
            createConfig("search_engine", "https://www.google.com/search", "搜索引擎URL");
            
            // 显示设置
            createConfig("view_mode", "icon", "视图模式 (icon/list)");
            createConfig("columns", "4", "书签列数 (2-6)");
            
            // 背景设置
            createConfig("background_image", "", "背景图URL（空则无背景）");
            createConfig("background_overlay", "0.5", "背景遮罩透明度 (0-1)");
            
            LOG.info("Default configurations created.");
        }
    }

    private void createConfig(String key, String value, String description) {
        Config config = new Config();
        config.configKey = key;
        config.configValue = value;
        config.description = description;
        config.persist();
    }

    private void initDefaultBookmarks() {
        if (BookmarkGroup.count() == 0) {
            LOG.info("Creating default groups and bookmarks...");
            
            // 常用工具组
            BookmarkGroup toolsGroup = new BookmarkGroup();
            toolsGroup.name = "常用工具";
            toolsGroup.icon = "wrench";
            toolsGroup.sortOrder = 0;
            toolsGroup.visible = true;
            toolsGroup.persist();
            
            createBookmark(toolsGroup, "Google", "https://www.google.com", "全球最大的搜索引擎", "search", 0);
            createBookmark(toolsGroup, "Gmail", "https://mail.google.com", "Google邮箱服务", "mail", 1);
            
            // 开发资源组
            BookmarkGroup devGroup = new BookmarkGroup();
            devGroup.name = "开发资源";
            devGroup.icon = "code";
            devGroup.sortOrder = 1;
            devGroup.visible = true;
            devGroup.persist();
            
            createBookmark(devGroup, "GitHub", "https://github.com", "全球最大的代码托管平台", "github", 0);
            createBookmark(devGroup, "Stack Overflow", "https://stackoverflow.com", "程序员问答社区", "message-circle", 1);
            
            // 社交媒体组
            BookmarkGroup socialGroup = new BookmarkGroup();
            socialGroup.name = "社交媒体";
            socialGroup.icon = "users";
            socialGroup.sortOrder = 2;
            socialGroup.visible = true;
            socialGroup.persist();
            
            createBookmark(socialGroup, "Twitter", "https://twitter.com", "全球社交平台", "twitter", 0);
            createBookmark(socialGroup, "YouTube", "https://youtube.com", "全球最大的视频平台", "youtube", 1);
            
            LOG.info("Default groups and bookmarks created.");
        }
    }

    private void createBookmark(BookmarkGroup group, String name, String url, String description, String icon, int sortOrder) {
        Bookmark bookmark = new Bookmark();
        bookmark.group = group;
        bookmark.name = name;
        bookmark.url = url;
        bookmark.description = description;
        bookmark.icon = icon;
        bookmark.sortOrder = sortOrder;
        bookmark.visible = true;
        bookmark.openInNewTab = true;
        bookmark.persist();
    }
}
