
import React, { useEffect, useState } from 'react';
import { isAndroidPlatform, isIOSPlatform } from '@/utils/platform';
import { Label } from '@/components/ui/label';
import { Capacitor } from '@capacitor/core';

// 버전 정보 인터페이스 정의
interface VersionInfo {
  versionName: string;
  buildNumber: number;
  versionCode?: number;
  platform?: string;
  pluginResponse?: string;
  timestamp?: number;
  error?: boolean;
  errorMessage?: string;
  defaultValuesUsed?: boolean;  
}

interface AppVersionInfoProps {
  className?: string;
  showDevInfo?: boolean;
  editable?: boolean;
}

const AppVersionInfo: React.FC<AppVersionInfoProps> = ({
  className,
  showDevInfo = true,
  editable = false
}) => {
  // 하드코딩된 버전 정보 - 빌드 스크립트에서 설정한 값과 일치시켜야 함
  const hardcodedVersionInfo: VersionInfo = {
    versionName: '1.1.1.3',
    buildNumber: 7,
    versionCode: 7,
    platform: Capacitor.getPlatform(),
    defaultValuesUsed: false
  };

  const [versionInfo, setVersionInfo] = useState<VersionInfo>(hardcodedVersionInfo);
  const [loading, setLoading] = useState(false);

  // 개발자 정보 표시
  const renderDevInfo = () => {
    if (versionInfo && showDevInfo) {
      return (
        <div className="text-xs text-muted-foreground">
          <p>
            {versionInfo.versionCode ? `버전 코드: ${versionInfo.versionCode}` : ''}
            {versionInfo.buildNumber ? `, 빌드: ${versionInfo.buildNumber}` : ''}
            {versionInfo.platform ? ` (${versionInfo.platform})` : ''}
          </p>
          {versionInfo.errorMessage && (
            <p className="text-destructive">오류: {versionInfo.errorMessage}</p>
          )}
        </div>
      );
    }
    return null;
  };

  return (
    <div className={className}>
      <div className="flex flex-col space-y-1">
        <Label className="text-base">버전 정보</Label>
        <p className="text-sm">
          {loading ? (
            "버전 정보 로딩 중..."
          ) : (
            versionInfo.versionName || "알 수 없음"
          )}
        </p>
        {renderDevInfo()}
      </div>
    </div>
  );
};

export default AppVersionInfo;
