#!/usr/bin/env node

/**
 * Verify Capacitor plugin Android structure (post-restructure)
 * 
 * This script verifies that the plugin follows the standard Capacitor structure:
 * - android/src/main/java/... (plugin code)
 * - android/build.gradle (plugin build config)
 * 
 * This script is now optional since the plugin uses standard structure.
 * It can be used to verify the structure is correct.
 * 
 * @author Matthew Raymer
 */

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

function findAppRoot() {
  let currentDir = __dirname;
  
  // Go up from scripts/ to plugin root
  currentDir = path.dirname(currentDir);
  
  // Verify we're in the plugin root
  const pluginPackageJson = path.join(currentDir, 'package.json');
  if (!fs.existsSync(pluginPackageJson)) {
    throw new Error('Could not find plugin package.json - script may be in wrong location');
  }
  
  // Go up from plugin root to node_modules/@timesafari
  currentDir = path.dirname(currentDir);
  
  // Go up from node_modules/@timesafari to node_modules
  currentDir = path.dirname(currentDir);
  
  // Go up from node_modules to app root
  const appRoot = path.dirname(currentDir);
  
  // Verify we found an app root
  const androidDir = path.join(appRoot, 'android');
  if (!fs.existsSync(androidDir)) {
    throw new Error(`Could not find app android directory. Looked in: ${appRoot}`);
  }
  
  return appRoot;
}

/**
 * Verify plugin uses standard Capacitor structure
 */
function verifyPluginStructure() {
  console.log('🔍 Verifying Daily Notification Plugin structure...');
  
  try {
    const APP_ROOT = findAppRoot();
    const PLUGIN_PATH = path.join(APP_ROOT, 'node_modules', '@timesafari', 'daily-notification-plugin');
    const ANDROID_PLUGIN_PATH = path.join(PLUGIN_PATH, 'android');
    const PLUGIN_JAVA_PATH = path.join(ANDROID_PLUGIN_PATH, 'src', 'main', 'java');
    
    if (!fs.existsSync(ANDROID_PLUGIN_PATH)) {
      console.log('ℹ️  Plugin not found in node_modules (may not be installed yet)');
      return;
    }
    
    // Check for standard structure
    const hasStandardStructure = fs.existsSync(PLUGIN_JAVA_PATH);
    const hasOldStructure = fs.existsSync(path.join(ANDROID_PLUGIN_PATH, 'plugin'));
    
    if (hasOldStructure) {
      console.log('⚠️  WARNING: Plugin still uses old structure (android/plugin/)');
      console.log('   This should not happen after restructure. Please rebuild plugin.');
      return;
    }
    
    if (hasStandardStructure) {
      console.log('✅ Plugin uses standard Capacitor structure (android/src/main/java/)');
      console.log('   No fixes needed - plugin path is correct!');
    } else {
      console.log('⚠️  Plugin structure not recognized');
      console.log(`   Expected: ${PLUGIN_JAVA_PATH}`);
    }
    
  } catch (error) {
    console.error('❌ Error verifying plugin structure:', error.message);
    process.exit(1);
  }
}

/**
 * Run verification
 */
function verifyAll() {
  console.log('🔍 Daily Notification Plugin - Structure Verification');
  console.log('==================================================\n');
  
  verifyPluginStructure();
  
  console.log('\n✅ Verification complete!');
}

// Run if called directly
if (import.meta.url === `file://${process.argv[1]}`) {
  verifyAll();
}

export { verifyPluginStructure, verifyAll };
