# -*- coding: utf-8 -*-
from ppms.migration import SQLPacket, PacketCategory, MaxVersionDependency, VersionType

TEXT_CONSTANTS = "('001773', '001251')"

DELETE_QUERIES = [
    "DELETE DT435 WHERE DI023517 = '000296' AND DI001538 IN (SELECT DI000296 FROM DT425 WHERE DI000295 IN %s)" %
    TEXT_CONSTANTS,
    "DELETE DT435 WHERE DI023517 = '024662' AND DI001538 IN (SELECT DI024662 FROM DT425 WHERE DI000295 IN %s)" %
    TEXT_CONSTANTS,
    "DELETE DT425 WHERE DI000295 IN %s" % TEXT_CONSTANTS]


class PortChangeFunctionalIDTextConstants(SQLPacket):
    """Port a new text constant for the change functional id feature

    Also updates an old one
    """
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 10074

    FILENAME = 'wi10074_functional_id_text_constants.sql'

    def get_dependencies(self):
        return MaxVersionDependency(version='39.5.7', version_type=VersionType.CUSTOMIZING),

    def fix(self, runner):
        for delete_query in DELETE_QUERIES:
            self.modify_and_log(delete_query)

        self.apply_dml_file(self.sql_planta_de_dbms, self.FILENAME)

        return self.success()