package handlers

import (
	"fmt"
	"os"
	"time"

	"github.com/anqorithm/naqa-api/internal/constants"
	"github.com/gofiber/fiber/v2"
	"github.com/google/uuid"
	"go.mongodb.org/mongo-driver/mongo"
)

type Handler struct {
	db *mongo.Database
}

func NewHandler(db *mongo.Database) *Handler {
	return &Handler{
		db: db,
	}
}

// ###############################################################################
// Handler Functions
// ###############################################################################

// RootHandler handles the "/" endpoint
// @Summary Get API root information
// @Description Get basic API information including version, description, and supported endpoints
// @Tags Root
// @Accept json
// @Produce json
// @Success 200 {object} models.RootResponse "API information"
// @Router / [get]
func (h *Handler) RootHandler(c *fiber.Ctx) error {
	return c.JSON(fiber.Map{
		"app_name":      os.Getenv("APP_NAME"),
		"version":       os.Getenv("API_VERSION"),
		"description":   os.Getenv("APP_DESCRIPTION"),
		"request_time":  time.Now().Format(time.RFC3339),
		"environment":   os.Getenv("ENVIRONMENT"),
		"supported_api": []string{"/api/v1"},
	})
}

// ApiV1Handler handles the "/api/v1" endpoint
// @Summary Get API v1 information
// @Description Get API v1 status, version, and available endpoints
// @Tags API Info
// @Accept json
// @Produce json
// @Success 200 {object} models.ApiV1Response "API v1 information"
// @Router /api/v1 [get]
func (h *Handler) ApiV1Handler(c *fiber.Ctx) error {
	return c.JSON(fiber.Map{
		"status":      "active",
		"message":     "Welcome to NAQA API v1",
		"version":     "1.0.0",
		"env":         os.Getenv("ENVIRONMENT"),
		"server_time": time.Now().Format(time.RFC3339),
		"request_id":  c.Get("X-Request-ID", uuid.New().String()),
		"endpoints": []string{
			"/api/v1/stocks",
			"/api/v1/health",
			"/api/v1/metrics",
		},
	})
}

// HealthCheckHandler handles the health check endpoint
// @Summary Health check
// @Description Check if the API is healthy and running
// @Tags Health
// @Accept json
// @Produce json
// @Success 200 {object} models.HealthResponse "Health check response"
// @Router /api/v1/health [get]
func (h *Handler) HealthCheckHandler(c *fiber.Ctx) error {
	return c.JSON(fiber.Map{
		"status":     "OK",
		"timestamp":  time.Now().Format(time.RFC3339),
		"message":    constants.SuccessHealthCheck,
		"request_id": c.Get("X-Request-ID", uuid.New().String()),
	})
}

// GetStocksBaseHandler handles the base stocks endpoint
// @Summary Get stocks API information
// @Description Get information about available stock endpoints, years, and examples
// @Tags Stocks
// @Accept json
// @Produce json
// @Success 200 {object} models.StocksBaseResponse "Stocks API information"
// @Router /api/v1/stocks [get]
func (h *Handler) GetStocksBaseHandler(c *fiber.Ctx) error {
	latestYear := constants.AvailableYears[len(constants.AvailableYears)-1]
	return c.JSON(fiber.Map{
		"status":          "success",
		"message":         "Welcome to Stocks API",
		"available_years": constants.AvailableYears,
		"endpoints": []map[string]interface{}{
			{
				"name":        "Get Stocks",
				"path":        "/api/v1/stocks/year/{year}",
				"method":      "GET",
				"description": "Get all stocks for a specific year",
				"example":     fmt.Sprintf("/api/v1/stocks/year/%s", latestYear),
			},
			{
				"name":        "Search Stocks",
				"path":        "/api/v1/stocks/year/{year}/search",
				"method":      "GET",
				"description": "Search stocks with filters",
				"example":     fmt.Sprintf("/api/v1/stocks/year/%s/search?sector=الطاقة&sharia_opinion=نقية", latestYear),
				"parameters": []string{
					"name", "code", "sector", "sharia_opinion",
				},
			},
			{
				"name":        "Calculate Purification",
				"path":        "/api/v1/stocks/year/{year}/calculate-purification",
				"method":      "POST",
				"description": "Calculate stock purification amount",
				"example": map[string]interface{}{
					"url": fmt.Sprintf("/api/v1/stocks/year/%s/calculate-purification", latestYear),
					"body": map[string]interface{}{
						"start_date":       "2023-01-01",
						"end_date":         "2023-12-31",
						"number_of_stocks": 100,
						"stock_code":       "1111",
					},
				},
			},
		},
		"documentation": "https://github.com/anqorithm/naqa-api",
	})
}
