package memory

import (
	"context"
	"errors"
	"fmt"
	"io/fs"
	"sync"
	"testing/fstest"

	"source.toby3d.me/toby3d/home/internal/domain"
	"source.toby3d.me/toby3d/home/internal/entry"
)

type memoryEntryRepository struct {
	mutex *sync.RWMutex
	files fstest.MapFS
}

func (repo *memoryEntryRepository) Get(_ context.Context, _ domain.Language, p string) (*domain.Page, error) {
	f, err := repo.files.Open(p)
	if err != nil {
		return nil, fmt.Errorf("cannot get entry from memory: %w", err)
	}
	defer f.Close()

	return nil, nil
}

func (repo *memoryEntryRepository) Stat(_ context.Context, l domain.Language, p string) (bool, error) {
	_, err := fs.Stat(repo.files, p)

	return errors.Is(err, fs.ErrExist), nil
}

func NewMemoryEntryRepository(files fstest.MapFS) entry.Repository {
	return &memoryEntryRepository{
		mutex: new(sync.RWMutex),
		files: files,
	}
}
