#!/usr/bin/env python3
"""
Add missing columns to flipper_signatures table
"""

import sys
from pathlib import Path

# Add project root to path
sys.path.insert(0, str(Path(__file__).parent.parent))

from src.database.connection import get_engine
from sqlalchemy import text

def main():
    engine = get_engine()

    with engine.connect() as conn:
        print("Adding missing columns to flipper_signatures table...")

        try:
            # Add file_hash column
            conn.execute(text("""
                ALTER TABLE flipper_signatures
                ADD COLUMN IF NOT EXISTS file_hash VARCHAR(64) UNIQUE
            """))
            conn.execute(text("""
                CREATE INDEX IF NOT EXISTS ix_flipper_signatures_file_hash
                ON flipper_signatures(file_hash)
            """))
            print("✓ Added file_hash column")
        except Exception as e:
            print(f"file_hash: {e}")

        try:
            # Add file_path column
            conn.execute(text("""
                ALTER TABLE flipper_signatures
                ADD COLUMN IF NOT EXISTS file_path VARCHAR(500)
            """))
            print("✓ Added file_path column")
        except Exception as e:
            print(f"file_path: {e}")

        try:
            # Add raw_format column
            conn.execute(text("""
                ALTER TABLE flipper_signatures
                ADD COLUMN IF NOT EXISTS raw_format VARCHAR(20)
            """))
            print("✓ Added raw_format column")
        except Exception as e:
            print(f"raw_format: {e}")

        conn.commit()
        print("\n✓ Migration complete!")

if __name__ == "__main__":
    main()
