package handlers

import (
	"bookhoard/internal/database"
	"net/http"
	"strconv"

	"github.com/jackc/pgx/v5"
	"github.com/labstack/echo/v5"
)

type SystemSettingsHandler struct {
	db *database.Queries
}

func NewSystemSettingsHandler(db *database.Queries) *SystemSettingsHandler {
	return &SystemSettingsHandler{
		db: db,
	}
}

type UpdateScanSettingsRequest struct {
	ScanPollIntervalSeconds int32 `json:"scan_poll_interval_seconds" validate:"required,min=1,max=3600"`
	AutoScanEnabled         bool  `json:"auto_scan_enabled"`
}

type ScanSettingsResponse struct {
	ScanPollIntervalSeconds int32  `json:"scan_poll_interval_seconds"`
	AutoScanEnabled         bool   `json:"auto_scan_enabled"`
	Message                 string `json:"message,omitempty"`
}

func (h *SystemSettingsHandler) UpdateScanSettings(c *echo.Context) error {
	var req UpdateScanSettingsRequest
	if err := c.Bind(&req); err != nil {
		return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid request"})
	}
	if err := c.Validate(&req); err != nil {
		return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
	}

	scanFrequencyValue := strconv.FormatInt(int64(req.ScanPollIntervalSeconds), 10)
	autoScanValue := strconv.FormatBool(req.AutoScanEnabled)

	err := h.db.UpdateSystemSetting(c.Request().Context(), database.UpdateSystemSettingParams{
		SettingKey:   "scan_poll_interval_seconds",
		SettingValue: scanFrequencyValue,
	})
	if err != nil {
		if err == pgx.ErrNoRows {
			return c.JSON(http.StatusNotFound, map[string]string{"error": "system setting not found"})
		}
		return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
	}

	err = h.db.UpdateSystemSetting(c.Request().Context(), database.UpdateSystemSettingParams{
		SettingKey:   "auto_scan_enabled",
		SettingValue: autoScanValue,
	})
	if err != nil {
		if err == pgx.ErrNoRows {
			return c.JSON(http.StatusNotFound, map[string]string{"error": "system setting not found"})
		}
		return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
	}

	return c.JSON(http.StatusOK, ScanSettingsResponse{
		ScanPollIntervalSeconds: req.ScanPollIntervalSeconds,
		AutoScanEnabled:         req.AutoScanEnabled,
		Message:                 "scan settings updated successfully",
	})
}

func (h *SystemSettingsHandler) GetScanSettings(c *echo.Context) error {
	scanFrequencySetting, err := h.db.GetSystemSetting(c.Request().Context(), "scan_poll_interval_seconds")
	if err != nil {
		if err == pgx.ErrNoRows {
			return c.JSON(http.StatusOK, ScanSettingsResponse{
				ScanPollIntervalSeconds: 60,
				AutoScanEnabled:         true,
			})
		}
		return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
	}

	autoScanSetting, err := h.db.GetSystemSetting(c.Request().Context(), "auto_scan_enabled")
	if err != nil {
		if err == pgx.ErrNoRows {
			return c.JSON(http.StatusOK, ScanSettingsResponse{
				ScanPollIntervalSeconds: 60,
				AutoScanEnabled:         true,
			})
		}
		return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
	}

	scanFrequency, err := strconv.ParseInt(scanFrequencySetting, 10, 32)
	if err != nil {
		return c.JSON(http.StatusInternalServerError, map[string]string{"error": "invalid scan frequency setting"})
	}

	autoScanEnabled, err := strconv.ParseBool(autoScanSetting)
	if err != nil {
		return c.JSON(http.StatusInternalServerError, map[string]string{"error": "invalid auto scan setting"})
	}

	return c.JSON(http.StatusOK, ScanSettingsResponse{
		ScanPollIntervalSeconds: int32(scanFrequency),
		AutoScanEnabled:         autoScanEnabled,
	})
}
