"""rename_entries_to_crm_entries

Revision ID: 244a0b0f35be
Revises: 485d40e86e4b
Create Date: 2026-04-15 20:05:20.835281

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = '244a0b0f35be'
down_revision: Union[str, None] = '485d40e86e4b'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # 1. Drop FK from support_tickets -> entries before touching the entries table
    op.drop_constraint('support_tickets_linked_entry_id_fkey', 'support_tickets', type_='foreignkey')
    # 2. Drop dependent table first, then parent
    op.drop_table('entry_links')
    op.drop_table('entries')
    # 3. Create new tables with crm_ prefix
    op.create_table('crm_entries',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('type', sa.String(length=10), nullable=False),
    sa.Column('title', sa.String(length=500), nullable=False),
    sa.Column('body', sa.Text(), nullable=True),
    sa.Column('status', sa.String(length=20), nullable=True),
    sa.Column('severity', sa.String(length=10), nullable=True),
    sa.Column('author_id', sa.String(length=128), nullable=False),
    sa.Column('author_name', sa.String(length=255), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('crm_entry_links',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('entry_id', sa.UUID(), nullable=False),
    sa.Column('entity_type', sa.String(length=20), nullable=False),
    sa.Column('entity_id', sa.String(length=128), nullable=False),
    sa.ForeignKeyConstraint(['entry_id'], ['crm_entries.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('entry_id', 'entity_type', 'entity_id')
    )
    # 4. Recreate FK on support_tickets pointing at new table
    op.create_foreign_key(None, 'support_tickets', 'crm_entries', ['linked_entry_id'], ['id'], ondelete='SET NULL')


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'support_tickets', type_='foreignkey')
    op.create_foreign_key('support_tickets_linked_entry_id_fkey', 'support_tickets', 'entries', ['linked_entry_id'], ['id'], ondelete='SET NULL')
    op.create_table('entries',
    sa.Column('id', sa.UUID(), autoincrement=False, nullable=False),
    sa.Column('type', sa.VARCHAR(length=10), autoincrement=False, nullable=False),
    sa.Column('title', sa.VARCHAR(length=500), autoincrement=False, nullable=False),
    sa.Column('body', sa.TEXT(), autoincrement=False, nullable=True),
    sa.Column('status', sa.VARCHAR(length=20), autoincrement=False, nullable=True),
    sa.Column('severity', sa.VARCHAR(length=10), autoincrement=False, nullable=True),
    sa.Column('author_id', sa.VARCHAR(length=128), autoincrement=False, nullable=False),
    sa.Column('author_name', sa.VARCHAR(length=255), autoincrement=False, nullable=True),
    sa.Column('created_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=False),
    sa.Column('updated_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=False),
    sa.PrimaryKeyConstraint('id', name='entries_pkey'),
    postgresql_ignore_search_path=False
    )
    op.create_table('entry_links',
    sa.Column('id', sa.UUID(), autoincrement=False, nullable=False),
    sa.Column('entry_id', sa.UUID(), autoincrement=False, nullable=False),
    sa.Column('entity_type', sa.VARCHAR(length=20), autoincrement=False, nullable=False),
    sa.Column('entity_id', sa.VARCHAR(length=128), autoincrement=False, nullable=False),
    sa.ForeignKeyConstraint(['entry_id'], ['entries.id'], name='entry_links_entry_id_fkey', ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id', name='entry_links_pkey'),
    sa.UniqueConstraint('entry_id', 'entity_type', 'entity_id', name='entry_links_entry_id_entity_type_entity_id_key')
    )
    op.drop_table('crm_entry_links')
    op.drop_table('crm_entries')
    # ### end Alembic commands ###
