export interface RoomDecorDefinition {
  decor_id: string;
  name: string;
  item_id: string;
  category: 'floor' | 'rug' | 'wall';
  placement_surface: 'floor' | 'wall';
  grid_size: number;
  rotation_steps: number[];
  stackable: boolean;
  icon: string;
  texture?: string;
  default_scale: number;
  default_rotation_degrees: number;
  default_position: {
    x: number;
    y: number;
  };
  default_z_index: number;
  shop: {
    category: 'space';
    description: string;
    price: number;
    tags: string[];
    sort_order: number;
  };
  collision_size?: {
    x: number;
    y: number;
  };
  collision_offset?: {
    x: number;
    y: number;
  };
}

export interface RoomDecorLegacyDefault {
  scale: number;
  default_position: {
    x: number;
    y: number;
  };
}

export const ROOM_DECOR_ROOM_SCALE = 0.7;
export const ROOM_DECOR_BED_DEFAULT_SCALE = ROOM_DECOR_ROOM_SCALE;
export const ROOM_DECOR_BOOKSHELF_DEFAULT_SCALE = 0.12;
export const ROOM_DECOR_FLOOR_RUG_DEFAULT_SCALE = 1.0;
export const ROOM_DECOR_WALL_DECOR_DEFAULT_SCALE = 0.04;
export const ROOM_DECOR_LEGACY_BED_MAX_SCALE = 0.35;
export const ROOM_DECOR_LEGACY_WALL_DECOR_SCALES = [0.7, 0.18];

export const ROOM_DECOR_LEGACY_DEFAULTS: Record<string, RoomDecorLegacyDefault> = {
  whale_floor_rug: {
    scale: 0.42,
    default_position: { x: 0, y: 230 },
  },
  whale_memory_board: {
    scale: 0.16,
    default_position: { x: 260, y: -295 },
  },
  whale_tail_lamp: {
    scale: 0.16,
    default_position: { x: 330, y: -250 },
  },
  boat_cabin_bed: {
    scale: 1,
    default_position: { x: -230, y: 35 },
  },
  low_wave_bed: {
    scale: 1,
    default_position: { x: -140, y: 55 },
  },
  whale_tail_headboard_bed: {
    scale: 1,
    default_position: { x: 0, y: 45 },
  },
  dev_whale_bookshelf: {
    scale: 1,
    default_position: { x: -300, y: -55 },
  },
  datawhale_bug_feature_badge: {
    scale: 1,
    default_position: { x: -300, y: -290 },
  },
  datawhale_buddhist_learning_badge: {
    scale: 1,
    default_position: { x: 0, y: -290 },
  },
  datawhale_ok_working_badge: {
    scale: 1,
    default_position: { x: 300, y: -290 },
  },
};

