import { getTransactionStatus } from "@mandujs/core/change";
import { getRootDir } from "../../util/fs";

export async function changeStatus(): Promise<boolean> {
  const rootDir = getRootDir();

  console.log(`🥟 Mandu Change Status\n`);

  try {
    const { state, change } = await getTransactionStatus(rootDir);

    if (state.active && change) {
      console.log(`🔄 Active transaction:`);
      console.log(`   ID: ${change.id}`);
      console.log(`   Snapshot: ${change.snapshotId}`);
      console.log(`   Started: ${new Date(change.createdAt).toLocaleString()}`);
      if (change.message) {
        console.log(`   Message: ${change.message}`);
      }
      if (change.autoGenerated) {
        console.log(`   Source: Auto-correct`);
      }
      console.log(`\n💡 Available commands:`);
      console.log(`   Commit: bunx mandu change commit`);
      console.log(`   Rollback: bunx mandu change rollback`);
    } else {
      console.log(`✅ No active transaction`);
      console.log(`\n💡 Start a new transaction: bunx mandu change begin`);
    }

    return true;
  } catch (error) {
    console.error(`❌ Failed to get status: ${error instanceof Error ? error.message : String(error)}`);
    return false;
  }
}
