package com.cfdeployer.config;

import jakarta.servlet.MultipartConfigElement;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;

@Configuration
public class MultipartConfig {

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();

        // Set max file size for chunks (10MB per request is safe)
        factory.setMaxFileSize(DataSize.ofMegabytes(10));
        factory.setMaxRequestSize(DataSize.ofMegabytes(10));

        // Important: Set file size threshold to write to disk immediately
        // Setting to 0 means all uploads go directly to disk, not memory
        factory.setFileSizeThreshold(DataSize.ofBytes(0));

        return factory.createMultipartConfig();
    }
}
