package user

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/http"
	"path/filepath"
	"strings"
	"time"

	"gitea.local/admin/hspguard/internal/config"
	imiddleware "gitea.local/admin/hspguard/internal/middleware"
	"gitea.local/admin/hspguard/internal/repository"
	"gitea.local/admin/hspguard/internal/storage"
	"gitea.local/admin/hspguard/internal/util"
	"gitea.local/admin/hspguard/internal/web"
	"github.com/go-chi/chi/v5"
	"github.com/google/uuid"
	"github.com/minio/minio-go/v7"
)

type UserHandler struct {
	repo  *repository.Queries
	minio *storage.FileStorage
	cfg   *config.AppConfig
}

func NewUserHandler(repo *repository.Queries, minio *storage.FileStorage, cfg *config.AppConfig) *UserHandler {
	return &UserHandler{
		repo,
		minio,
		cfg,
	}
}

func (h *UserHandler) RegisterRoutes(api chi.Router) {
	api.Group(func(protected chi.Router) {
		authMiddleware := imiddleware.NewAuthMiddleware(h.cfg)
		protected.Use(authMiddleware.Runner)

		protected.Put("/avatar", h.uploadAvatar)
	})

	api.Post("/register", h.register)
	api.Get("/avatar/{avatar}", h.getAvatar)
}

type RegisterParams struct {
	FullName    string `json:"full_name"`
	Email       string `json:"email"`
	PhoneNumber string `json:"phone"`
	Password    string `json:"password"`
}

func (h *UserHandler) register(w http.ResponseWriter, r *http.Request) {
	var params RegisterParams

	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(&params); err != nil {
		web.Error(w, "failed to parse request body", http.StatusBadRequest)
		return
	}

	if params.Email == "" || params.FullName == "" || params.Password == "" {
		web.Error(w, "missing required fields", http.StatusBadRequest)
		return
	}

	_, err := h.repo.FindUserEmail(context.Background(), params.Email)
	if err == nil {
		web.Error(w, "user with provided email already exists", http.StatusBadRequest)
		return
	}

	hash, err := util.HashPassword(params.Password)
	if err != nil {
		web.Error(w, "failed to create user account", http.StatusInternalServerError)
		return
	}

	id, err := h.repo.InsertUser(context.Background(), repository.InsertUserParams{
		FullName:     params.FullName,
		Email:        params.Email,
		PasswordHash: hash,
		IsAdmin:      false,
	})
	if err != nil {
		web.Error(w, "failed to create new user", http.StatusInternalServerError)
		return
	}

	encoder := json.NewEncoder(w)

	type Response struct {
		Id string `json:"id"`
	}

	w.Header().Set("Content-Type", "application/json")

	if err := encoder.Encode(Response{
		Id: id.String(),
	}); err != nil {
		web.Error(w, "failed to encode response", http.StatusInternalServerError)
	}
}

func (h *UserHandler) getAvatar(w http.ResponseWriter, r *http.Request) {
	avatarObject := chi.URLParam(r, "avatar")

	object, err := h.minio.GetObject(r.Context(), "guard-storage", avatarObject, minio.GetObjectOptions{})
	if err != nil {
		web.Error(w, "avatar not found", http.StatusNotFound)
		return
	}
	defer object.Close()

	stat, err := object.Stat()
	if err != nil {
		log.Printf("ERR: failed to get object stats: %v\n", err)
		web.Error(w, "failed to get avatar", http.StatusNotFound)
		return
	}

	w.Header().Set("Content-Type", stat.ContentType)
	w.WriteHeader(http.StatusOK)
	io.Copy(w, object)
}

func (h *UserHandler) uploadAvatar(w http.ResponseWriter, r *http.Request) {
	userId, ok := util.GetRequestUserId(r.Context())
	if !ok {
		web.Error(w, "failed to get user id from auth session", http.StatusInternalServerError)
		return
	}

	user, err := h.repo.FindUserId(r.Context(), uuid.MustParse(userId))
	if err != nil {
		web.Error(w, "user with provided id does not exist", http.StatusUnauthorized)
		return
	}

	err = r.ParseMultipartForm(10 << 20)
	if err != nil {
		web.Error(w, "invalid form data", http.StatusBadRequest)
		return
	}

	file, header, err := r.FormFile("image")
	if err != nil {
		web.Error(w, "missing image file", http.StatusBadRequest)
		return
	}
	defer file.Close()

	ext := strings.ToLower(filepath.Ext(header.Filename))
	if ext != ".png" && ext != ".jpg" && ext != ".jpeg" && ext != ".webp" {
		web.Error(w, "unsupported image format", http.StatusBadRequest)
		return
	}
	objectName := fmt.Sprintf("profile_%s_%d%s", userId, time.Now().UnixNano(), ext)

	uploadInfo, err := h.minio.PutObject(r.Context(), "guard-storage", objectName, file, header.Size, minio.PutObjectOptions{
		ContentType: header.Header.Get("Content-Type"),
	})
	if err != nil {
		web.Error(w, "failed to upload image", http.StatusInternalServerError)
		return
	}

	imgURI := fmt.Sprintf("%s/api/v1/avatar/%s", h.cfg.Uri, uploadInfo.Key)

	if err := h.repo.UpdateProfilePicture(r.Context(), repository.UpdateProfilePictureParams{
		ProfilePicture: &imgURI,
		ID:             user.ID,
	}); err != nil {
		web.Error(w, "failed to update profile picture", http.StatusInternalServerError)
		return
	}

	if !user.AvatarVerified {
		if err := h.repo.UserVerifyAvatar(r.Context(), user.ID); err != nil {
			log.Println("ERR: Failed to update avatar_verified:", err)
			web.Error(w, "failed to verify avatar", http.StatusInternalServerError)
			return
		}
	}

	type Response struct {
		URL string `json:"url"`
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	encoder := json.NewEncoder(w)

	w.Header().Set("Content-Type", "application/json")

	if err := encoder.Encode(Response{URL: fmt.Sprintf("%s/avatar/%s", h.cfg.Uri, uploadInfo.Key)}); err != nil {
		web.Error(w, "failed to write response", http.StatusInternalServerError)
	}
}
