package services

import (
	"testing"
)

func TestNewStorageService(t *testing.T) {
	// StorageService requires a CredentialsService pointer
	// Test with nil value (basic constructor test)
	service := NewStorageService(nil)
	if service == nil {
		t.Error("Expected service, got nil")
	}
	if service.credentialsService != nil {
		t.Error("Expected nil credentialsService")
	}
}

func TestUploadConfig_Validation(t *testing.T) {
	// Test the UploadConfig struct is properly defined
	cfg := UploadConfig{
		Endpoint:  "https://s3.example.com",
		AccessKey: "access123",
		SecretKey: "secret456",
		Bucket:    "my-bucket",
		Region:    "us-east-1",
	}

	if cfg.Endpoint == "" {
		t.Error("Expected endpoint to be set")
	}
	if cfg.AccessKey == "" {
		t.Error("Expected accessKey to be set")
	}
	if cfg.SecretKey == "" {
		t.Error("Expected secretKey to be set")
	}
	if cfg.Bucket == "" {
		t.Error("Expected bucket to be set")
	}
	if cfg.Region == "" {
		t.Error("Expected region to be set")
	}
}

func TestUploadConfig_DefaultRegion(t *testing.T) {
	// Test that empty region would need defaulting
	cfg := UploadConfig{
		Endpoint:  "https://r2.cloudflare.com",
		AccessKey: "access",
		SecretKey: "secret",
		Bucket:    "bucket",
		Region:    "", // Empty
	}
	_ = cfg.Endpoint
	_ = cfg.AccessKey
	_ = cfg.SecretKey
	_ = cfg.Bucket

	// In the actual getClient, empty region defaults to "auto"
	if cfg.Region != "" {
		t.Error("Region should be empty for this test case")
	}

	// Simulate default
	if cfg.Region == "" {
		cfg.Region = "auto"
	}
	if cfg.Region != "auto" {
		t.Errorf("Expected region='auto', got '%s'", cfg.Region)
	}
}

func TestUploadConfig_IncompleteFields(t *testing.T) {
	// Test incomplete config detection
	incomplete := UploadConfig{
		Endpoint:  "https://s3.example.com",
		AccessKey: "",
		SecretKey: "",
		Bucket:    "bucket",
		Region:    "us-east-1",
	}
	_ = incomplete.Endpoint
	_ = incomplete.Bucket
	_ = incomplete.Region

	// Validation logic that would be in getClient
	if incomplete.AccessKey == "" || incomplete.SecretKey == "" {
		// Would return error
		t.Log("Correctly identified incomplete credentials")
	} else {
		t.Error("Should detect missing credentials")
	}
}
