package handlers

import (
	"strconv"

	"github.com/gofiber/fiber/v2"
)

func (h *Handler) GlobalSearch(c *fiber.Ctx) error {
	q := c.Query("q")
	if q == "" {
		return c.Status(400).JSON(fiber.Map{"error": "query parameter 'q' required"})
	}
	limit, _ := strconv.Atoi(c.Query("limit", "10"))

	companies, _ := h.db.SearchCompanies(q, limit)
	filings, _ := h.db.SearchFilings(q, limit)

	return c.JSON(fiber.Map{
		"companies": fiber.Map{"data": companies, "total": len(companies)},
		"filings":   fiber.Map{"data": filings, "total": len(filings)},
	})
}
