package main

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

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()

	r.GET("/health", func(c *gin.Context) {
		c.JSON(200, gin.H{"status": "ok", "service": "omnicommerce-api", "version": "1.0.0"})
	})

	v1 := r.Group("/api/v1")
	{
		// Auth
		v1.POST("/auth/login", handleLogin)
		v1.POST("/auth/register", handleRegister)
		v1.POST("/auth/refresh", handleRefreshToken)
		v1.POST("/auth/logout", handleLogout)

		// Products & Catalog
		v1.GET("/products", handleListProducts)
		v1.GET("/products/:id", handleGetProduct)
		v1.POST("/products", handleCreateProduct)
		v1.PUT("/products/:id", handleUpdateProduct)
		v1.DELETE("/products/:id", handleDeleteProduct)
		v1.GET("/products/:id/variants", handleListVariants)
		v1.POST("/products/:id/variants", handleCreateVariant)
		v1.GET("/categories", handleListCategories)
		v1.GET("/brands", handleListBrands)

		// Orders
		v1.GET("/orders", handleListOrders)
		v1.GET("/orders/:id", handleGetOrder)
		v1.POST("/orders", handleCreateOrder)
		v1.PUT("/orders/:id/status", handleUpdateOrderStatus)
		v1.POST("/orders/:id/cancel", handleCancelOrder)
		v1.POST("/orders/:id/refund", handleRequestRefund)

		// Cart
		v1.GET("/cart", handleGetCart)
		v1.POST("/cart/items", handleAddToCart)
		v1.PUT("/cart/items/:id", handleUpdateCartItem)
		v1.DELETE("/cart/items/:id", handleRemoveCartItem)

		// Customers
		v1.GET("/customers", handleListCustomers)
		v1.POST("/customers", handleCreateCustomer)
		v1.GET("/customers/:id", handleGetCustomer)
		v1.GET("/customers/:id/addresses", handleListAddresses)

		// Vendors
		v1.GET("/vendors", handleListVendors)
		v1.GET("/vendors/:id", handleGetVendor)
		v1.POST("/vendors/register", handleVendorRegistration)

		// Payments
		v1.POST("/payments", handleCreatePayment)
		v1.GET("/payments/:id", handleGetPayment)
		v1.POST("/payments/:id/capture", handleCapturePayment)

		// Search
		v1.GET("/search", handleSearch)

		// Analytics
		v1.GET("/analytics/dashboard", handleDashboardAnalytics)
		v1.GET("/analytics/sales", handleSalesAnalytics)

		// Messaging
		v1.GET("/conversations", handleListConversations)
		v1.POST("/messages", handleSendMessage)

		// Admin
		admin := v1.Group("/admin")
		{
			admin.GET("/tenants", handleListTenants)
			admin.GET("/settlements", handleListSettlements)
			admin.POST("/settlements/:id/payout", handleProcessPayout)
			admin.GET("/moderation/products", handleModerationQueue)
			admin.POST("/moderation/products/:id/approve", handleApproveProduct)
		}
	}

	srv := &http.Server{Addr: ":8080", Handler: r}

	go func() {
		if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			log.Fatalf("listen: %s\n", err)
		}
	}()

	quit := make(chan os.Signal, 1)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit
	log.Println("Shutting down...")

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	srv.Shutdown(ctx)
	log.Println("Server exited")
}
