firebaseSetupCommand function

Future<void> firebaseSetupCommand({
  1. required bool full,
  2. required String env,
})

Orchestrates the full Firebase setup flow for a Flutter project.

If full is true, it performs a comprehensive setup including tool checks, login, project creation, and dependency configuration. The env parameter specifies the target environment (e.g., 'dev').

Implementation

Future<void> firebaseSetupCommand({
  required bool full,
  required String env,
}) async {
  final firebase = FirebaseService();
  final flutter = FlutterService();
  final prompt = PromptService();
  final envService = EnvService();
  final feature = FeatureService();

  print('šŸš€ FirePilot: Elite Firebase Setup ($env)\n');

  try {
    /// šŸ”¹ Validate Flutter project
    if (!File('pubspec.yaml').existsSync()) {
      print('āŒ Not a Flutter project');
      print('šŸ‘‰ Run this inside a Flutter project folder');
      return;
    }

    /// šŸ”¹ Check & auto-fix tools
    await _checkAndFixTools();

    /// šŸ”¹ Ask Project ID
    /// šŸ”¹ Ask Project ID with Validation
    String projectId = '';
    while (true) {
      projectId = prompt.ask('Enter Firebase project ID');

      if (projectId.isEmpty) {
        print('āŒ Project ID cannot be empty');
        continue;
      }

      final regExp = RegExp(r'^[a-z][a-z0-9-]{5,29}$');
      if (!regExp.hasMatch(projectId)) {
        print('āŒ Invalid Project ID format');
        print('šŸ‘‰ Must be 6-30 chars, lowercase, numbers, and hyphens (no underscores)');
        print('šŸ‘‰ Must start with a letter');
        continue;
      }
      break;
    }

    /// šŸ”¹ Firebase Login
    print('šŸ” Checking Firebase login...');
    await firebase.login();

    /// šŸ”¹ List Projects (Helpful for Quota debugging)
    print('\nšŸ“‹ Your existing Firebase projects:');
    await firebase.listProjects();

    /// šŸ”¹ Create Project (Optional)
    final shouldCreate = prompt.confirm('Create Firebase project?');

    if (shouldCreate) {
      print('šŸ“¦ Creating Firebase project...');
      try {
        await firebase.createProject(projectId);
      } catch (e) {
        print('\nāŒ Project creation failed!');
        print('šŸ‘‰ Possible reasons:');
        print('   1. ID "$projectId" is already taken (globally unique).');
        print('   2. You have reached your Firebase project quota.');
        print('   3. Network or permission issues.');
        print('šŸ‘‰ TIP: Try a MORE UNIQUE ID (e.g. my-elite-$projectId)\n');

        final proceed = prompt.confirm(
            'Continue anyway? (Choose yes ONLY if the project already exists)');
        if (!proceed) {
          print('āŒ Setup aborted by user');
          return;
        }
      }
    } else {
      print('ā­ Skipping project creation');
    }

    /// šŸ”¹ Configure FlutterFire
    print('āš™ļø Configuring FlutterFire...');
    await firebase.configure(projectId);

    /// šŸ”¹ Add Dependencies
    print('šŸ“¦ Adding Firebase dependencies...');
    await flutter.addDeps();

    /// šŸ”‘ Setup SHA
    print('šŸ”‘ Setting up SHA...');
    try {
      await firebase.setupSha(projectId);
    } catch (e) {
      print('āš ļø SHA setup failed, skipping...');
    }

    /// šŸ”¹ Create Environment Folder
    print('šŸŒ Setting up environment...');
    envService.create(env);

    /// šŸ”„ Full Setup (UPDATED: Firestore removed)
    if (full) {
      print('šŸ”„ Running FULL setup...\n');

      final features = ['auth', 'fcm']; // āœ… firestore removed

      for (final f in features) {
        await feature.enable(f, projectId: projectId);
      }

      print('\nšŸ“¦ Enabled: auth, fcm');
      print('🚫 Skipped: firestore (manual enable recommended)');
    } else {
      print('ā„¹ļø Skipping feature setup (use --full to enable)');
    }

    print('\nšŸŽ‰ Firebase setup completed successfully for [$env]');
  } catch (e) {
    print('\nāŒ Setup failed!');
    print('Error: $e');
  }
}