package secret

import (
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"encoding/base64"
	"fmt"
	"os"
	"path/filepath"
	"sync"
)

const keyFileName = ".muyue_key"

var (
	masterKey []byte
	once      sync.Once
	keyErr    error
)

func getKey() ([]byte, error) {
	once.Do(func() {
		keyPath := keyPath()
		data, err := os.ReadFile(keyPath)
		if err == nil && len(data) == 32 {
			masterKey = data
			return
		}
		masterKey = make([]byte, 32)
		if _, err := rand.Read(masterKey); err != nil {
			keyErr = fmt.Errorf("generate key: %w", err)
			return
		}
		keyDir := filepath.Dir(keyPath)
		os.MkdirAll(keyDir, 0700)
		if err := os.WriteFile(keyPath, masterKey, 0600); err != nil {
			keyErr = fmt.Errorf("write key: %w", err)
			return
		}
	})
	return masterKey, keyErr
}

func keyPath() string {
	home, err := os.UserHomeDir()
	if err != nil {
		return ".muyue_key"
	}
	return filepath.Join(home, keyFileName)
}

func Encrypt(plaintext string) (string, error) {
	if plaintext == "" {
		return "", nil
	}
	key, err := getKey()
	if err != nil {
		return "", err
	}
	block, err := aes.NewCipher(key)
	if err != nil {
		return "", err
	}
	aesgcm, err := cipher.NewGCM(block)
	if err != nil {
		return "", err
	}
	nonce := make([]byte, aesgcm.NonceSize())
	if _, err := rand.Read(nonce); err != nil {
		return "", err
	}
	ciphertext := aesgcm.Seal(nonce, nonce, []byte(plaintext), nil)
	return base64.StdEncoding.EncodeToString(ciphertext), nil
}

func Decrypt(encoded string) (string, error) {
	if encoded == "" {
		return "", nil
	}
	data, err := base64.StdEncoding.DecodeString(encoded)
	if err != nil {
		return "", fmt.Errorf("decode: %w", err)
	}
	key, err := getKey()
	if err != nil {
		return "", err
	}
	block, err := aes.NewCipher(key)
	if err != nil {
		return "", err
	}
	aesgcm, err := cipher.NewGCM(block)
	if err != nil {
		return "", err
	}
	nonceSize := aesgcm.NonceSize()
	if len(data) < nonceSize {
		return "", fmt.Errorf("ciphertext too short")
	}
	nonce, ciphertext := data[:nonceSize], data[nonceSize:]
	plaintext, err := aesgcm.Open(nil, nonce, ciphertext, nil)
	if err != nil {
		return "", fmt.Errorf("decrypt: %w", err)
	}
	return string(plaintext), nil
}

func IsEncrypted(s string) bool {
	if s == "" {
		return false
	}
	_, err := base64.StdEncoding.DecodeString(s)
	if err != nil {
		return false
	}
	decrypted, err := Decrypt(s)
	return err == nil && decrypted != ""
}

func resetForTesting() {
	masterKey = nil
	keyErr = nil
	once = sync.Once{}
}
