from alembic import op
import sqlalchemy as sa
from sqlalchemy import inspect
revision = 'add_enterprise_homepage_fields'
down_revision = 'service_detail_001'  # Latest migration head
branch_labels = None
depends_on = None

def column_exists(table_name, column_name, connection):
    """Check if a column exists in a table."""
    inspector = inspect(connection)
    columns = [col['name'] for col in inspector.get_columns(table_name)]
    return column_name in columns

def upgrade() -> None:
    connection = op.get_bind()
    
    # Helper function to add column if it doesn't exist
    def add_column_if_not_exists(table_name, column, column_name):
        if not column_exists(table_name, column_name, connection):
            op.add_column(table_name, column)
    
    # Hero video fields - use TEXT to avoid row size limit
    add_column_if_not_exists('page_contents', sa.Column('hero_video_url', sa.Text(), nullable=True), 'hero_video_url')
    add_column_if_not_exists('page_contents', sa.Column('hero_video_poster', sa.Text(), nullable=True), 'hero_video_poster')
    
    # Features section fields - use TEXT for subtitles
    add_column_if_not_exists('page_contents', sa.Column('features_section_title', sa.Text(), nullable=True), 'features_section_title')
    add_column_if_not_exists('page_contents', sa.Column('features_section_subtitle', sa.Text(), nullable=True), 'features_section_subtitle')
    
    # Stats section fields - use TEXT for subtitles
    add_column_if_not_exists('page_contents', sa.Column('stats_section_title', sa.Text(), nullable=True), 'stats_section_title')
    add_column_if_not_exists('page_contents', sa.Column('stats_section_subtitle', sa.Text(), nullable=True), 'stats_section_subtitle')
    
    # Rooms section fields - use TEXT for all string fields
    add_column_if_not_exists('page_contents', sa.Column('rooms_section_title', sa.Text(), nullable=True), 'rooms_section_title')
    add_column_if_not_exists('page_contents', sa.Column('rooms_section_subtitle', sa.Text(), nullable=True), 'rooms_section_subtitle')
    add_column_if_not_exists('page_contents', sa.Column('rooms_section_button_text', sa.Text(), nullable=True), 'rooms_section_button_text')
    add_column_if_not_exists('page_contents', sa.Column('rooms_section_button_link', sa.Text(), nullable=True), 'rooms_section_button_link')
    add_column_if_not_exists('page_contents', sa.Column('rooms_section_enabled', sa.Boolean(), nullable=True, server_default='1'), 'rooms_section_enabled')
    
    # Services section fields - use TEXT for all string fields
    add_column_if_not_exists('page_contents', sa.Column('services_section_button_text', sa.Text(), nullable=True), 'services_section_button_text')
    add_column_if_not_exists('page_contents', sa.Column('services_section_button_link', sa.Text(), nullable=True), 'services_section_button_link')
    add_column_if_not_exists('page_contents', sa.Column('services_section_limit', sa.Integer(), nullable=True), 'services_section_limit')
    
    # Sections enabled (JSON field for toggling sections)
    add_column_if_not_exists('page_contents', sa.Column('sections_enabled', sa.Text(), nullable=True), 'sections_enabled')
    
    # Newsletter section fields - use TEXT for all string fields
    add_column_if_not_exists('page_contents', sa.Column('newsletter_section_title', sa.Text(), nullable=True), 'newsletter_section_title')
    add_column_if_not_exists('page_contents', sa.Column('newsletter_section_subtitle', sa.Text(), nullable=True), 'newsletter_section_subtitle')
    add_column_if_not_exists('page_contents', sa.Column('newsletter_placeholder', sa.Text(), nullable=True), 'newsletter_placeholder')
    add_column_if_not_exists('page_contents', sa.Column('newsletter_button_text', sa.Text(), nullable=True), 'newsletter_button_text')
    add_column_if_not_exists('page_contents', sa.Column('newsletter_enabled', sa.Boolean(), nullable=True, server_default='0'), 'newsletter_enabled')
    
    # Trust badges section fields - use TEXT for subtitles
    add_column_if_not_exists('page_contents', sa.Column('trust_badges_section_title', sa.Text(), nullable=True), 'trust_badges_section_title')
    add_column_if_not_exists('page_contents', sa.Column('trust_badges_section_subtitle', sa.Text(), nullable=True), 'trust_badges_section_subtitle')
    add_column_if_not_exists('page_contents', sa.Column('trust_badges', sa.Text(), nullable=True), 'trust_badges')
    add_column_if_not_exists('page_contents', sa.Column('trust_badges_enabled', sa.Boolean(), nullable=True, server_default='0'), 'trust_badges_enabled')
    
    # Promotions section fields - use TEXT for subtitles
    add_column_if_not_exists('page_contents', sa.Column('promotions_section_title', sa.Text(), nullable=True), 'promotions_section_title')
    add_column_if_not_exists('page_contents', sa.Column('promotions_section_subtitle', sa.Text(), nullable=True), 'promotions_section_subtitle')
    add_column_if_not_exists('page_contents', sa.Column('promotions', sa.Text(), nullable=True), 'promotions')
    add_column_if_not_exists('page_contents', sa.Column('promotions_enabled', sa.Boolean(), nullable=True, server_default='0'), 'promotions_enabled')
    
    # Blog section fields - use TEXT for subtitles
    add_column_if_not_exists('page_contents', sa.Column('blog_section_title', sa.Text(), nullable=True), 'blog_section_title')
    add_column_if_not_exists('page_contents', sa.Column('blog_section_subtitle', sa.Text(), nullable=True), 'blog_section_subtitle')
    add_column_if_not_exists('page_contents', sa.Column('blog_posts_limit', sa.Integer(), nullable=True), 'blog_posts_limit')
    add_column_if_not_exists('page_contents', sa.Column('blog_enabled', sa.Boolean(), nullable=True, server_default='0'), 'blog_enabled')

