#!/usr/bin/env python3
"""
Script to create the single_analysis_purchases table for pay-per-analysis functionality
"""

from app import app
from models import db, SingleAnalysisPurchase
from sqlalchemy import text

def create_single_analysis_purchase_table():
    """Create the single_analysis_purchases table if it doesn't exist"""

    with app.app_context():
        # Check if table already exists
        inspector = db.inspect(db.engine)
        tables = inspector.get_table_names()

        if 'single_analysis_purchases' in tables:
            print("✅ Table 'single_analysis_purchases' already exists")

            # Check current entries
            count = SingleAnalysisPurchase.query.count()
            print(f"   Current entries: {count}")
        else:
            # Create the table
            print("📦 Creating 'single_analysis_purchases' table...")

            # Create only the new table
            db.create_all()

            print("✅ Table 'single_analysis_purchases' created successfully!")

        # Verify the table structure
        print("\n📊 Table structure:")
        columns = inspector.get_columns('single_analysis_purchases')
        for col in columns:
            print(f"   - {col['name']}: {col['type']}")

    return True

if __name__ == "__main__":
    create_single_analysis_purchase_table()