#!/usr/bin/env python3
"""
Finale Inventory to WHIMS Migration - Initialization Script

This script initiates the complete migration process from Finale Inventory to WHIMS.
It will run all migration steps in sequence, including:
  1. Database backup
  2. Database cleanup
  3. Product import (with categories from goparts)
  4. Purchase order migration
  5. Stock inventory migration
  6. Stock history migration
  7. Migration validation

Usage:
    python3 init.py

Requirements:
  - Python 3.x
  - MySQL access credentials in .env file
  - Access to goparts database for category lookup
  - Finale export files in migration-dev directory:
    * PurchaseOrderWDetail.json
    * StockQuantityBySublocationInUnitsWDetail.json
    * StockHistoryTransactionDetails (1).json
    * ProductListScreen.csv

⚠️  WARNING: This will clear all existing data in the WHIMS database!
Make sure you have reviewed the migration plan before proceeding.
"""

import os
import sys
import subprocess

def main():
    """Main entry point for migration"""
    print("\n" + "="*70)
    print("   FINALE INVENTORY TO WHIMS MIGRATION - INITIALIZATION")
    print("="*70)

    # Get the directory where this script is located
    script_dir = os.path.dirname(os.path.abspath(__file__))
    runner_script = os.path.join(script_dir, 'run_migration.py')

    # Check if runner script exists
    if not os.path.exists(runner_script):
        print("\n❌ Error: run_migration.py not found!")
        print(f"   Expected location: {runner_script}")
        sys.exit(1)

    print(f"\n📂 Migration directory: {script_dir}")
    print(f"📄 Running migration from: {runner_script}")

    # Run the migration
    try:
        result = subprocess.run(
            [sys.executable, runner_script],
            cwd=script_dir,
            check=False
        )

        sys.exit(result.returncode)

    except Exception as e:
        print(f"\n❌ Error running migration: {str(e)}")
        sys.exit(1)

if __name__ == "__main__":
    main()
