/**
 * Check table structure to see what columns exist
 */

import { getProjectConnection } from './utils/dbManager.js';
import Treelogy from './models/treelogy.js';

async function checkTableStructure() {
  try {
    console.log('🔍 Checking table structure...\n');

    // Get project details
    const project = await Treelogy.findByPk(1);
    if (!project) {
      console.log('❌ Project not found');
      return;
    }

    console.log('✅ Project found:', project.project_name);

    // Get project database connection
    const projectConnection = await getProjectConnection(project.dbname);

    // Check treedetailsentered table structure
    console.log('\n📊 Checking treedetailsentered table structure...');
    const tableStructure = await projectConnection.query(`
      SELECT column_name, data_type 
      FROM information_schema.columns 
      WHERE table_name = 'treedetailsentered' 
      ORDER BY ordinal_position
    `);

    console.log('Columns in treedetailsentered:');
    tableStructure[0].forEach(col => {
      console.log(`  - ${col.column_name} (${col.data_type})`);
    });

    // Check trees table structure
    console.log('\n📊 Checking trees table structure...');
    const treesStructure = await projectConnection.query(`
      SELECT column_name, data_type 
      FROM information_schema.columns 
      WHERE table_name = 'trees' 
      ORDER BY ordinal_position
    `);

    console.log('Columns in trees:');
    treesStructure[0].forEach(col => {
      console.log(`  - ${col.column_name} (${col.data_type})`);
    });

    // Check if condition table exists
    console.log('\n📊 Checking condition table...');
    const conditionTable = await projectConnection.query(`
      SELECT EXISTS (
        SELECT FROM information_schema.tables 
        WHERE table_name = 'condition'
      )
    `);

    console.log('Condition table exists:', conditionTable[0][0].exists);

    if (conditionTable[0][0].exists) {
      const conditionStructure = await projectConnection.query(`
        SELECT column_name, data_type 
        FROM information_schema.columns 
        WHERE table_name = 'condition' 
        ORDER BY ordinal_position
      `);

      console.log('Columns in condition:');
      conditionStructure[0].forEach(col => {
        console.log(`  - ${col.column_name} (${col.data_type})`);
      });
    }

  } catch (error) {
    console.error('❌ Error checking table structure:', error.message);
  }
}

// Run the check
checkTableStructure().catch(console.error);
