# -*- coding: utf-8 -*-
from ppms import ppms
from ppms.constants import *
from ppms.migration import SQLPacket, PacketCategory, MaxVersionDependency, VersionType, MinVersionDependency, \
    PacketDependency, PacketState, ChangeDTPPacket

ALTER_TABLE_STATEMENTS = {DBMS_ORACLE: ("alter table DT496 add CAN_INSERT number(5,0) default 1",
                                        "update DT496 set CAN_INSERT = 1",
                                        "alter table DT496 add constraint DT496CAN_INSERT_NOT_NULL check(CAN_INSERT is not null)",
                                        "alter table DT497 add CAN_INSERT number(5,0) default 1",
                                        "update DT497 set CAN_INSERT = 1",
                                        "alter table DT497 add constraint DT497CAN_INSERT_NOT_NULL check(CAN_INSERT is not null)"),

                          DBMS_MSSQL:  ("alter table DT496 add CAN_INSERT numeric(5,0) default 1",
                                        "update DT496 set CAN_INSERT = 1",
                                        "alter table DT496 add constraint DT496CAN_INSERT_NOT_NULL check([CAN_INSERT] is not null)",
                                        "alter table DT497 add CAN_INSERT numeric(5,0) default 1",
                                        "update DT497 set CAN_INSERT = 1",
                                        "alter table DT497 add constraint DT497CAN_INSERT_NOT_NULL check([CAN_INSERT] is not null)")}


class A_AddProcessInsertionFlagSchema(SQLPacket):
    """Port the process insertion schema changes"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13738

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

    def fix(self, runner):
        for statement in ALTER_TABLE_STATEMENTS[ppms.db_get_dbms_name()]:
            self.modify_and_log(statement)

        return self.success()


class B_AddProcessInsertionFlagCustomizing(SQLPacket):
    """Port the process insertion flag DataItem"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13738

    FILENAME = 'wi13738_insertion_flag.sql'
    __PATH = r'migration.hotfix.S_39_5_13.wi13738_process_model_insertion_flag.A_AddProcessInsertionFlagSchema'

    def get_dependencies(self):
        return (MinVersionDependency(version='39.4', version_type=VersionType.CUSTOMIZING),
                MaxVersionDependency(version='39.5', version_type=VersionType.CUSTOMIZING),
                PacketDependency(packet_path=self.__PATH, expected_state=PacketState.INSTALLED))

    def fix(self, runner):
        self.apply_dml_file(self.sql_planta_de_dbms, self.FILENAME)

        return self.success()


class C_ChangeCustomizingForInsertionFlag(ChangeDTPPacket):
    """Port the process insertion dataarea settings"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13738

    __PATH = r'migration.hotfix.S_39_5_13.wi13738_process_model_insertion_flag.A_AddProcessInsertionFlagSchema'

    def get_dependencies(self):
        return (MinVersionDependency(version='39.4', version_type=VersionType.CUSTOMIZING),
                MaxVersionDependency(version='39.5', version_type=VersionType.CUSTOMIZING),
                PacketDependency(packet_path=self.__PATH, expected_state=PacketState.INSTALLED))

    def fix(self, runner):
        for da_id in (337693, 339367):
            record = self.get_record(dt_num=406, key_list=[da_id], di_list=['create_enabled', 'insert_enabled'])
            if not self.change_value(record=record, attribute='create_enabled', old_value=1, new_value=0):
                return self.fail('Could not change customizing')

            if not self.change_value(record=record, attribute='insert_enabled', old_value=1, new_value=0):
                return self.fail('Could not change customizing')

        return self.success()