import { Request } from 'express';

// API Request/Response types
export interface GenerateImageRequest {
  prompt: string;
  filename: string;
}

export interface TextToImageRequest {
  prompt: string;
  filename: string;
  aspectRatio?: string; // Gemini aspect ratio format (e.g., "1:1", "16:9", "3:2")
  autoEnhance?: boolean; // Defaults to true
  enhancementOptions?: {
    template?:
      | 'photorealistic'
      | 'illustration'
      | 'minimalist'
      | 'sticker'
      | 'product'
      | 'comic'
      | 'general'; // Defaults to "photorealistic"
  };
  meta?: {
    tags?: string[]; // Optional array of tags for tracking/grouping (not stored, only logged)
  };
}

export interface GenerateImageResponse {
  success: boolean;
  message: string;
  data?: {
    filename: string;
    filepath: string;
    url?: string; // API URL for accessing the image
    description?: string;
    model: string;
    generatedAt: string;
    geminiParams?: GeminiParams; // Gemini SDK parameters used for generation
    promptEnhancement?: {
      originalPrompt: string;
      enhancedPrompt: string;
      detectedLanguage?: string;
      appliedTemplate?: string;
      enhancements: string[];
    };
  };
  error?: string;
}

// Extended Express Request with file uploads
export interface GenerateImageRequestWithFiles extends Request {
  body: GenerateImageRequest;
  files?: Express.Multer.File[];
}

// Image generation service types
export interface ImageGenerationOptions {
  prompt: string;
  imageId: string; // UUID used as filename in storage (same as image.id in DB)
  referenceImages?: ReferenceImage[];
  aspectRatio?: string;
  orgSlug?: string;
  projectSlug?: string;
  userId?: string;
  meta?: {
    tags?: string[];
  };
}

export interface ReferenceImage {
  buffer: Buffer;
  mimetype: string;
  originalname: string;
}

export interface GeminiParams {
  model: string;
  config: {
    responseModalities: string[];
    imageConfig?: {
      aspectRatio?: string;
    };
  };
  contentsStructure: {
    role: string;
    partsCount: number;
    hasReferenceImages: boolean;
  };
}

export interface ImageGenerationResult {
  success: boolean;
  imageId?: string; // UUID filename (same as image.id in DB)
  filepath?: string;
  url?: string; // CDN URL for accessing the image
  size?: number; // File size in bytes
  description?: string;
  model: string;
  geminiParams?: GeminiParams; // Gemini SDK parameters used for generation
  error?: string;
  errorCode?: string; // Gemini-specific error code (GEMINI_RATE_LIMIT, GEMINI_TIMEOUT, etc.)
  errorType?: 'generation' | 'storage'; // Distinguish between generation and storage errors
  generatedImageData?: GeneratedImageData; // Available when generation succeeds but storage fails
}

// Intermediate result after image generation, before storage
export interface GeneratedImageData {
  buffer: Buffer;
  mimeType: string;
  fileExtension: string;
  description?: string;
  width: number;
  height: number;
}

// Logging types
export interface LogContext {
  requestId: string;
  endpoint: string;
  method: string;
  timestamp: string;
}

// Prompt Enhancement types
export interface PromptEnhancementRequest {
  prompt: string;
  options?: {
    template?:
      | 'photorealistic'
      | 'illustration'
      | 'minimalist'
      | 'sticker'
      | 'product'
      | 'comic'
      | 'general'; // Defaults to "photorealistic"
  };
}

export interface PromptEnhancementResponse {
  success: boolean;
  originalPrompt: string;
  enhancedPrompt?: string;
  detectedLanguage?: string;
  appliedTemplate?: string;
  metadata?: {
    style?: string;
    aspectRatio?: string;
    enhancements: string[];
  };
  error?: string;
}

// Enhanced Generate Request (with auto-enhancement option)
export interface EnhancedGenerateImageRequest extends GenerateImageRequest {
  aspectRatio?: string; // Gemini aspect ratio format (e.g., "1:1", "16:9", "3:2")
  autoEnhance?: boolean; // Defaults to true
  enhancementOptions?: {
    template?:
      | 'photorealistic'
      | 'illustration'
      | 'minimalist'
      | 'sticker'
      | 'product'
      | 'comic'
      | 'general'; // Defaults to "photorealistic"
  };
  meta?: {
    tags?: string[];
  };
}

// Upload file types
export interface UploadFileRequest {
  metadata?: {
    description?: string;
    tags?: string[];
  };
}

export interface UploadFileResponse {
  success: boolean;
  message: string;
  data?: {
    filename: string;
    originalName: string;
    path: string;
    url: string;
    size: number;
    contentType: string;
    uploadedAt: string;
  };
  error?: string;
}

// Environment configuration
export interface Config {
  port: number;
  geminiApiKey: string;
  resultsDir: string;
  uploadsDir: string;
  maxFileSize: number;
  maxFiles: number;
}
