# -*- coding: utf-8 -*-

from ppms.migration import SQLPacket, PacketCategory
from ppms import ppms


class MigrateUsersToCorrectPLANTALinkRole(SQLPacket):
    """Migrate everyone that was using the wrong PLANTA Link role to the correct role"""
    wi_number = 19688
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY

    def fix(self, runner):
        give_correct_role_query = """UPDATE
                                       DT518
                                     SET
                                       DI010699 = '01100028'
                                     WHERE
                                       DI010699 = '01100043'
                                       AND DI010697 NOT IN (SELECT DI010697
                                                            FROM DT518
                                                            WHERE DI010699 = '01100028')"""

        self.modify_and_log(give_correct_role_query)

        delete_leftover_roles_query = """DELETE
                                           DT518
                                         WHERE
                                           DI010699 = '01100043'"""

        self.modify_and_log(delete_leftover_roles_query)

        return self.success()
