import boto3
from botocore.client import Config
from typing import Any, Optional

class FileUploadService:
    """Service to handle file uploads to MinIO/S3."""

    def __init__(
        self,
        endpoint_url: str,
        access_key: str,
        secret_key: str,
        bucket_name: str,
    ):
        self.s3 = boto3.client(
            "s3",
            endpoint_url=endpoint_url,
            aws_access_key_id=access_key,
            aws_secret_access_key=secret_key,
            config=Config(signature_version="s3v4"),
        )
        self.bucket_name = bucket_name

    def upload_file(self, file_content: bytes, object_name: str, content_type: Optional[str] = None):
        """Upload a file to the defined bucket."""
        extra_args = {}
        if content_type:
            extra_args["ContentType"] = content_type
        
        self.s3.put_object(
            Bucket=self.bucket_name,
            Key=object_name,
            Body=file_content,
            **extra_args
        )

    def generate_presigned_url(self, object_name: str, expiration: int = 3600) -> str:
        """Generate a presigned URL for downloading an object."""
        return self.s3.generate_presigned_url(
            "get_object",
            Params={"Bucket": self.bucket_name, "Key": object_name},
            ExpiresIn=expiration,
        )
