import React, { useEffect } from 'react';
import * as Updates from 'expo-updates';
import { Alert } from 'react-native';

interface UpdateCheckerProps {
  children: React.ReactNode;
}

const UpdateChecker: React.FC<UpdateCheckerProps> = ({ children }) => {
  useEffect(() => {
    const checkForUpdates = async () => {
      try {
        const update = await Updates.checkForUpdateAsync();
        if (update.isAvailable) {
          Alert.alert(
            "Update available",
            "Restart app to apply the update?",
            [
              { text: "Later", style: "cancel" },
              { text: "Update & Restart", onPress: () => Updates.reloadAsync() }
            ]
          );
        }
      } catch (error) {
        // Update check failed silently
      }
    };

    // Only check on production builds
    if (!__DEV__) {
      checkForUpdates();
    }
  }, []);

  return <>{children}</>;
};

export default UpdateChecker;