"""add guest profile crm tables

Revision ID: add_guest_profile_crm
Revises: ff515d77abbe
Create Date: 2024-01-01 12:00:00.000000

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = 'add_guest_profile_crm'
down_revision = '9bb08492a382'
branch_labels = None
depends_on = None


def upgrade() -> None:
    # Add new fields to users table
    op.add_column('users', sa.Column('is_vip', sa.Boolean(), nullable=False, server_default='0'))
    op.add_column('users', sa.Column('lifetime_value', sa.Numeric(10, 2), nullable=True, server_default='0'))
    op.add_column('users', sa.Column('satisfaction_score', sa.Numeric(3, 2), nullable=True))
    op.add_column('users', sa.Column('last_visit_date', sa.DateTime(), nullable=True))
    op.add_column('users', sa.Column('total_visits', sa.Integer(), nullable=False, server_default='0'))
    
    # Create guest_preferences table
    op.create_table(
        'guest_preferences',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('user_id', sa.Integer(), nullable=False),
        sa.Column('preferred_room_location', sa.String(length=100), nullable=True),
        sa.Column('preferred_floor', sa.Integer(), nullable=True),
        sa.Column('preferred_room_type_id', sa.Integer(), nullable=True),
        sa.Column('preferred_amenities', sa.JSON(), nullable=True),
        sa.Column('special_requests', sa.Text(), nullable=True),
        sa.Column('preferred_services', sa.JSON(), nullable=True),
        sa.Column('preferred_contact_method', sa.String(length=50), nullable=True),
        sa.Column('preferred_language', sa.String(length=10), nullable=True, server_default='en'),
        sa.Column('dietary_restrictions', sa.JSON(), nullable=True),
        sa.Column('additional_preferences', sa.JSON(), nullable=True),
        sa.Column('created_at', sa.DateTime(), nullable=False),
        sa.Column('updated_at', sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
        sa.ForeignKeyConstraint(['preferred_room_type_id'], ['room_types.id'], ),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_guest_preferences_id'), 'guest_preferences', ['id'], unique=False)
    op.create_index(op.f('ix_guest_preferences_user_id'), 'guest_preferences', ['user_id'], unique=False)
    
    # Create guest_notes table
    op.create_table(
        'guest_notes',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('user_id', sa.Integer(), nullable=False),
        sa.Column('created_by', sa.Integer(), nullable=False),
        sa.Column('note', sa.Text(), nullable=False),
        sa.Column('is_important', sa.Boolean(), nullable=False, server_default='0'),
        sa.Column('is_private', sa.Boolean(), nullable=False, server_default='0'),
        sa.Column('created_at', sa.DateTime(), nullable=False),
        sa.Column('updated_at', sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
        sa.ForeignKeyConstraint(['created_by'], ['users.id'], ),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_guest_notes_id'), 'guest_notes', ['id'], unique=False)
    op.create_index(op.f('ix_guest_notes_user_id'), 'guest_notes', ['user_id'], unique=False)
    
    # Create guest_tags table
    op.create_table(
        'guest_tags',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('name', sa.String(length=50), nullable=False),
        sa.Column('color', sa.String(length=7), nullable=True, server_default='#3B82F6'),
        sa.Column('description', sa.String(length=255), nullable=True),
        sa.Column('created_at', sa.DateTime(), nullable=False),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('name')
    )
    op.create_index(op.f('ix_guest_tags_id'), 'guest_tags', ['id'], unique=False)
    op.create_index(op.f('ix_guest_tags_name'), 'guest_tags', ['name'], unique=True)
    
    # Create guest_tag_associations table
    op.create_table(
        'guest_tag_associations',
        sa.Column('user_id', sa.Integer(), nullable=False),
        sa.Column('tag_id', sa.Integer(), nullable=False),
        sa.Column('created_at', sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(['tag_id'], ['guest_tags.id'], ),
        sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
        sa.PrimaryKeyConstraint('user_id', 'tag_id')
    )
    
    # Create guest_communications table
    op.create_table(
        'guest_communications',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('user_id', sa.Integer(), nullable=False),
        sa.Column('staff_id', sa.Integer(), nullable=True),
        sa.Column('communication_type', sa.Enum('email', 'phone', 'sms', 'chat', 'in_person', 'other', name='communicationtype'), nullable=False),
        sa.Column('direction', sa.Enum('inbound', 'outbound', name='communicationdirection'), nullable=False),
        sa.Column('subject', sa.String(length=255), nullable=True),
        sa.Column('content', sa.Text(), nullable=False),
        sa.Column('booking_id', sa.Integer(), nullable=True),
        sa.Column('is_automated', sa.Boolean(), nullable=False, server_default='0'),
        sa.Column('communication_metadata', sa.Text(), nullable=True),
        sa.Column('created_at', sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(['booking_id'], ['bookings.id'], ),
        sa.ForeignKeyConstraint(['staff_id'], ['users.id'], ),
        sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_guest_communications_id'), 'guest_communications', ['id'], unique=False)
    op.create_index(op.f('ix_guest_communications_user_id'), 'guest_communications', ['user_id'], unique=False)
    
    # Create guest_segments table
    op.create_table(
        'guest_segments',
        sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
        sa.Column('name', sa.String(length=100), nullable=False),
        sa.Column('description', sa.Text(), nullable=True),
        sa.Column('criteria', sa.JSON(), nullable=True),
        sa.Column('is_active', sa.Boolean(), nullable=False, server_default='1'),
        sa.Column('created_at', sa.DateTime(), nullable=False),
        sa.Column('updated_at', sa.DateTime(), nullable=False),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('name')
    )
    op.create_index(op.f('ix_guest_segments_id'), 'guest_segments', ['id'], unique=False)
    op.create_index(op.f('ix_guest_segments_name'), 'guest_segments', ['name'], unique=True)
    
    # Create guest_segment_associations table
    op.create_table(
        'guest_segment_associations',
        sa.Column('user_id', sa.Integer(), nullable=False),
        sa.Column('segment_id', sa.Integer(), nullable=False),
        sa.Column('assigned_at', sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(['segment_id'], ['guest_segments.id'], ),
        sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
        sa.PrimaryKeyConstraint('user_id', 'segment_id')
    )


def downgrade() -> None:
    # Drop tables in reverse order
    op.drop_table('guest_segment_associations')
    op.drop_table('guest_segments')
    op.drop_table('guest_communications')
    op.drop_table('guest_tag_associations')
    op.drop_table('guest_tags')
    op.drop_table('guest_notes')
    op.drop_table('guest_preferences')
    
    # Remove columns from users table
    op.drop_column('users', 'total_visits')
    op.drop_column('users', 'last_visit_date')
    op.drop_column('users', 'satisfaction_score')
    op.drop_column('users', 'lifetime_value')
    op.drop_column('users', 'is_vip')

