package docstore

import (
	"context"
	"os"
	"path/filepath"
	"strings"

	"github.com/tech/sendico/pkg/merrors"
	"github.com/tech/sendico/pkg/mlogger"
	"go.uber.org/zap"
)

type LocalStore struct {
	logger   mlogger.Logger
	rootPath string
}

func NewLocalStore(logger mlogger.Logger, cfg LocalConfig) (*LocalStore, error) {
	root := strings.TrimSpace(cfg.RootPath)
	if root == "" {
		return nil, merrors.InvalidArgument("docstore: local root_path is empty")
	}
	store := &LocalStore{
		logger:   logger.Named("docstore").Named("local"),
		rootPath: root,
	}
	store.logger.Info("Document storage initialised", zap.String("root_path", root))
	return store, nil
}

func (s *LocalStore) Save(ctx context.Context, key string, data []byte) error {
	if err := ctx.Err(); err != nil {
		return err
	}
	path := filepath.Join(s.rootPath, filepath.Clean(key))
	if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
		s.logger.Warn("Failed to create document directory", zap.Error(err), zap.String("path", path))
		return err
	}
	if err := os.WriteFile(path, data, 0o600); err != nil {
		s.logger.Warn("Failed to write document file", zap.Error(err), zap.String("path", path))
		return err
	}
	return nil
}

func (s *LocalStore) Load(ctx context.Context, key string) ([]byte, error) {
	if err := ctx.Err(); err != nil {
		return nil, err
	}
	path := filepath.Join(s.rootPath, filepath.Clean(key))
	data, err := os.ReadFile(path)
	if err != nil {
		s.logger.Warn("Failed to read document file", zap.Error(err), zap.String("path", path))
		return nil, err
	}
	return data, nil
}

var _ Store = (*LocalStore)(nil)
