/*
 * Validates uploaded image files for type and size.
 *
 * Author: Harkamal
 * Date: April 2026
 */

package com.petshop.backend.util;

import com.petshop.backend.exception.BusinessException;
import org.springframework.web.multipart.MultipartFile;

import java.util.Locale;

public final class ImageValidationUtil {

    public static final long DEFAULT_MAX_IMAGE_SIZE = 5 * 1024 * 1024;

    private ImageValidationUtil() {}

    public static void validate(MultipartFile file) {
        validate(file, DEFAULT_MAX_IMAGE_SIZE);
    }

    public static void validate(MultipartFile file, long maxSizeBytes) {
        if (file.isEmpty()) {
            throw new BusinessException("Please select an image to upload");
        }
        if (file.getSize() > maxSizeBytes) {
            throw new BusinessException("Image file size must be less than 5MB");
        }
        String contentType = file.getContentType();
        if (contentType == null) {
            throw new BusinessException("Only JPG, PNG, and GIF images are allowed");
        }
        String normalized = contentType.toLowerCase(Locale.ROOT);
        if (!normalized.equals("image/jpeg") && !normalized.equals("image/png") && !normalized.equals("image/gif")) {
            throw new BusinessException("Only JPG, PNG, and GIF images are allowed");
        }
    }
}
