import {
  AppstoreOutlined,
  BellOutlined,
  BookOutlined,
  FileImageOutlined,
  FireOutlined,
  FundProjectionScreenOutlined,
  PictureOutlined,
  RocketOutlined,
  SoundOutlined,
  StarOutlined,
  ThunderboltOutlined,
} from '@ant-design/icons';
import type { ReactNode } from 'react';

export type PlayAppEnvironment = 'preview' | 'test' | 'prod';

export type PlayAppEntry = {
  id: string;
  name: string;
  accentClassName: string;
  statusLabel: string;
  isReady: boolean;
  icon: ReactNode;
  usagePriority?: number;
  supportedEnvironments?: PlayAppEnvironment[];
  searchKeywords?: string[];
  searchDescription?: string;
};

export const APP_LIBRARY_ENTRIES: PlayAppEntry[] = [
  {
    id: 'baseball-ticket-bay',
    name: '야구-티켓베이',
    accentClassName: 'apps-library__card--baseball-ticket-bay',
    statusLabel: '알림',
    isReady: true,
    icon: <BellOutlined />,
    usagePriority: 100,
    supportedEnvironments: ['preview', 'test'],
    searchKeywords: ['야구', '티켓베이', 'ticketbay', '야구 티켓', '웹푸시', '알림'],
    searchDescription: '팀, 구역, 통로, 가격 조건으로 야구 티켓 알림 조건을 저장하고 테스트 푸시를 보냅니다.',
  },
  {
    id: 'e-reader',
    name: 'E-Reader',
    accentClassName: 'apps-library__card--reader',
    statusLabel: '읽기',
    isReady: true,
    icon: <BookOutlined />,
    usagePriority: 80,
    supportedEnvironments: ['preview', 'test'],
    searchKeywords: ['e-reader', 'reader', 'ebook', 'article', 'web article', '기사', '전자책', '리더'],
    searchDescription: 'Apps 보관함에서 인터넷 기사와 웹 콘텐츠를 전자책처럼 넘겨 읽습니다.',
  },
  {
    id: 'photoprism',
    name: 'PhotoPrism',
    accentClassName: 'apps-library__card--photoprism',
    statusLabel: '연결',
    isReady: true,
    icon: <FileImageOutlined />,
    usagePriority: 70,
    supportedEnvironments: ['preview', 'test'],
    searchKeywords: ['photoprism', 'photo', 'album', 'gallery', '사진 앨범'],
    searchDescription: 'Apps 보관함에서 PhotoPrism 앨범과 사진을 단독 앱 형태로 엽니다.',
  },
  {
    id: 'photo-puzzle',
    name: '사진 퍼즐',
    accentClassName: 'apps-library__card--puzzle',
    statusLabel: '실행',
    isReady: true,
    icon: <PictureOutlined />,
    usagePriority: 60,
    supportedEnvironments: ['preview', 'test'],
    searchKeywords: ['photo puzzle', '퍼즐', '사진', '슬라이드 퍼즐'],
    searchDescription: 'Apps 보관함에서 사진 퍼즐 게임을 바로 실행합니다.',
  },
  {
    id: 'the-quest',
    name: 'The Quest',
    accentClassName: 'apps-library__card--the-quest',
    statusLabel: '신규',
    isReady: true,
    icon: <ThunderboltOutlined />,
    usagePriority: 50,
    supportedEnvironments: ['preview', 'test'],
    searchKeywords: ['the quest', 'rpg', 'mobile rpg', 'phaser', 'quest', 'rpg game', '모바일 rpg'],
    searchDescription: 'Apps 보관함에서 한국형 모바일 RPG 데모 The Quest를 실행합니다.',
  },
  {
    id: 'tetris',
    name: 'Tetris',
    accentClassName: 'apps-library__card--tetris',
    statusLabel: '실행',
    isReady: true,
    icon: <FundProjectionScreenOutlined />,
    usagePriority: 40,
    supportedEnvironments: ['preview', 'test'],
    searchKeywords: ['테트리스', 'block', 'arcade', '블록 게임'],
    searchDescription: 'Apps 보관함에서 테트리스 게임을 바로 실행합니다.',
  },
  { id: 'beat-lab', name: 'Beat Lab', accentClassName: 'apps-library__card--beat', statusLabel: '준비', isReady: false, icon: <SoundOutlined /> },
  { id: 'sticker-booth', name: 'Sticker Booth', accentClassName: 'apps-library__card--sticker', statusLabel: '준비', isReady: false, icon: <StarOutlined /> },
  { id: 'launch-note', name: 'Launch Note', accentClassName: 'apps-library__card--launch', statusLabel: '예정', isReady: false, icon: <RocketOutlined /> },
  { id: 'arcade-pack', name: 'Arcade Pack', accentClassName: 'apps-library__card--arcade', statusLabel: '예정', isReady: false, icon: <FireOutlined /> },
  { id: 'app-vault', name: 'App Vault', accentClassName: 'apps-library__card--vault', statusLabel: '테마', isReady: false, icon: <AppstoreOutlined /> },
];

export function getReadyPlayAppEntries() {
  return APP_LIBRARY_ENTRIES.filter((entry) => entry.isReady);
}

export function getSupportedPlayAppEnvironments(entry: PlayAppEntry): PlayAppEnvironment[] {
  if (entry.supportedEnvironments && entry.supportedEnvironments.length > 0) {
    return entry.supportedEnvironments;
  }

  return ['preview'];
}

export function isPlayAppSupportedInEnvironment(entry: PlayAppEntry, environment: PlayAppEnvironment) {
  return getSupportedPlayAppEnvironments(entry).includes(environment);
}

export function findReadyPlayAppEntryById(appId: string | null | undefined) {
  if (!appId) {
    return null;
  }

  return getReadyPlayAppEntries().find((entry) => entry.id === appId) ?? null;
}
