/**
 * AeThex Avatar Templates
 * Pre-configured avatar presets and styles for different platforms
 */

import { AvatarPlatformId } from './avatar-platforms';

export interface AvatarTemplate {
  id: string;
  name: string;
  description: string;
  thumbnail: string;
  category: 'humanoid' | 'stylized' | 'anime' | 'robot' | 'creature' | 'custom';
  style: 'realistic' | 'cartoon' | 'anime' | 'lowpoly' | 'voxel' | 'chibi';
  platforms: AvatarPlatformId[];
  features: string[];
  polyCount: 'low' | 'medium' | 'high' | 'very-high';
  rigged: boolean;
  animated: boolean;
  blendShapes: boolean;
  tags: string[];
}

export interface AvatarPreset {
  id: string;
  name: string;
  platform: AvatarPlatformId;
  description: string;
  settings: {
    targetPolygons: number;
    targetBones: number;
    targetMaterials: number;
    targetTextureSize: number;
    preserveBlendShapes: boolean;
    optimizeForVR: boolean;
    generateLODs: boolean;
  };
}

export const avatarTemplates: AvatarTemplate[] = [
  // Universal Templates
  {
    id: 'universal-humanoid',
    name: 'Universal Humanoid',
    description: 'Standard humanoid avatar compatible with all platforms',
    thumbnail: '/templates/universal-humanoid.png',
    category: 'humanoid',
    style: 'realistic',
    platforms: ['universal', 'vrchat', 'roblox', 'recroom', 'spatial', 'sandbox'],
    features: ['Full body', 'Finger bones', 'Face rig', 'Eye tracking ready'],
    polyCount: 'medium',
    rigged: true,
    animated: true,
    blendShapes: true,
    tags: ['starter', 'universal', 'humanoid'],
  },
  {
    id: 'universal-stylized',
    name: 'Stylized Character',
    description: 'Stylized human character with exaggerated proportions',
    thumbnail: '/templates/universal-stylized.png',
    category: 'stylized',
    style: 'cartoon',
    platforms: ['universal', 'roblox', 'recroom', 'sandbox'],
    features: ['Cartoon proportions', 'Simple rig', 'Expressive face'],
    polyCount: 'low',
    rigged: true,
    animated: true,
    blendShapes: true,
    tags: ['stylized', 'cartoon', 'beginner'],
  },
  {
    id: 'universal-anime',
    name: 'Anime Character',
    description: 'Anime-style humanoid with VRM-ready setup',
    thumbnail: '/templates/universal-anime.png',
    category: 'anime',
    style: 'anime',
    platforms: ['universal', 'vrchat', 'spatial', 'neos', 'resonite', 'chilloutvr'],
    features: ['Anime shading', 'MToon material', 'VRM expressions', 'Hair physics'],
    polyCount: 'medium',
    rigged: true,
    animated: true,
    blendShapes: true,
    tags: ['anime', 'vrm', 'vtuber'],
  },

  // VRChat Specific
  {
    id: 'vrchat-quest-optimized',
    name: 'Quest-Optimized Avatar',
    description: 'Low-poly avatar optimized for Meta Quest standalone',
    thumbnail: '/templates/vrchat-quest.png',
    category: 'humanoid',
    style: 'lowpoly',
    platforms: ['vrchat'],
    features: ['Quest compatible', 'Single material', 'Optimized bones', 'Mobile shaders'],
    polyCount: 'low',
    rigged: true,
    animated: true,
    blendShapes: true,
    tags: ['vrchat', 'quest', 'mobile', 'optimized'],
  },
  {
    id: 'vrchat-full-body',
    name: 'Full Body Tracking Avatar',
    description: 'Avatar with full body tracking support and PhysBones',
    thumbnail: '/templates/vrchat-fbt.png',
    category: 'humanoid',
    style: 'realistic',
    platforms: ['vrchat'],
    features: ['FBT ready', 'PhysBones', 'Eye tracking', 'Face tracking', 'OSC support'],
    polyCount: 'high',
    rigged: true,
    animated: true,
    blendShapes: true,
    tags: ['vrchat', 'fbt', 'fullbody', 'advanced'],
  },
  {
    id: 'vrchat-furry',
    name: 'Furry/Anthro Base',
    description: 'Anthropomorphic character base with digitigrade legs',
    thumbnail: '/templates/vrchat-furry.png',
    category: 'creature',
    style: 'cartoon',
    platforms: ['vrchat', 'chilloutvr', 'neos', 'resonite'],
    features: ['Digitigrade legs', 'Tail physics', 'Ear physics', 'Custom expressions'],
    polyCount: 'medium',
    rigged: true,
    animated: true,
    blendShapes: true,
    tags: ['furry', 'anthro', 'creature'],
  },

  // Roblox Specific
  {
    id: 'roblox-r15',
    name: 'Roblox R15 Character',
    description: 'Standard Roblox R15 avatar with all body parts',
    thumbnail: '/templates/roblox-r15.png',
    category: 'humanoid',
    style: 'cartoon',
    platforms: ['roblox'],
    features: ['R15 compatible', 'Layered clothing support', 'Dynamic head ready'],
    polyCount: 'low',
    rigged: true,
    animated: true,
    blendShapes: true,
    tags: ['roblox', 'r15', 'standard'],
  },
  {
    id: 'roblox-rthro',
    name: 'Roblox Rthro Character',
    description: 'Realistic proportioned Roblox avatar',
    thumbnail: '/templates/roblox-rthro.png',
    category: 'humanoid',
    style: 'realistic',
    platforms: ['roblox'],
    features: ['Rthro proportions', 'Extended skeleton', 'Face animations'],
    polyCount: 'medium',
    rigged: true,
    animated: true,
    blendShapes: true,
    tags: ['roblox', 'rthro', 'realistic'],
  },
  {
    id: 'roblox-blocky',
    name: 'Classic Blocky Avatar',
    description: 'Classic blocky Roblox-style character',
    thumbnail: '/templates/roblox-blocky.png',
    category: 'stylized',
    style: 'voxel',
    platforms: ['roblox', 'sandbox'],
    features: ['Classic look', 'Simple rig', 'Accessory slots'],
    polyCount: 'low',
    rigged: true,
    animated: false,
    blendShapes: false,
    tags: ['roblox', 'classic', 'blocky', 'nostalgic'],
  },

  // RecRoom Specific
  {
    id: 'recroom-standard',
    name: 'Rec Room Character',
    description: 'Fun, stylized Rec Room avatar',
    thumbnail: '/templates/recroom-standard.png',
    category: 'stylized',
    style: 'cartoon',
    platforms: ['recroom'],
    features: ['Rec Room style', 'Simple materials', 'Props ready'],
    polyCount: 'low',
    rigged: true,
    animated: true,
    blendShapes: true,
    tags: ['recroom', 'stylized', 'fun'],
  },

  // Spatial Specific
  {
    id: 'spatial-professional',
    name: 'Professional Avatar',
    description: 'Business-ready avatar for Spatial meetings',
    thumbnail: '/templates/spatial-professional.png',
    category: 'humanoid',
    style: 'realistic',
    platforms: ['spatial', 'meta-horizon'],
    features: ['Professional look', 'Business attire', 'Clean design'],
    polyCount: 'medium',
    rigged: true,
    animated: true,
    blendShapes: true,
    tags: ['spatial', 'professional', 'business'],
  },

  // Sandbox/Metaverse
  {
    id: 'sandbox-voxel',
    name: 'Sandbox Voxel Character',
    description: 'Voxel-style character for The Sandbox',
    thumbnail: '/templates/sandbox-voxel.png',
    category: 'stylized',
    style: 'voxel',
    platforms: ['sandbox', 'decentraland'],
    features: ['Voxel aesthetic', 'NFT ready', 'Equipment slots'],
    polyCount: 'low',
    rigged: true,
    animated: true,
    blendShapes: false,
    tags: ['sandbox', 'voxel', 'nft', 'metaverse'],
  },
  {
    id: 'decentraland-wearable',
    name: 'Decentraland Avatar',
    description: 'Web3-optimized avatar for Decentraland',
    thumbnail: '/templates/decentraland.png',
    category: 'humanoid',
    style: 'lowpoly',
    platforms: ['decentraland'],
    features: ['Wearable slots', 'Ultra optimized', 'Blockchain ready'],
    polyCount: 'low',
    rigged: true,
    animated: true,
    blendShapes: false,
    tags: ['decentraland', 'web3', 'nft', 'optimized'],
  },

  // Specialty
  {
    id: 'robot-mech',
    name: 'Robot/Mech Avatar',
    description: 'Mechanical humanoid robot character',
    thumbnail: '/templates/robot-mech.png',
    category: 'robot',
    style: 'realistic',
    platforms: ['universal', 'vrchat', 'roblox', 'spatial'],
    features: ['Hard surface', 'Mechanical joints', 'LED effects', 'Transform ready'],
    polyCount: 'high',
    rigged: true,
    animated: true,
    blendShapes: false,
    tags: ['robot', 'mech', 'scifi'],
  },
  {
    id: 'chibi-cute',
    name: 'Chibi Character',
    description: 'Super-deformed cute chibi avatar',
    thumbnail: '/templates/chibi.png',
    category: 'anime',
    style: 'chibi',
    platforms: ['universal', 'vrchat', 'recroom', 'roblox'],
    features: ['Chibi proportions', 'Big head', 'Cute expressions'],
    polyCount: 'low',
    rigged: true,
    animated: true,
    blendShapes: true,
    tags: ['chibi', 'cute', 'anime', 'kawaii'],
  },
];

