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


ADD_COLUMNS = {DBMS_MSSQL: ("""alter table DT499 add DI061407 varchar(8)""",
                            """alter table DT499 add DI062282 varchar(18)""",
                            """alter table DT499 add DI062283 varchar(18)""",
                            """alter table DT496 add UUID uniqueidentifier default newid() not null""",
                            """alter table DT497 add UUID uniqueidentifier default newid() not null""",
                            """alter table DT498 add UUID uniqueidentifier default newid() not null""",
                            """alter table DT499 add UUID uniqueidentifier default newid() not null"""),

               DBMS_ORACLE: ("""alter table DT499 add DI061407 varchar2(8 char)""",
                             """alter table DT499 add DI062282 varchar2(18 char)""",
                             """alter table DT499 add DI062283 varchar2(18 char)""",
                             """alter table DT496 add UUID raw(16) default SYS_GUID() not null""",
                             """alter table DT497 add UUID raw(16) default SYS_GUID() not null""",
                             """alter table DT498 add UUID raw(16) default SYS_GUID() not null""",
                             """alter table DT499 add UUID raw(16) default SYS_GUID() not null""")}


class A_PortProcessModelSchema(SQLPacket):
    """Adds new data items to DT 496, 497, 498 and  499

    061407, 062282, 062283, 060965, 060966, 060967, 060968, 060049, 062228, 062229, 062287, 062288, 063227"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13721

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

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

        return self.success()


class B_PortProcessModelDataitems(SQLPacket):
    """Port new dataitems for the process model from Venus to Venus on Earth"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13721

    FILENAME = 'wi13721_process_model.sql'
    __PATH = r'migration.hotfix.S_39_5_13.wi13721_process_model.A_PortProcessModelSchema'

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

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

        return self.success()


class C_SetProcessModelUUID(ChangeDTPPacket):
    """Set the UUID DI in DT 415 for the new UUID columns in the process model tables"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13721

    __PATH = r'migration.hotfix.S_39_5_13.wi13721_process_model.B_PortProcessModelDataitems'

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

    def fix(self, runner):
        for table, uuid_di in (('496', '060965'), ('497', '060966'),
                               ('498', '060967'), ('499', '060968')):
            record = self.get_record(dt_num=415, key_list=[table], di_list=['uuid_di'])
            if not self.change_value(record=record, attribute='uuid_di', old_value='', new_value=uuid_di):
                return self.fail('Could not set UUID DI for table %s' % table)

        return self.success()