# -*- coding: utf-8 -*-
from ppms.migration import *
import hql


class AddDisLongestPathAndBucketId(ChangeDTPPacket, SQLPacket):
    """add longest__path to aob,
    add bucketID to project.
    longest_critical_path to match the bitfield calculated by rouge scheduler. The new Values are:
    0 = No critical Path
    1 = on Projects Critical Path
    2 = on Buckets Critical Path
    3 = on Projects and Buckets Critical Path"""
    run_on_startup = True
    done_after_success = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 21970

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

    filename_aob_ddl = 'WI21970_create_aob_longest_path_di_ddl.sql'
    filename_bucket_ddl = 'WI21970_create_project_bucket_di_ddl.sql'

    def fix(self, packet_runner):
        # Since the MP-Framework disregards cross dependencies we need to create this column in Server and CU MPs
        self.log_heading("Dropping necessary column(s)")
        self.drop_column('DT461', 'BUCKET_ID')
        self.drop_column('DT823', 'BUCKET_ID')
        self.drop_column('DT465', 'LONGEST_PATH')
        self.drop_column('DT825', 'LONGEST_PATH')

        self.create_aob_critical_path_di()
        self.create_project_bucket_dis()
        return self.success()

    def create_aob_critical_path_di(self):
        self.log_heading("Inserting new longest path DIs (AOB)")
        self.apply_fix_ddl_file(self.sql_planta_de_dbms, self.filename_aob_ddl, split=True, separator=';/')

    def create_project_bucket_dis(self):
        self.log_heading("Inserting new bucket DIs")
        self.apply_fix_ddl_file(self.sql_planta_de_dbms, self.filename_bucket_ddl, split=True, separator=';/')

