package routes

import (
	"bssapp-backend/db"
	"bssapp-backend/queries"
	"bssapp-backend/utils"
	"database/sql"
	"encoding/json"
	"log"
	"net/http"
	"strings"

	"github.com/gorilla/mux"
)

type BrandClassificationLookupResponse struct {
	Groups []queries.BrandGroupOption `json:"groups"`
}

type BrandSyncResponse struct {
	Upserted int `json:"upserted"`
	Deleted  int `json:"deleted"`
	Total    int `json:"total"`
}

type BrandSetGroupPayload struct {
	GroupID int `json:"group_id"`
}

type BrandBulkItem struct {
	BrandCode string `json:"brand_code"`
	GroupID   int    `json:"group_id"`
}

type BrandBulkPayload struct {
	Items []BrandBulkItem `json:"items"`
}

func GetBrandClassificationLookupsHandler(pg *sql.DB) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		if err := queries.EnsureBrandClassificationTables(pg); err != nil {
			http.Error(w, "brand tables bootstrap error", http.StatusInternalServerError)
			return
		}

		traceID := utils.TraceIDFromRequest(r)
		ctx := utils.ContextWithTraceID(r.Context(), traceID)

		groups, err := queries.ListBrandGroups(ctx, pg)
		if err != nil {
			http.Error(w, "brand groups lookup error", http.StatusInternalServerError)
			return
		}

		_ = json.NewEncoder(w).Encode(BrandClassificationLookupResponse{Groups: groups})
	}
}

func ListBrandsHandler(pg *sql.DB) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		if err := queries.EnsureBrandClassificationTables(pg); err != nil {
			http.Error(w, "brand tables bootstrap error", http.StatusInternalServerError)
			return
		}

		q := strings.TrimSpace(r.URL.Query().Get("q"))
		traceID := utils.TraceIDFromRequest(r)
		ctx := utils.ContextWithTraceID(r.Context(), traceID)

		rows, err := queries.ListBrandsWithGroups(ctx, pg, q, 20000)
		if err != nil {
			http.Error(w, "brand list error", http.StatusInternalServerError)
			return
		}
		_ = json.NewEncoder(w).Encode(rows)
	}
}

func SyncBrandsFromMSSQLHandler(pg *sql.DB) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json; charset=utf-8")

		mssql := db.GetDB()
		if mssql == nil {
			http.Error(w, "mssql connection not available", http.StatusServiceUnavailable)
			return
		}

		traceID := utils.TraceIDFromRequest(r)
		ctx := utils.ContextWithTraceID(r.Context(), traceID)

		res, err := queries.SyncBrandsFromMSSQL(ctx, mssql, pg)
		if err != nil {
			log.Printf("brand sync error trace=%s err=%v", traceID, err)
			http.Error(w, "brand sync error", http.StatusInternalServerError)
			return
		}

		_ = json.NewEncoder(w).Encode(BrandSyncResponse{
			Upserted: res.Upserted,
			Deleted:  res.Deleted,
			Total:    res.Total,
		})
	}
}

func SetBrandGroupHandler(pg *sql.DB) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json; charset=utf-8")

		if err := queries.EnsureBrandClassificationTables(pg); err != nil {
			http.Error(w, "brand tables bootstrap error", http.StatusInternalServerError)
			return
		}

		brandCode := strings.TrimSpace(mux.Vars(r)["code"])
		if brandCode == "" {
			http.Error(w, "invalid brand_code", http.StatusBadRequest)
			return
		}

		var payload BrandSetGroupPayload
		if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
			http.Error(w, "invalid payload", http.StatusBadRequest)
			return
		}
		if payload.GroupID < 0 || payload.GroupID > 3 {
			http.Error(w, "invalid group_id", http.StatusBadRequest)
			return
		}

		traceID := utils.TraceIDFromRequest(r)
		ctx := utils.ContextWithTraceID(r.Context(), traceID)

		tx, err := pg.BeginTx(ctx, nil)
		if err != nil {
			http.Error(w, "pg transaction start error", http.StatusInternalServerError)
			return
		}
		defer tx.Rollback()

		if err := queries.SetBrandGroup(ctx, tx, brandCode, payload.GroupID); err != nil {
			http.Error(w, "brand group save error", http.StatusInternalServerError)
			return
		}

		if err := tx.Commit(); err != nil {
			http.Error(w, "pg transaction commit error", http.StatusInternalServerError)
			return
		}

		_ = json.NewEncoder(w).Encode(map[string]any{
			"success":    true,
			"brand_code": brandCode,
			"group_id":   payload.GroupID,
		})
	}
}

func SetBrandGroupsBulkHandler(pg *sql.DB) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json; charset=utf-8")

		if err := queries.EnsureBrandClassificationTables(pg); err != nil {
			http.Error(w, "brand tables bootstrap error", http.StatusInternalServerError)
			return
		}

		var payload BrandBulkPayload
		if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
			http.Error(w, "invalid payload", http.StatusBadRequest)
			return
		}
		if len(payload.Items) == 0 {
			_ = json.NewEncoder(w).Encode(map[string]any{"success": true, "updated": 0})
			return
		}

		traceID := utils.TraceIDFromRequest(r)
		ctx := utils.ContextWithTraceID(r.Context(), traceID)

		tx, err := pg.BeginTx(ctx, nil)
		if err != nil {
			http.Error(w, "pg transaction start error", http.StatusInternalServerError)
			return
		}
		defer tx.Rollback()

		updated := 0
		for _, it := range payload.Items {
			code := strings.TrimSpace(it.BrandCode)
			if code == "" {
				continue
			}
			if it.GroupID < 0 || it.GroupID > 3 {
				http.Error(w, "invalid group_id", http.StatusBadRequest)
				return
			}
			if err := queries.SetBrandGroup(ctx, tx, code, it.GroupID); err != nil {
				http.Error(w, "brand group save error", http.StatusInternalServerError)
				return
			}
			updated++
		}

		if err := tx.Commit(); err != nil {
			http.Error(w, "pg transaction commit error", http.StatusInternalServerError)
			return
		}

		_ = json.NewEncoder(w).Encode(map[string]any{
			"success": true,
			"updated": updated,
		})
	}
}
