package files

import (
	"crypto/rand"
	"encoding/hex"
	"errors"
	"io"
	"os"
	"path/filepath"
	"strings"

	"gorm.io/gorm"
	"senlinai-agent/backend/internal/models"
)

type Service struct {
	root          string
	db            *gorm.DB
	createTemp    func(string, string) (stagedFile, error)
	publish       func(string, string) error
	remove        func(string) error
	newStorageKey func() (string, error)
}

type stagedFile interface {
	io.Writer
	Close() error
	Name() string
}

type StoredFile struct {
	OriginalName string
	StorageKey   string
	RelativePath string
	AbsolutePath string
}

// NewService 集中持有存储根目录；任何服务端本地路径都只能由该服务构造。
func NewService(root string, databases ...*gorm.DB) *Service {
	var database *gorm.DB
	if len(databases) > 0 {
		database = databases[0]
	}
	return &Service{
		root: root, db: database,
		createTemp:    func(directory, pattern string) (stagedFile, error) { return os.CreateTemp(directory, pattern) },
		publish:       os.Link,
		remove:        os.Remove,
		newStorageKey: randomStorageKey,
	}
}

// Save 使用项目公开 identity 和随机 opaque key 定位文件；硬链接发布提供跨请求的排他创建语义。
func (s *Service) Save(projectIdentity string, originalName string, content io.Reader) (StoredFile, error) {
	cleanName := filepath.Base(strings.ReplaceAll(strings.TrimSpace(originalName), "\\", "/"))
	if cleanName == "." || cleanName == "" {
		cleanName = "upload.bin"
	}
	projectSegment := filepath.Base(strings.ReplaceAll(strings.TrimSpace(projectIdentity), "\\", "/"))
	if projectSegment == "." || projectSegment == "" || projectSegment != strings.TrimSpace(projectIdentity) {
		return StoredFile{}, ErrSourcePathRequired
	}
	directoryRelative := filepath.ToSlash(filepath.Join("projects", projectSegment))
	directoryAbsolute, err := s.absolutePath(directoryRelative)
	if err != nil {
		return StoredFile{}, err
	}
	if err := os.MkdirAll(directoryAbsolute, 0o755); err != nil {
		return StoredFile{}, err
	}
	// 临时文件与最终文件位于同一目录，完整关闭后再排他发布，避免暴露半写入内容。
	file, err := s.createTemp(directoryAbsolute, ".upload-*")
	if err != nil {
		return StoredFile{}, err
	}
	if _, err := io.Copy(file, content); err != nil {
		_ = file.Close()
		s.cleanupOwnedFiles(file.Name())
		return StoredFile{}, err
	}
	if err := file.Close(); err != nil {
		s.cleanupOwnedFiles(file.Name())
		return StoredFile{}, err
	}
	for attempt := 0; attempt < storageKeyAttempts; attempt++ {
		storageKey, err := s.newStorageKey()
		if err != nil {
			s.cleanupOwnedFiles(file.Name())
			return StoredFile{}, err
		}
		relative := filepath.ToSlash(filepath.Join(directoryRelative, storageKey))
		absolute, err := s.absolutePath(relative)
		if err != nil {
			s.cleanupOwnedFiles(file.Name())
			return StoredFile{}, err
		}
		if err := s.publish(file.Name(), absolute); err != nil {
			if errors.Is(err, os.ErrExist) {
				continue
			}
			s.cleanupOwnedFiles(file.Name())
			return StoredFile{}, err
		}
		if err := s.remove(file.Name()); err != nil {
			// final path 由本次排他发布创建，因此这里只清理本次请求拥有的两个路径。
			s.cleanupOwnedFiles(file.Name(), absolute)
			return StoredFile{}, err
		}
		return StoredFile{OriginalName: cleanName, StorageKey: storageKey, RelativePath: relative, AbsolutePath: absolute}, nil
	}
	s.cleanupOwnedFiles(file.Name())
	return StoredFile{}, ErrStorageKeyCollision
}

// Remove 只依据 Service 生成的相对路径定位文件，并尽量移除直至存储根目录的空父目录。
func (s *Service) Remove(stored StoredFile) error {
	absolute, err := s.absolutePath(stored.RelativePath)
	if err != nil {
		return err
	}
	if err := s.remove(absolute); err != nil && !errors.Is(err, os.ErrNotExist) {
		return err
	}
	root, err := filepath.Abs(s.root)
	if err != nil {
		return err
	}
	for directory := filepath.Dir(absolute); directory != root; directory = filepath.Dir(directory) {
		entries, err := os.ReadDir(directory)
		if errors.Is(err, os.ErrNotExist) {
			continue
		}
		if err != nil {
			return err
		}
		if len(entries) > 0 {
			return nil
		}
		if err := s.remove(directory); err != nil && !errors.Is(err, os.ErrNotExist) {
			return err
		}
	}
	return nil
}

func (s *Service) absolutePath(relativePath string) (string, error) {
	cleaned := filepath.Clean(filepath.FromSlash(strings.TrimSpace(relativePath)))
	if cleaned == "." || filepath.IsAbs(cleaned) || cleaned == ".." || strings.HasPrefix(cleaned, ".."+string(filepath.Separator)) {
		return "", ErrSourcePathRequired
	}
	root, err := filepath.Abs(s.root)
	if err != nil {
		return "", err
	}
	absolute, err := filepath.Abs(filepath.Join(root, cleaned))
	if err != nil {
		return "", err
	}
	relativeToRoot, err := filepath.Rel(root, absolute)
	if err != nil || relativeToRoot == ".." || strings.HasPrefix(relativeToRoot, ".."+string(filepath.Separator)) {
		return "", ErrSourcePathRequired
	}
	return absolute, nil
}

func (s *Service) cleanupOwnedFiles(paths ...string) {
	for _, path := range paths {
		_ = s.remove(path)
	}
}

func (s *Service) database() *gorm.DB {
	if s.db != nil {
		return s.db
	}
	return models.DBService
}

var (
	ErrSourceTitleRequired = errors.New("source title is required")
	ErrSourcePathRequired  = errors.New("source file path is required")
	ErrStorageKeyCollision = errors.New("unable to allocate unique storage key")
)

const storageKeyAttempts = 8

func randomStorageKey() (string, error) {
	buffer := make([]byte, 16)
	if _, err := rand.Read(buffer); err != nil {
		return "", err
	}
	return hex.EncodeToString(buffer), nil
}

// CreateSource 只持久化 Save 产生的相对路径，不接受 handler 自行拼接本地路径。
func (s *Service) CreateSource(ownerID uint, project *models.SenlinAgentProject, title string, stored StoredFile) (*models.SenlinAgentSource, error) {
	title = strings.TrimSpace(title)
	if title == "" {
		title = stored.OriginalName
	}
	if title == "" {
		return nil, ErrSourceTitleRequired
	}
	relativePath := filepath.ToSlash(strings.TrimSpace(stored.RelativePath))
	if relativePath == "" || filepath.IsAbs(relativePath) {
		return nil, ErrSourcePathRequired
	}
	source := &models.SenlinAgentSource{
		ProjectID: project.ID, ProjectIdentity: project.Identity, CreatedBy: ownerID,
		Kind: "file", Title: title, FilePath: relativePath,
	}
	if err := s.database().Create(source).Error; err != nil {
		return nil, err
	}
	return source, nil
}
