import api from './api';

export interface ImageProcessRequest {
  image_url: string;
  operation?: string;
  width?: number;
  height?: number;
  crop_x?: number;
  crop_y?: number;
  crop_width?: number;
  crop_height?: number;
  rotation?: number;
  flip_h?: boolean;
  flip_v?: boolean;
  brightness?: number;
  contrast?: number;
  saturation?: number;
  blur?: number;
  sharpen?: number;
  grayscale?: boolean;
  quality?: number;
}

export interface QuickEditRequest {
  image_url: string;
  width?: number;
  rotation?: number;
  flip_h?: boolean;
  flip_v?: boolean;
  brightness?: number;
  contrast?: number;
  saturation?: number;
  grayscale?: boolean;
  quality?: number;
}

export interface ImageProcessResponse {
  url: string;
  format?: string;
}

/**
 * Process image with various operations (crop, resize, filters, etc.)
 */
export const processImage = async (request: ImageProcessRequest): Promise<ImageProcessResponse> => {
  const response = await api.post('/image-processing/process', request);
  return response.data;
};

/**
 * Quick edit for common operations in one call
 */
export const quickEditImage = async (request: QuickEditRequest): Promise<ImageProcessResponse> => {
  const response = await api.post('/image-processing/quick-edit', request);
  return response.data;
};

/**
 * Crop and upload image file
 */
export const cropAndUpload = async (
  file: File,
  cropData?: { x: number; y: number; width: number; height: number },
  quality = 85,
  maxWidth = 1500
): Promise<ImageProcessResponse> => {
  const formData = new FormData();
  formData.append('image', file);
  
  if (cropData) {
    formData.append('crop_data', JSON.stringify(cropData));
  }
  
  formData.append('quality', quality.toString());
  formData.append('max_width', maxWidth.toString());
  
  const response = await api.post('/image-processing/crop-upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
  
  return response.data;
};

/**
 * Resize image to specific width (maintains aspect ratio)
 */
export const resizeImage = async (imageUrl: string, width: number, quality = 85): Promise<ImageProcessResponse> => {
  return quickEditImage({
    image_url: imageUrl,
    width,
    quality,
  });
};

/**
 * Apply filters to image
 */
export const applyFilters = async (
  imageUrl: string,
  filters: {
    brightness?: number;
    contrast?: number;
    saturation?: number;
    blur?: number;
    grayscale?: boolean;
  },
  quality = 85
): Promise<ImageProcessResponse> => {
  return quickEditImage({
    image_url: imageUrl,
    ...filters,
    quality,
  });
};

/**
 * Rotate image (90, 180, 270 degrees)
 */
export const rotateImage = async (imageUrl: string, rotation: number, quality = 85): Promise<ImageProcessResponse> => {
  return quickEditImage({
    image_url: imageUrl,
    rotation,
    quality,
  });
};

/**
 * Flip image horizontally or vertically
 */
export const flipImage = async (
  imageUrl: string,
  flipH: boolean,
  flipV: boolean,
  quality = 85
): Promise<ImageProcessResponse> => {
  return quickEditImage({
    image_url: imageUrl,
    flip_h: flipH,
    flip_v: flipV,
    quality,
  });
};
