package com.caliverse.admin.domain.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectAttributesRequest;
import software.amazon.awssdk.services.s3.model.GetObjectAttributesResponse;
import software.amazon.awssdk.services.s3.model.S3Exception;

import java.util.Optional;

@Slf4j
@Service
@ConditionalOnProperty(name = "amazon.s3.enabled", havingValue = "true")
@RequiredArgsConstructor
public class S3Service {
    @Autowired(required = false)
    private final S3Client s3Client;

    @Value("${amazon.s3.bucket-name}")
    private String bucketName;

    public Optional<GetObjectAttributesResponse> getObjectMetadata(String key) {
        try {
            GetObjectAttributesRequest request = GetObjectAttributesRequest.builder()
                    .bucket(bucketName)
                    .key(key)
                    .build();

            GetObjectAttributesResponse response = s3Client.getObjectAttributes(request);
            return Optional.of(response);
        } catch (S3Exception e) {
            log.error("Error retrieving object metadata: {}", e.getMessage());
            return Optional.empty();
        }
    }
}
