package controllers

import (
	"net/http"

	"fotbal-club/internal/models"
	"fotbal-club/internal/services"

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

type SetupController struct {
	setupService *services.SetupService
}

func NewSetupController(db *gorm.DB) *SetupController {
	return &SetupController{
		setupService: services.NewSetupService(db),
	}
}

type SetupStatusResponse struct {
	Status         string `json:"status"`
	SMTPConfigured bool   `json:"smtp_configured"`
	ClubImported   bool   `json:"club_imported"`
}

type ClubInfoRequest struct {
	FACRClubID     string `json:"facr_club_id" binding:"required"`
	Name           string `json:"name" binding:"required"`
	ShortName      string `json:"short_name"`
	LogoURL        string `json:"logo_url"`
	PrimaryColor   string `json:"primary_color"`
	SecondaryColor string `json:"secondary_color"`
	TextColor      string `json:"text_color"`
}

// GetSetupStatus returns the current setup status
// @Summary Get setup status
// @Description Returns the current setup status
// @Tags setup
// @Produce json
// @Success 200 {object} SetupStatusResponse
// @Router /api/v1/setup/status [get]
func (sc *SetupController) GetSetupStatus(c *gin.Context) {
	setupInfo, err := sc.setupService.GetSetupStatus()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get setup status"})
		return
	}

	c.JSON(http.StatusOK, SetupStatusResponse{
		Status:         string(setupInfo.Status),
		SMTPConfigured: setupInfo.SMTPConfigured,
		ClubImported:   setupInfo.ClubImported,
	})
}

// SaveSMTPConfig marks SMTP as configured
// @Summary Save SMTP configuration
// @Description Marks SMTP configuration as completed
// @Tags setup
// @Accept json
// @Produce json
// @Success 200 {object} map[string]string
// @Router /api/v1/setup/smtp [post]
func (sc *SetupController) SaveSMTPConfig(c *gin.Context) {
	if err := sc.setupService.MarkSMTPConfigured(); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save SMTP configuration"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "SMTP configuration saved successfully"})
}

// SaveClubInfo saves club information from FACR
// @Summary Save club information
// @Description Saves club information imported from FACR
// @Tags setup
// @Accept json
// @Produce json
// @Param clubInfo body ClubInfoRequest true "Club information"
// @Success 200 {object} map[string]string
// @Failure 400 {object} map[string]string
// @Router /api/v1/setup/club [post]
func (sc *SetupController) SaveClubInfo(c *gin.Context) {
	var req ClubInfoRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	clubInfo := &models.ClubInfo{
		FACRClubID:     req.FACRClubID,
		Name:           req.Name,
		ShortName:      req.ShortName,
		LogoURL:        req.LogoURL,
		PrimaryColor:   req.PrimaryColor,
		SecondaryColor: req.SecondaryColor,
		TextColor:      req.TextColor,
	}

	if err := sc.setupService.SaveClubInfo(clubInfo); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save club information"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Club information saved successfully"})
}

// CompleteSetup marks the setup as completed
// @Summary Complete setup
// @Description Marks the initial setup as completed
// @Tags setup
// @Produce json
// @Success 200 {object} map[string]string
// @Router /api/v1/setup/complete [post]
func (sc *SetupController) CompleteSetup(c *gin.Context) {
	if err := sc.setupService.CompleteSetup(); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to complete setup"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Setup completed successfully"})
}

// SkipSetup marks the setup as skipped
// @Summary Skip setup
// @Description Skips the initial setup
// @Tags setup
// @Produce json
// @Success 200 {object} map[string]string
// @Router /api/v1/setup/skip [post]
func (sc *SetupController) SkipSetup(c *gin.Context) {
	if err := sc.setupService.SkipSetup(); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to skip setup"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Setup skipped"})
}
