# vim: set fileencoding=utf-8

"""Fix SDVX song display issue in music data.

Revision ID: 5c927879651b
Revises: 5fb631197b27
Create Date: 2019-09-08 06:12:56.681720

"""
from alembic import op
from sqlalchemy.sql import text


# revision identifiers, used by Alembic.
revision = '5c927879651b'
down_revision = '5fb631197b27'
branch_labels = None
depends_on = None


def upgrade():
    conn = op.get_bind()

    sql = "SELECT id, name, artist FROM music WHERE game = 'sdvx'"
    results = conn.execute(text(sql), {})
    for result in results:
        musicid = result['id']
        new_title = title = result['name']
        new_artist = artist = result['artist']

        # Fix accent issues with title/artist
        accent_lut: Dict[str, str] = {
            '驩': 'Ø',
            '齲': '♥',
            '齶': '♡',
            '趁': 'Ǣ',
            '騫': 'á',
            '曦': 'à',
            '驫': 'ā',
            '齷': 'é',
            '曩': 'è',
            '䧺': 'ê',
            '骭': 'ü',
        }

        for orig, rep in accent_lut.items():
            new_title = new_title.replace(orig, rep)
            new_artist = new_artist.replace(orig, rep)

        if new_title != title or new_artist != artist:
            sql = "UPDATE music SET name = :name, artist = :artist WHERE id = :id AND game = 'sdvx'"
            conn.execute(
                text(sql),
                {
                    'id': musicid,
                    'name': new_title,
                    'artist': new_artist,
                },
            )


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    pass
    # ### end Alembic commands ###
