package main

import (
	"context"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"

	"chuan/internal/handlers"
	"chuan/internal/services"
	"chuan/internal/web"

	"github.com/go-chi/chi/v5"
	"github.com/go-chi/chi/v5/middleware"
	"github.com/go-chi/cors"
)

func main() {
	// 初始化服务
	p2pService := services.NewP2PService()

	// 初始化处理器
	h := handlers.NewHandler(p2pService)

	// 创建路由
	r := chi.NewRouter()

	// 中间件
	r.Use(middleware.Logger)
	r.Use(middleware.Recoverer)
	r.Use(middleware.Compress(5))

	// CORS 配置
	r.Use(cors.Handler(cors.Options{
		AllowedOrigins:   []string{"*"},
		AllowedMethods:   []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowedHeaders:   []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
		ExposedHeaders:   []string{"Link"},
		AllowCredentials: true,
		MaxAge:           300,
	}))

	// // 静态文件服务
	// fileServer := http.FileServer(http.Dir("./web/static/"))
	// r.Handle("/static/*", http.StripPrefix("/static", fileServer))

	// 嵌入式前端文件服务
	r.Handle("/*", web.CreateFrontendHandler())

	// 路由定义
	r.Get("/api", h.IndexHandler) // API 状态页面
	r.Get("/ws/webrtc", h.HandleWebRTCWebSocket)

	// 文字传输API路由
	r.Post("/api/create-text-room", h.CreateTextRoomHandler)
	r.Get("/api/get-text-content", h.GetTextContentHandler)

	// 文件传输API路由
	r.Post("/api/create-room", h.CreateRoomHandler)
	r.Get("/api/room-info", h.RoomInfoHandler)

	// 启动服务器
	srv := &http.Server{
		Addr:         ":8080",
		Handler:      r,
		ReadTimeout:  30 * time.Second,
		WriteTimeout: 30 * time.Second,
		IdleTimeout:  120 * time.Second,
	}

	// 优雅关闭
	go func() {
		log.Printf("服务器启动在端口 :8080")
		if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			log.Fatalf("服务器启动失败: %v", err)
		}
	}()

	// 等待中断信号
	quit := make(chan os.Signal, 1)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit
	log.Println("正在关闭服务器...")

	// 设置关闭超时
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	if err := srv.Shutdown(ctx); err != nil {
		log.Fatal("服务器强制关闭:", err)
	}

	log.Println("服务器已退出")
}