export const ROOM_DECOR_DEFINITIONS: RoomDecorDefinition[] = [
  {
    decor_id: 'whale_floor_rug',
    item_id: 'decor_whale_floor_rug',
    name: '鲸浪地毯',
    category: 'rug',
    placement_surface: 'floor',
    grid_size: 16,
    rotation_steps: [0],
    stackable: false,
    icon: 'res://assets/ui/mall/items/room_decor_whale_floor_rug.png',
    texture: 'res://assets/maps/personal_space/v1/decor/room_decor_whale_floor_rug_roomfit.png',
    default_scale: ROOM_DECOR_FLOOR_RUG_DEFAULT_SCALE,
    default_rotation_degrees: 0,
    default_position: { x: 0, y: 161 },
    default_z_index: -8,
    shop: { category: 'space', description: '蓝白鲸鱼主题地毯，适合铺在个人房间地板区域。', price: 260, tags: ['房间家具', '可拖拽', '地面'], sort_order: 110 },
  },
  {
    decor_id: 'whale_memory_board',
    item_id: 'decor_whale_memory_board',
    name: '鲸语记忆板',
    category: 'wall',
    placement_surface: 'wall',
    grid_size: 16,
    rotation_steps: [0],
    stackable: false,
    icon: 'res://assets/ui/mall/items/room_decor_whale_memory_board.png',
    default_scale: 0.11,
    default_rotation_degrees: 0,
    default_position: { x: 182, y: -207 },
    default_z_index: -14,
    shop: { category: 'space', description: '挂在房间里的鲸鱼木质装饰板，适合点缀窗边墙面。', price: 220, tags: ['房间家具', '可拖拽', '挂件'], sort_order: 120 },
  },
  {
    decor_id: 'whale_tail_lamp',
    item_id: 'decor_whale_tail_lamp',
    name: '鲸尾暖灯',
    category: 'floor',
    placement_surface: 'floor',
    grid_size: 16,
    rotation_steps: [0, 90, 180, 270],
    stackable: false,
    icon: 'res://assets/ui/mall/items/room_decor_whale_tail_lamp.png',
    texture: 'res://assets/maps/personal_space/v1/decor/room_decor_whale_tail_lamp_roomfit.png',
    default_scale: 1,
    default_rotation_degrees: 0,
    default_position: { x: 231, y: -175 },
    default_z_index: -10,
    shop: { category: 'space', description: '鲸尾造型的温暖装饰灯，可自由摆放在个人房间中。', price: 360, tags: ['房间家具', '可拖拽', '灯具'], sort_order: 130 },
    collision_size: { x: 50, y: 32 },
    collision_offset: { x: 0, y: 56 },
  },
  {
    decor_id: 'boat_cabin_bed',
    item_id: 'decor_boat_cabin_bed',
    name: '船舱小床',
    category: 'floor',
    placement_surface: 'floor',
    grid_size: 16,
    rotation_steps: [0, 90, 180, 270],
    stackable: false,
    icon: 'res://assets/ui/mall/items/room_decor_boat_cabin_bed.png',
    texture: 'res://assets/maps/personal_space/v1/decor/room_decor_boat_cabin_bed_roomfit.png',
    default_scale: ROOM_DECOR_BED_DEFAULT_SCALE,
    default_rotation_degrees: 0,
    default_position: { x: -161, y: 25 },
    default_z_index: -9,
    shop: { category: 'space', description: '白木船舱造型的小床，适合放在个人房间地面区域。', price: 520, tags: ['房间家具', '可拖拽', '床'], sort_order: 140 },
    collision_size: { x: 220, y: 112 },
    collision_offset: { x: 0, y: 52 },
  },
  {
    decor_id: 'low_wave_bed',
    item_id: 'decor_low_wave_bed',
    name: '海浪低床',
    category: 'floor',
    placement_surface: 'floor',
    grid_size: 16,
    rotation_steps: [0, 90, 180, 270],
    stackable: false,
    icon: 'res://assets/ui/mall/items/room_decor_low_wave_bed.png',
    texture: 'res://assets/maps/personal_space/v1/decor/room_decor_low_wave_bed_roomfit.png',
    default_scale: ROOM_DECOR_BED_DEFAULT_SCALE,
    default_rotation_degrees: 0,
    default_position: { x: -98, y: 39 },
    default_z_index: -9,
    shop: { category: 'space', description: '蓝白海浪被面的低矮小床，适合轻松的海风房间。', price: 500, tags: ['房间家具', '可拖拽', '床'], sort_order: 150 },
    collision_size: { x: 220, y: 112 },
    collision_offset: { x: 0, y: 56 },
  },
  {
    decor_id: 'whale_tail_headboard_bed',
    item_id: 'decor_whale_tail_headboard_bed',
    name: '鲸尾床头床',
    category: 'floor',
    placement_surface: 'floor',
    grid_size: 16,
    rotation_steps: [0, 90, 180, 270],
    stackable: false,
    icon: 'res://assets/ui/mall/items/room_decor_whale_tail_headboard_bed.png',
    texture: 'res://assets/maps/personal_space/v1/decor/room_decor_whale_tail_headboard_bed_roomfit.png',
    default_scale: ROOM_DECOR_BED_DEFAULT_SCALE,
    default_rotation_degrees: 0,
    default_position: { x: 0, y: 32 },
    default_z_index: -9,
    shop: { category: 'space', description: '鲸尾床头和深蓝被面的主题小床，鲸镇特色更明显。', price: 580, tags: ['房间家具', '可拖拽', '床'], sort_order: 180 },
    collision_size: { x: 214, y: 112 },
    collision_offset: { x: 0, y: 62 },
  },
  {
    decor_id: 'dev_whale_bookshelf',
    item_id: 'decor_dev_whale_bookshelf',
    name: '程序员鲸书架',
    category: 'floor',
    placement_surface: 'floor',
    grid_size: 16,
    rotation_steps: [0, 90, 180, 270],
    stackable: false,
    icon: 'res://assets/ui/mall/items/room_decor_dev_whale_bookshelf.png',
    texture: 'res://assets/ui/mall/items/room_decor_dev_whale_bookshelf.png',
    default_scale: ROOM_DECOR_BOOKSHELF_DEFAULT_SCALE,
    default_rotation_degrees: 0,
    default_position: { x: -210, y: -39 },
    default_z_index: -10,
    shop: { category: 'space', description: '带 GitHub、Datawhale 和代码小物件的蓝白书架，适合程序员风格的个人房间。', price: 620, tags: ['房间家具', '可拖拽', '书架'], sort_order: 190 },
    collision_size: { x: 626.667, y: 226.667 },
    collision_offset: { x: 0, y: 580 },
  },
  {
    decor_id: 'datawhale_bug_feature_badge',
    item_id: 'decor_datawhale_bug_feature_badge',
    name: 'BUG特性徽章',
    category: 'wall',
    placement_surface: 'wall',
    grid_size: 16,
    rotation_steps: [0],
    stackable: false,
    icon: 'res://assets/ui/mall/items/room_decor_datawhale_bug_feature_badge.png',
    texture: 'res://assets/maps/personal_space/v1/decor/room_decor_datawhale_bug_feature_badge_hires_clean.png',
    default_scale: ROOM_DECOR_WALL_DECOR_DEFAULT_SCALE,
    default_rotation_degrees: 0,
    default_position: { x: -210, y: -203 },
    default_z_index: -14,
    shop: { category: 'space', description: '写着“这不是BUG 这是feature”的佛系学习小徽章，适合贴在个人房间墙面。', price: 120, tags: ['房间家具', '可拖拽', '徽章'], sort_order: 200 },
  },
  {
    decor_id: 'datawhale_buddhist_learning_badge',
    item_id: 'decor_datawhale_buddhist_learning_badge',
    name: '佛系学习徽章',
    category: 'wall',
    placement_surface: 'wall',
    grid_size: 16,
    rotation_steps: [0],
    stackable: false,
    icon: 'res://assets/ui/mall/items/room_decor_datawhale_buddhist_learning_badge.png',
    texture: 'res://assets/maps/personal_space/v1/decor/room_decor_datawhale_buddhist_learning_badge_hires_clean.png',
    default_scale: ROOM_DECOR_WALL_DECOR_DEFAULT_SCALE,
    default_rotation_degrees: 0,
    default_position: { x: 0, y: -203 },
    default_z_index: -14,
    shop: { category: 'space', description: 'Datawhale 佛系学习主题徽章，适合贴在个人房间墙面。', price: 140, tags: ['房间家具', '可拖拽', '徽章'], sort_order: 210 },
  },
  {
    decor_id: 'datawhale_ok_working_badge',
    item_id: 'decor_datawhale_ok_working_badge',
    name: '已经在做徽章',
    category: 'wall',
    placement_surface: 'wall',
    grid_size: 16,
    rotation_steps: [0],
    stackable: false,
    icon: 'res://assets/ui/mall/items/room_decor_datawhale_ok_working_badge.png',
    texture: 'res://assets/maps/personal_space/v1/decor/room_decor_datawhale_ok_working_badge_hires_clean.png',
    default_scale: ROOM_DECOR_WALL_DECOR_DEFAULT_SCALE,
    default_rotation_degrees: 0,
    default_position: { x: 210, y: -203 },
    default_z_index: -14,
    shop: { category: 'space', description: '写着“OKKKK 已经在做了”的工作状态徽章，适合贴在个人房间墙面。', price: 120, tags: ['房间家具', '可拖拽', '徽章'], sort_order: 220 },
  },
];

export function findRoomDecorDefinition(decorId?: string): RoomDecorDefinition | undefined {
  const normalizedDecorId = (decorId || '').trim();
  return ROOM_DECOR_DEFINITIONS.find((decor) => normalizedDecorId && decor.decor_id === normalizedDecorId);
}