def downgrade() -> None:
    # Blog section fields
    op.drop_column('page_contents', 'blog_enabled')
    op.drop_column('page_contents', 'blog_posts_limit')
    op.drop_column('page_contents', 'blog_section_subtitle')
    op.drop_column('page_contents', 'blog_section_title')
    
    # Promotions section fields
    op.drop_column('page_contents', 'promotions_enabled')
    op.drop_column('page_contents', 'promotions')
    op.drop_column('page_contents', 'promotions_section_subtitle')
    op.drop_column('page_contents', 'promotions_section_title')
    
    # Trust badges section fields
    op.drop_column('page_contents', 'trust_badges_enabled')
    op.drop_column('page_contents', 'trust_badges')
    op.drop_column('page_contents', 'trust_badges_section_subtitle')
    op.drop_column('page_contents', 'trust_badges_section_title')
    
    # Newsletter section fields
    op.drop_column('page_contents', 'newsletter_enabled')
    op.drop_column('page_contents', 'newsletter_button_text')
    op.drop_column('page_contents', 'newsletter_placeholder')
    op.drop_column('page_contents', 'newsletter_section_subtitle')
    op.drop_column('page_contents', 'newsletter_section_title')
    
    # Sections enabled
    op.drop_column('page_contents', 'sections_enabled')
    
    # Services section fields
    op.drop_column('page_contents', 'services_section_limit')
    op.drop_column('page_contents', 'services_section_button_link')
    op.drop_column('page_contents', 'services_section_button_text')
    
    # Rooms section fields
    op.drop_column('page_contents', 'rooms_section_enabled')
    op.drop_column('page_contents', 'rooms_section_button_link')
    op.drop_column('page_contents', 'rooms_section_button_text')
    op.drop_column('page_contents', 'rooms_section_subtitle')
    op.drop_column('page_contents', 'rooms_section_title')
    
    # Stats section fields
    op.drop_column('page_contents', 'stats_section_subtitle')
    op.drop_column('page_contents', 'stats_section_title')
    
    # Features section fields
    op.drop_column('page_contents', 'features_section_subtitle')
    op.drop_column('page_contents', 'features_section_title')
    
    # Hero video fields
    op.drop_column('page_contents', 'hero_video_poster')
    op.drop_column('page_contents', 'hero_video_url')

