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

class ChangeLoadReportIndex(SQLPacket):
    """Adds the cost type to the index for load per report (DT827)"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 18365

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

    def fix(self, packet_runner):
        statements = {DBMS_ORACLE: ["""DROP INDEX I_DT827_COSTS""",
                         """CREATE INDEX I_DT827_COSTS ON DT827 (DI008516 ASC,
                                 DI008535 ASC, DI008518 ASC,
                                 DI008523 ASC, DI008660 ASC, DI008661 ASC,
                                 DI008663 ASC, DI008664 ASC, DI008665 ASC,
                                 DI008662 ASC, DI008519 ASC)"""],
                             DBMS_MSSQL: ["""DROP INDEX DT827.I_DT827_COSTS""",
                         """CREATE INDEX I_DT827_COSTS ON DT827 (DI008516 ASC,
                                 DI008535 ASC, DI008518 ASC,
                                 DI008523 ASC, DI008660 ASC, DI008661 ASC,
                                 DI008663 ASC, DI008664 ASC, DI008665 ASC,
                                 DI008662 ASC, DI008519 ASC)"""]}

        try:
            drop_statement = statements[self.DBMS][0]
            self.modify_and_log(drop_statement)
        except ppms.DatabaseError:
            self.log("Skipped dropping index I_DT827_COSTS as it doesn't exist.")
        
        create_statement = statements[self.DBMS][1]
        self.modify_and_log(create_statement)

        return self.success()