package com.dora;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.net.InetAddress;

@SpringBootApplication
@EnableScheduling
@EnableAsync
@EnableCaching
@Slf4j
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ApplicationListener<ApplicationReadyEvent> applicationReadyEventListener(Environment environment) {
        return event -> {
            try {
                String port = environment.getProperty("server.port", "8083");
                String contextPath = environment.getProperty("server.servlet.context-path", "");
                String hostAddress = InetAddress.getLocalHost().getHostAddress();
                
                log.info("\n" +
                        "========================================================================================================\n" +
                        "  🎉 1818AI 用户端服务启动成功！\n" +
                        "========================================================================================================\n" +
                        "  📍 本地访问地址:\n" +
                        "     http://localhost:{}{}\n" +
                        "  📍 外网访问地址:\n" +
                        "     http://{}:{}{}\n" +
                        "  📚 接口文档地址 (Knife4j):\n" +
                        "     http://localhost:{}{}/doc.html\n" +
                        "     http://{}:{}{}/doc.html\n" +
                        "  📖 Swagger UI:\n" +
                        "     http://localhost:{}{}/swagger-ui/index.html\n" +
                        "========================================================================================================\n",
                        port, contextPath,
                        hostAddress, port, contextPath,
                        port, contextPath,
                        hostAddress, port, contextPath,
                        port, contextPath
                );
            } catch (Exception e) {
                log.error("获取服务器地址失败", e);
            }
        };
    }

}
