package storage

import (
	"encoding/binary"
	"fmt"
	"gf-lt/models"
	"sort"
	"unsafe"

	"github.com/jmoiron/sqlx"
)

type VectorRepo interface {
	WriteVector(*models.VectorRow) error
	SearchClosest(q []float32, limit int) ([]models.VectorRow, error)
	ListFiles() ([]string, error)
	RemoveEmbByFileName(filename string) error
	DB() *sqlx.DB
}

// SerializeVector converts []float32 to binary blob
func SerializeVector(vec []float32) []byte {
	buf := make([]byte, len(vec)*4) // 4 bytes per float32
	for i, v := range vec {
		binary.LittleEndian.PutUint32(buf[i*4:], mathFloat32bits(v))
	}
	return buf
}

// DeserializeVector converts binary blob back to []float32
func DeserializeVector(data []byte) []float32 {
	count := len(data) / 4
	vec := make([]float32, count)
	for i := 0; i < count; i++ {
		vec[i] = mathBitsToFloat32(binary.LittleEndian.Uint32(data[i*4:]))
	}
	return vec
}

// mathFloat32bits and mathBitsToFloat32 are helpers to convert between float32 and uint32
func mathFloat32bits(f float32) uint32 {
	return binary.LittleEndian.Uint32((*(*[4]byte)(unsafe.Pointer(&f)))[:4])
}

func mathBitsToFloat32(b uint32) float32 {
	return *(*float32)(unsafe.Pointer(&b))
}

func fetchTableName(emb []float32) (string, error) {
	switch len(emb) {
	case 768:
		return "embeddings_768", nil
	default:
		return "", fmt.Errorf("no table for the size of %d", len(emb))
	}
}

func (p ProviderSQL) WriteVector(row *models.VectorRow) error {
	tableName, err := fetchTableName(row.Embeddings)
	if err != nil {
		return err
	}
	serializedEmbeddings := SerializeVector(row.Embeddings)
	query := fmt.Sprintf("INSERT INTO %s(embeddings, slug, raw_text, filename) VALUES (?, ?, ?, ?)", tableName)
	_, err = p.db.Exec(query, serializedEmbeddings, row.Slug, row.RawText, row.FileName)
	return err
}

func (p ProviderSQL) SearchClosest(q []float32, limit int) ([]models.VectorRow, error) {
	tableName, err := fetchTableName(q)
	if err != nil {
		return nil, err
	}
	querySQL := "SELECT embeddings, slug, raw_text, filename FROM " + tableName
	rows, err := p.db.Query(querySQL)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	type SearchResult struct {
		vector   models.VectorRow
		distance float32
	}
	var allResults []SearchResult
	for rows.Next() {
		var (
			embeddingsBlob          []byte
			slug, rawText, fileName string
		)
		if err := rows.Scan(&embeddingsBlob, &slug, &rawText, &fileName); err != nil {
			continue
		}

		storedEmbeddings := DeserializeVector(embeddingsBlob)

		// Calculate cosine similarity (returns value between -1 and 1, where 1 is most similar)
		similarity := cosineSimilarity(q, storedEmbeddings)
		distance := 1 - similarity // Convert to distance where 0 is most similar

		result := SearchResult{
			vector: models.VectorRow{
				Embeddings: storedEmbeddings,
				Slug:       slug,
				RawText:    rawText,
				FileName:   fileName,
			},
			distance: distance,
		}
		allResults = append(allResults, result)
	}
	// Sort by distance
	sort.Slice(allResults, func(i, j int) bool {
		return allResults[i].distance < allResults[j].distance
	})
	// Truncate to limit
	if len(allResults) > limit {
		allResults = allResults[:limit]
	}
	// Convert back to VectorRow slice
	results := make([]models.VectorRow, len(allResults))
	for i, result := range allResults {
		result.vector.Distance = result.distance
		results[i] = result.vector
	}
	return results, nil
}

// cosineSimilarity calculates the cosine similarity between two vectors
func cosineSimilarity(a, b []float32) float32 {
	if len(a) != len(b) {
		return 0.0
	}
	var dotProduct, normA, normB float32
	for i := 0; i < len(a); i++ {
		dotProduct += a[i] * b[i]
		normA += a[i] * a[i]
		normB += b[i] * b[i]
	}
	if normA == 0 || normB == 0 {
		return 0.0
	}
	return dotProduct / (sqrt(normA) * sqrt(normB))
}

// sqrt returns the square root of a float32
func sqrt(f float32) float32 {
	// A simple implementation of square root using Newton's method
	if f == 0 {
		return 0
	}
	guess := f / 2
	for i := 0; i < 10; i++ { // 10 iterations should be enough for good precision
		guess = (guess + f/guess) / 2
	}
	return guess
}

func (p ProviderSQL) ListFiles() ([]string, error) {
	query := "SELECT DISTINCT filename FROM embeddings_768"
	rows, err := p.db.Query(query)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var allFiles []string
	for rows.Next() {
		var filename string
		if err := rows.Scan(&filename); err != nil {
			continue
		}
		allFiles = append(allFiles, filename)
	}
	return allFiles, nil
}

func (p ProviderSQL) RemoveEmbByFileName(filename string) error {
	query := "DELETE FROM embeddings_768 WHERE filename = ?"
	_, err := p.db.Exec(query, filename)
	return err
}
