/**
 * AeThex Asset Library - Type Definitions
 * Types for managing game assets (models, textures, audio, etc.)
 */

export type AssetType =
  | 'model'      // 3D models (GLB, FBX, OBJ)
  | 'texture'    // Images (PNG, JPG, WEBP)
  | 'audio'      // Sounds (MP3, WAV, OGG)
  | 'animation'  // Animation files
  | 'script'     // Code snippets
  | 'prefab'     // Pre-made game objects
  | 'material'   // Material definitions
  | 'particle'   // Particle effects
  | 'ui'         // UI elements
  | 'data';      // Data files (JSON, CSV)

export type AssetCategory =
  | 'characters'
  | 'environments'
  | 'props'
  | 'vehicles'
  | 'weapons'
  | 'effects'
  | 'ui-elements'
  | 'sounds'
  | 'music'
  | 'scripts'
  | 'materials'
  | 'uncategorized';

export interface AssetMetadata {
  // File info
  fileName: string;
  fileSize: number;
  mimeType: string;
  extension: string;

  // Asset-specific metadata
  width?: number;          // For images
  height?: number;         // For images
  duration?: number;       // For audio/video
  sampleRate?: number;     // For audio
  channels?: number;       // For audio
  polyCount?: number;      // For 3D models
  vertexCount?: number;    // For 3D models
  hasAnimations?: boolean; // For 3D models
  boneCount?: number;      // For 3D models
  materialCount?: number;  // For 3D models
}

export interface Asset {
  id: string;
  name: string;
  type: AssetType;
  category: AssetCategory;
  description?: string;
  tags: string[];

  // File data
  file?: File;
  dataUrl?: string;        // Base64 for small files
  blobUrl?: string;        // Object URL for large files
  thumbnailUrl?: string;   // Preview image

  // Metadata
  metadata: AssetMetadata;

  // Organization
  folderId?: string;
  favorite: boolean;

  // Timestamps
  createdAt: Date;
  updatedAt: Date;

  // Platform compatibility
  platforms: ('roblox' | 'uefn' | 'spatial' | 'universal')[];
}

export interface AssetFolder {
  id: string;
  name: string;
  parentId?: string;
  color?: string;
  icon?: string;
  createdAt: Date;
}

export interface AssetUploadOptions {
  name?: string;
  category?: AssetCategory;
  tags?: string[];
  folderId?: string;
  generateThumbnail?: boolean;
}

export interface AssetFilter {
  type?: AssetType | AssetType[];
  category?: AssetCategory | AssetCategory[];
  tags?: string[];
  folderId?: string;
  favorite?: boolean;
  search?: string;
}

export interface AssetSortOptions {
  field: 'name' | 'createdAt' | 'updatedAt' | 'fileSize' | 'type';
  direction: 'asc' | 'desc';
}

// File format mappings
export const FILE_TYPE_MAP: Record<string, AssetType> = {
  // Models
  '.glb': 'model',
  '.gltf': 'model',
  '.fbx': 'model',
  '.obj': 'model',
  '.dae': 'model',

  // Textures
  '.png': 'texture',
  '.jpg': 'texture',
  '.jpeg': 'texture',
  '.webp': 'texture',
  '.svg': 'texture',
  '.gif': 'texture',
  '.bmp': 'texture',
  '.tga': 'texture',

  // Audio
  '.mp3': 'audio',
  '.wav': 'audio',
  '.ogg': 'audio',
  '.m4a': 'audio',
  '.flac': 'audio',

  // Animations
  '.bvh': 'animation',
  '.anim': 'animation',

  // Scripts
  '.lua': 'script',
  '.verse': 'script',
  '.ts': 'script',
  '.js': 'script',

  // Data
  '.json': 'data',
  '.csv': 'data',
  '.xml': 'data',
};

export const ASSET_TYPE_ICONS: Record<AssetType, string> = {
  model: 'Box',
  texture: 'Image',
  audio: 'Volume2',
  animation: 'Play',
  script: 'FileCode',
  prefab: 'Package',
  material: 'Palette',
  particle: 'Sparkles',
  ui: 'Layout',
  data: 'Database',
};

export const ASSET_TYPE_COLORS: Record<AssetType, string> = {
  model: '#3b82f6',      // Blue
  texture: '#22c55e',    // Green
  audio: '#f97316',      // Orange
  animation: '#a855f7',  // Purple
  script: '#eab308',     // Yellow
  prefab: '#ec4899',     // Pink
  material: '#14b8a6',   // Teal
  particle: '#f43f5e',   // Rose
  ui: '#8b5cf6',         // Violet
  data: '#6b7280',       // Gray
};

export const CATEGORY_LABELS: Record<AssetCategory, string> = {
  characters: 'Characters',
  environments: 'Environments',
  props: 'Props',
  vehicles: 'Vehicles',
  weapons: 'Weapons',
  effects: 'Effects',
  'ui-elements': 'UI Elements',
  sounds: 'Sound Effects',
  music: 'Music',
  scripts: 'Scripts',
  materials: 'Materials',
  uncategorized: 'Uncategorized',
};

// Helper functions
export function getAssetTypeFromFile(file: File): AssetType {
  const extension = '.' + file.name.split('.').pop()?.toLowerCase();
  return FILE_TYPE_MAP[extension] || 'data';
}

export function formatFileSize(bytes: number): string {
  if (bytes === 0) return '0 B';
  const k = 1024;
  const sizes = ['B', 'KB', 'MB', 'GB'];
  const i = Math.floor(Math.log(bytes) / Math.log(k));
  return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i];
}

export function getAcceptedFileTypes(type?: AssetType): string {
  if (!type) {
    return Object.keys(FILE_TYPE_MAP).join(',');
  }

  return Object.entries(FILE_TYPE_MAP)
    .filter(([_, t]) => t === type)
    .map(([ext]) => ext)
    .join(',');
}

export function generateAssetId(): string {
  return `asset-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
}
