// Copyright © 2018 Banzai Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package file

import (
	"context"
	"os"
	"path"

	"emperror.dev/errors"

	"github.com/bank-vaults/bank-vaults/pkg/kv"
)

type file struct {
	path string
}

// New creates a new kv.Service backed by files, without any encryption
func New(path string) (service kv.Service, err error) {
	service = &file{path: path}

	return
}

func (f *file) Set(_ context.Context, key string, val []byte) error {
	return os.WriteFile(path.Join(f.path, key), val, 0o600)
}

func (f *file) Get(_ context.Context, key string) ([]byte, error) {
	val, err := os.ReadFile(path.Join(f.path, key))
	if os.IsNotExist(err) {
		return nil, kv.NewNotFoundError("key '%s' is not present in file", key)
	}

	return val, errors.WrapIff(err, "failed to read file for key: %s", key)
}
