package storage

import (
	"context"
	"io"
	"log"
	"net/url"

	"gitea.local/admin/hspguard/internal/config"
	"github.com/minio/minio-go/v7"
	"github.com/minio/minio-go/v7/pkg/credentials"
)

type FileStorage struct {
	client *minio.Client
}

func New(cfg *config.AppConfig) *FileStorage {
	client, err := minio.New(cfg.Minio.Endpoint, &minio.Options{
		Creds:  credentials.NewStaticV4(cfg.Minio.AccessKey, cfg.Minio.SecretKey, ""),
		Secure: false,
	})
	if err != nil {
		log.Fatalln("Failed to create minio client:", err)
		return nil
	}

	return &FileStorage{
		client,
	}
}

func (fs *FileStorage) PutObject(ctx context.Context, bucketName string, objectName string, reader io.Reader, size int64, opts minio.PutObjectOptions) (minio.UploadInfo, error) {
	return fs.client.PutObject(ctx, bucketName, objectName, reader, size, opts)
}

func (fs *FileStorage) GetObject(ctx context.Context, bucketName string, objectName string, opts minio.GetObjectOptions) (*minio.Object, error) {
	return fs.client.GetObject(ctx, bucketName, objectName, opts)
}

func (fs *FileStorage) EndpointURL() *url.URL {
	return fs.client.EndpointURL()
}
