#!/usr/bin/env python3
"""
Database reset script for the persona module.
This script will drop and recreate the store_personas table with the correct schema.
"""

import sys
import os
from datetime import datetime, timezone

# Add the project root to the Python path
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from src.utils.db import get_db_session
from src.marketing.apps.persona.model import StorePersona


def reset_persona_table():
    """Drop and recreate the store_personas table."""
    print("🔄 Resetting persona database table...")
    
    try:
        # Get database session
        db = get_db_session()
        
        # Drop the table if it exists
        print("🗑️ Dropping existing table...")
        StorePersona.__table__.drop(db.bind, checkfirst=True)
        
        # Create the table with correct schema
        print("🏗️ Creating new table with correct schema...")
        StorePersona.__table__.create(db.bind, checkfirst=True)
        
        print("✅ Table reset completed successfully!")
        print("\n📋 New table structure:")
        print("   - id: BIGINT PRIMARY KEY")
        print("   - store_id: BIGINT NOT NULL")
        print("   - branch_id: BIGINT")
        print("   - industry: VARCHAR(100)")
        print("   - audience_type: VARCHAR(100)")
        print("   - brand_voice: VARCHAR(100)")
        print("   - content_style: VARCHAR(100)")
        print("   - analysis_summary: TEXT")
        print("   - confidence_score: FLOAT")
        print("   - last_analyzed: TIMESTAMP")
        print("   - created_at: TIMESTAMP")
        print("   - updated_at: TIMESTAMP")
        print("   - is_active: BOOLEAN DEFAULT TRUE")
        
        db.close()
        
    except Exception as e:
        print(f"❌ Error resetting table: {str(e)}")
        import traceback
        traceback.print_exc()
        
        if 'db' in locals():
            db.close()


if __name__ == "__main__":
    print("🚀 Persona Database Reset Tool")
    print("=" * 50)
    
    # Confirm before proceeding
    response = input("⚠️ This will DELETE ALL existing persona data. Continue? (y/N): ")
    
    if response.lower() in ['y', 'yes']:
        reset_persona_table()
    else:
        print("❌ Operation cancelled.")
    
    print("\n🎉 Reset process completed!")
