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

from ppms import ppms
from ppms.migration.packet import *


class SucceedOldDeactivationPacket(SucceedOldPacket):
    '''Mark the old deactivation package as done in favor of the new one'''
    wi_number = 11342

    def fix(self, runner):
        if self.mark_packet_as_done('migration.hotfix.S_39_5_2.WI11342_customer_DIs_deactivation_for_Q1BQ2B.CustomerDIDeactivation'):
            return self.success()

        return self.fail()


class CustomerDIDeactivationSansDT400(SQLPacket):
    '''Deactivates customer-changed DIs that belong to Planta system tables (Q1B/Q2B)
       and writes the DIs that it finds into log file.

       These tables must not be changed, and the customer-inserted DIs belonging to them will be deactivated by this migration package.
       If you have inserted columns in these tables, you must correct your customizing
       (e.g. by outsourcing them to a specific table that does not belong to Q1B/Q2B, with a 1:1 relation to the table in Q1B/Q2B).

       After correcting your customizing, delete the columns in question to let this migration package succeed.

       Also reactivates customer-changed DIs that belonged to DT400 and were erroneously deactivated in old migration package
    '''
    version = 1
    run_on_startup = True
    done_after_success = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 11342

    # All DIs from Q1B & Q2B that were inserted by customer (identified by not having a standard license)
    dis_to_deactivate = """SELECT DI000238 FROM DT412 WHERE
                           (DI000239 IN (SELECT DI000265 FROM DT415 WHERE DI000265 != 400
                                         AND DI000266 IN ('Q1B', 'Q2B'))
                            AND (DI001869 NOT IN ('000', '011', 'ZZZ'))
                            AND DI000275 = 0)"""
    deactivate_dis = """UPDATE DT412 SET ENABLED = 0 WHERE DI000238 = '{0}'"""
    dis_to_reactivate = """SELECT DI000238 FROM DT412 WHERE DI000239 = 400 AND (DI001869 NOT IN ('000', '011', 'ZZZ')) AND ENABLED = 0"""
    reactivate_dis = """UPDATE DT412 SET ENABLED = 1 WHERE DI000238 = '{0}'"""

    def change_specific_dis(self, select_query, change_query):
        dis = ppms.db_select(select_query)

        for di in dis:
            self.modify_and_log(change_query.format(di[0]))

        return len(dis)

    def fix(self, runner):
        # New DIs are allowed in non-existing DT400
        reactivated = self.change_specific_dis(self.dis_to_reactivate, self.reactivate_dis)
        if (reactivated > 0):
            self.log('{0} columns from DT400 that were erroneously deactivated have been reactivated'.format(reactivated))

        deactivated = self.change_specific_dis(self.dis_to_deactivate, self.deactivate_dis)
        if (deactivated > 0):
            # There should be no DIs in Q1B/Q2B that were modified by customer
            self.log('Found {0} columns from Q1B / Q2B in database that were changed by customer. They will be deactived. Please check your customizing!'.format(reactivated))
            return self.fail()
        else:
            self.log('No customer-changed DIs in system tables found, everything ok.')
            return self.success()

