package routes

import (
	"encoding/base64"
	"os"
	"path/filepath"
	"strings"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/golang-jwt/jwt/v5"
	"stereo.cat/backend/internal/auth"
	"stereo.cat/backend/internal/types"
)

func RegisterFileRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
	api.POST("/upload", auth.JwtMiddleware(cfg.JWTSecret), func(c *gin.Context) {
		claims := c.MustGet("claims").(jwt.MapClaims)
		user := claims["user"].(auth.User)

		uid := user.ID
		if uid == "" {
			c.JSON(401, gin.H{"error": "unauthorized"})
			return
		}

		file, err := c.FormFile("file")
		if err != nil {
			c.JSON(400, gin.H{"error": "file is required"})
			return
		}

		filePath := filepath.Join(cfg.ImagePath, uid, file.Filename)

		if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
			c.JSON(500, gin.H{"error": "failed to create directory"})
			return
		}

		if err := c.SaveUploadedFile(file, filePath); err != nil {
			c.JSON(500, gin.H{"error": "failed to save file"})
			return
		}

		b64, err := convertToBase64(filePath)
		if err != nil {
		}

		fileMeta := types.File{
			ID:        uid + "_" + file.Filename,
			Path:      filePath,
			Owner:     uid,
			CreatedAt: time.Now(),
			Base64:    b64,
		}

		if err := cfg.Database.Create(&fileMeta).Error; err != nil {
			c.JSON(500, gin.H{"error": "failed to save file metadata"})
			return
		}

		c.JSON(200, gin.H{"message": "file uploaded successfully", "file_id": fileMeta.ID})
	})

	api.GET("/:name", func(c *gin.Context) {
		name := c.Param("name")
		parts := strings.SplitN(name, "_", 2)
		if len(parts) != 2 {
			c.JSON(400, gin.H{"error": "invalid file name"})
			return
		}
		uid, filename := parts[0], parts[1]
		path := filepath.Join(cfg.ImagePath, uid, filename)
		if _, err := os.Stat(path); err != nil {
			c.JSON(404, gin.H{"error": "file not found"})
			return
		}
		c.File(path)
	})

	api.GET("/list", auth.JwtMiddleware(cfg.JWTSecret), func(c *gin.Context) {
		claims := c.MustGet("claims").(jwt.MapClaims)
		user := claims["user"].(auth.User)

		var files []types.File
		if err := cfg.Database.Where("owner = ?", user.ID).Find(&files).Error; err != nil {
			c.JSON(500, gin.H{"error": "failed to retrieve files"})
			return
		}

		c.JSON(200, files)
	})
}

func convertToBase64(filePath string) (string, error) {
	file, err := os.ReadFile(filePath)
	if err != nil {
		return "", err
	}

	b64 := base64.StdEncoding.EncodeToString(file)
	return b64, nil
}