package fr

import (
	"io"
	"os"

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

func CloseFile(logger mlogger.Logger, file *os.File) {
	if err := file.Close(); err != nil {
		logger.Warn("Failed to close file", zap.Error(err))
	}
}

func ReadFile(logger mlogger.Logger, filePath string) ([]byte, error) {
	//nolint:gosec // Read path is provided by trusted caller configuration.
	file, err := os.Open(filePath)
	if err != nil {
		logger.Warn("Failed to open file", zap.String("path", filePath), zap.Error(err))
		return nil, err
	}
	defer CloseFile(logger, file)

	data, err := io.ReadAll(file)
	if err != nil {
		logger.Warn("Failed to read file", zap.String("path", filePath), zap.Error(err))
		return nil, err
	}

	return data, nil
}
