import type { generations, images, flows, promptUrlCache, liveScopes } from '@banatie/database';

// Database model types (inferred from Drizzle schema)
export type Generation = typeof generations.$inferSelect;
export type Image = typeof images.$inferSelect;
export type Flow = typeof flows.$inferSelect;
export type PromptUrlCacheEntry = typeof promptUrlCache.$inferSelect;
export type LiveScope = typeof liveScopes.$inferSelect;

// Insert types (for creating new records)
export type NewGeneration = typeof generations.$inferInsert;
export type NewImage = typeof images.$inferInsert;
export type NewFlow = typeof flows.$inferInsert;
export type NewPromptUrlCacheEntry = typeof promptUrlCache.$inferInsert;
export type NewLiveScope = typeof liveScopes.$inferInsert;

// Generation status enum (matches DB schema)
export type GenerationStatus = 'pending' | 'processing' | 'success' | 'failed';

// Image source enum (matches DB schema)
export type ImageSource = 'generated' | 'uploaded';

// Alias scope types (for resolution)
export type AliasScope = 'technical' | 'flow' | 'project';

// Alias resolution result
export interface AliasResolution {
  imageId: string;
  scope: AliasScope;
  flowId?: string;
  image?: Image;
}

// Enhanced generation with related data
export interface GenerationWithRelations extends Generation {
  outputImage?: Image;
  referenceImages?: Image[];
  flow?: Flow;
}

// Enhanced image with related data
export interface ImageWithRelations extends Image {
  generation?: Generation;
  usedInGenerations?: Generation[];
  flow?: Flow;
}

// Enhanced flow with computed counts
export interface FlowWithCounts extends Flow {
  generationCount: number;
  imageCount: number;
  generations?: Generation[];
  images?: Image[];
}

// Enhanced live scope with computed stats
export interface LiveScopeWithStats extends LiveScope {
  currentGenerations: number;
  lastGeneratedAt: Date | null;
  images?: Image[];
}

// Pagination metadata
export interface PaginationMeta {
  total: number;
  limit: number;
  offset: number;
  hasMore: boolean;
}

// Query filters for images
export interface ImageFilters {
  projectId: string;
  flowId?: string | undefined;
  source?: ImageSource | undefined;
  alias?: string | undefined;
  deleted?: boolean | undefined;
}

// Query filters for generations
export interface GenerationFilters {
  projectId: string;
  flowId?: string | undefined;
  status?: GenerationStatus | undefined;
  deleted?: boolean | undefined;
}

// Query filters for flows
export interface FlowFilters {
  projectId: string;
}

// Query filters for live scopes
export interface LiveScopeFilters {
  projectId: string;
  slug?: string | undefined;
}

// Cache statistics
export interface CacheStats {
  hits: number;
  misses: number;
  hitRate: number;
}
