from django.core.management.base import BaseCommand
from django.db import connection

class Command(BaseCommand):
    help = 'Optimize database performance with indexes'

    def handle(self, *args, **options):
        with connection.cursor() as cursor:
            # Add indexes for VAST response tables
            try:
                cursor.execute("""
                    CREATE INDEX IF NOT EXISTS idx_vast_response_ad_id_datetime 
                    ON "VAST_response_GO" ("AD_id", "datetime_timestamp");
                """)
                
                cursor.execute("""
                    CREATE INDEX IF NOT EXISTS idx_vast_response_status 
                    ON "VAST_response_GO" ("status");
                """)
                
                cursor.execute("""
                    CREATE INDEX IF NOT EXISTS idx_vast_response_adspot 
                    ON "VAST_response_GO" ("ad_spot_id");
                """)
                
                self.stdout.write(
                    self.style.SUCCESS('Successfully added database indexes')
                )
                
            except Exception as e:
                self.stdout.write(
                    self.style.ERROR(f'Failed to add indexes: {str(e)}')
                )
