package handlers

import (
	"aggios-app/backend/internal/domain"
	"context"
	"encoding/json"
	"log"
	"net/http"

	"golang.org/x/crypto/bcrypt"
)

// PublicSignupRequest representa o cadastro público via template
type PublicSignupRequest struct {
	TemplateSlug string `json:"template_slug"`
	Email        string `json:"email"`
	Password     string `json:"password"`
	Name         string `json:"name"`
	Subdomain    string `json:"subdomain"`
	CompanyName  string `json:"company_name"`
}

// PublicRegister handles public registration via template
func (h *SignupTemplateHandler) PublicRegister(w http.ResponseWriter, r *http.Request) {
	var req PublicSignupRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		log.Printf("Error decoding request body: %v", err)
		http.Error(w, "Invalid request body", http.StatusBadRequest)
		return
	}

	ctx := context.Background()

	// 1. Buscar o template
	template, err := h.repo.FindBySlug(ctx, req.TemplateSlug)
	if err != nil {
		log.Printf("Error finding template: %v", err)
		http.Error(w, "Template not found", http.StatusNotFound)
		return
	}

	// 2. Incrementar usage_count
	if err := h.repo.IncrementUsageCount(ctx, template.ID); err != nil {
		log.Printf("Error incrementing usage count: %v", err)
	}

	// 3. Verificar se email já existe
	emailExists, err := h.userRepo.EmailExists(req.Email)
	if err != nil {
		log.Printf("Error checking email: %v", err)
		http.Error(w, "Error processing registration", http.StatusInternalServerError)
		return
	}
	if emailExists {
		http.Error(w, "Email already registered", http.StatusBadRequest)
		return
	}

	// 4. Verificar se subdomain já existe (se fornecido)
	if req.Subdomain != "" {
		exists, err := h.tenantRepo.SubdomainExists(req.Subdomain)
		if err != nil {
			log.Printf("Error checking subdomain: %v", err)
			http.Error(w, "Error processing registration", http.StatusInternalServerError)
			return
		}
		if exists {
			http.Error(w, "Subdomain already taken", http.StatusBadRequest)
			return
		}
	}

	// 5. Hash da senha
	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
	if err != nil {
		log.Printf("Error hashing password: %v", err)
		http.Error(w, "Error processing registration", http.StatusInternalServerError)
		return
	}

	// 6. Criar tenant (empresa/cliente)
	tenant := &domain.Tenant{
		Name:        req.CompanyName,
		Domain:      req.Subdomain + ".aggios.app",
		Subdomain:   req.Subdomain,
		Description: "Registered via " + template.Name,
	}

	if err := h.tenantRepo.Create(tenant); err != nil {
		log.Printf("Error creating tenant: %v", err)
		http.Error(w, "Error creating account", http.StatusInternalServerError)
		return
	}

	// 7. Criar usuário admin do tenant
	user := &domain.User{
		Email:    req.Email,
		Password: string(hashedPassword),
		Name:     req.Name,
		Role:     "CLIENTE",
		TenantID: &tenant.ID,
	}

	if err := h.userRepo.Create(user); err != nil {
		log.Printf("Error creating user: %v", err)
		http.Error(w, "Error creating user", http.StatusInternalServerError)
		return
	}

	// 8. Resposta de sucesso
	response := map[string]interface{}{
		"success": true,
		"message": template.SuccessMessage,
		"tenant_id": tenant.ID,
		"user_id": user.ID,
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusCreated)
	json.NewEncoder(w).Encode(response)
}