export const platformPresets: AvatarPreset[] = [
  // VRChat Presets
  {
    id: 'vrchat-excellent',
    name: 'VRChat Excellent',
    platform: 'vrchat',
    description: 'Optimized for VRChat Excellent performance ranking',
    settings: {
      targetPolygons: 32000,
      targetBones: 75,
      targetMaterials: 4,
      targetTextureSize: 1024,
      preserveBlendShapes: true,
      optimizeForVR: true,
      generateLODs: true,
    },
  },
  {
    id: 'vrchat-good',
    name: 'VRChat Good',
    platform: 'vrchat',
    description: 'Balanced quality for VRChat Good performance ranking',
    settings: {
      targetPolygons: 50000,
      targetBones: 150,
      targetMaterials: 8,
      targetTextureSize: 2048,
      preserveBlendShapes: true,
      optimizeForVR: true,
      generateLODs: true,
    },
  },
  {
    id: 'vrchat-quest',
    name: 'VRChat Quest',
    platform: 'vrchat',
    description: 'Quest standalone compatible settings',
    settings: {
      targetPolygons: 10000,
      targetBones: 75,
      targetMaterials: 2,
      targetTextureSize: 512,
      preserveBlendShapes: true,
      optimizeForVR: true,
      generateLODs: false,
    },
  },

  // Roblox Presets
  {
    id: 'roblox-ugc',
    name: 'Roblox UGC',
    platform: 'roblox',
    description: 'Optimized for Roblox UGC marketplace',
    settings: {
      targetPolygons: 8000,
      targetBones: 76,
      targetMaterials: 1,
      targetTextureSize: 1024,
      preserveBlendShapes: true,
      optimizeForVR: false,
      generateLODs: false,
    },
  },
  {
    id: 'roblox-mobile',
    name: 'Roblox Mobile',
    platform: 'roblox',
    description: 'Extra optimization for mobile devices',
    settings: {
      targetPolygons: 4000,
      targetBones: 50,
      targetMaterials: 1,
      targetTextureSize: 512,
      preserveBlendShapes: false,
      optimizeForVR: false,
      generateLODs: false,
    },
  },

  // RecRoom Presets
  {
    id: 'recroom-standard',
    name: 'RecRoom Standard',
    platform: 'recroom',
    description: 'Standard RecRoom avatar settings',
    settings: {
      targetPolygons: 10000,
      targetBones: 52,
      targetMaterials: 4,
      targetTextureSize: 512,
      preserveBlendShapes: true,
      optimizeForVR: true,
      generateLODs: false,
    },
  },

  // Spatial Presets
  {
    id: 'spatial-quality',
    name: 'Spatial Quality',
    platform: 'spatial',
    description: 'High quality Spatial avatar',
    settings: {
      targetPolygons: 40000,
      targetBones: 100,
      targetMaterials: 8,
      targetTextureSize: 2048,
      preserveBlendShapes: true,
      optimizeForVR: true,
      generateLODs: true,
    },
  },

  // Sandbox Presets
  {
    id: 'sandbox-standard',
    name: 'Sandbox Standard',
    platform: 'sandbox',
    description: 'Standard Sandbox metaverse avatar',
    settings: {
      targetPolygons: 15000,
      targetBones: 60,
      targetMaterials: 8,
      targetTextureSize: 1024,
      preserveBlendShapes: false,
      optimizeForVR: false,
      generateLODs: false,
    },
  },

  // Decentraland Presets
  {
    id: 'decentraland-wearable',
    name: 'Decentraland Wearable',
    platform: 'decentraland',
    description: 'Ultra-optimized for Decentraland wearables',
    settings: {
      targetPolygons: 1500,
      targetBones: 52,
      targetMaterials: 2,
      targetTextureSize: 512,
      preserveBlendShapes: false,
      optimizeForVR: false,
      generateLODs: false,
    },
  },

  // Universal Presets
  {
    id: 'universal-balanced',
    name: 'Universal Balanced',
    platform: 'universal',
    description: 'Balanced settings for cross-platform use',
    settings: {
      targetPolygons: 30000,
      targetBones: 75,
      targetMaterials: 4,
      targetTextureSize: 1024,
      preserveBlendShapes: true,
      optimizeForVR: true,
      generateLODs: true,
    },
  },
  {
    id: 'universal-maximum',
    name: 'Universal Maximum',
    platform: 'universal',
    description: 'Maximum quality for archival purposes',
    settings: {
      targetPolygons: 100000,
      targetBones: 256,
      targetMaterials: 32,
      targetTextureSize: 4096,
      preserveBlendShapes: true,
      optimizeForVR: false,
      generateLODs: true,
    },
  },
];

export function getTemplatesForPlatform(platform: AvatarPlatformId): AvatarTemplate[] {
  return avatarTemplates.filter(t => t.platforms.includes(platform));
}

export function getTemplatesByCategory(category: AvatarTemplate['category']): AvatarTemplate[] {
  return avatarTemplates.filter(t => t.category === category);
}

export function getTemplatesByStyle(style: AvatarTemplate['style']): AvatarTemplate[] {
  return avatarTemplates.filter(t => t.style === style);
}

export function getPresetsForPlatform(platform: AvatarPlatformId): AvatarPreset[] {
  return platformPresets.filter(p => p.platform === platform);
}

export function getTemplateById(id: string): AvatarTemplate | undefined {
  return avatarTemplates.find(t => t.id === id);
}

export function getPresetById(id: string): AvatarPreset | undefined {
  return platformPresets.find(p => p.id === id);
}

export function searchTemplates(query: string): AvatarTemplate[] {
  const lowerQuery = query.toLowerCase();
  return avatarTemplates.filter(t =>
    t.name.toLowerCase().includes(lowerQuery) ||
    t.description.toLowerCase().includes(lowerQuery) ||
    t.tags.some(tag => tag.toLowerCase().includes(lowerQuery))
  );
}
