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

import itertools

from ppms import ppms
from ppms.constants import *
from ppms.migration import SQLPacket, PacketCategory, VersionType, MinVersionDependency, MaxVersionDependency


def flatten(nested_list):
    return list(itertools.chain.from_iterable(nested_list))


class FixEarnedValueSchema(SQLPacket):
    """Port procedures and views used in EVA"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 10922

    DDL_FILENAMES = ('WI10922_DT827_CONDENSED.sql',
                     'WI10922_V_EVA_PR.sql',
                     'WI10922_V_EVA_VG.sql',
                     'WI10922_V_EVA_VGR.sql',
                     'WI10922_V_PLANTA_CUMU_DT827.sql')
    DROP_OBJECTS = ('T_RSUM',
                    'DT827_CONDENSED',
                    'V_EVA_VGR',
                    'V_EVA_VGR_B',
                    'V_EVA_JOIN_BASELINE',
                    'V_EVA_BASELINE',
                    'V_EVA_REPORTS',
                    'V_EVA_ALL_MONTHS',
                    'V_EVA_PR',
                    'V_EVA_VG',
                    'V_PLANTA_CUMU_DT827',
                    'P_RunningSum_827')
    DROP_QUERY = "SELECT 'DROP ' || object_type || ' ' || object_name FROM sys.all_objects WHERE upper(object_name) = '{object}' AND owner = user"

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

    def fix(self, runner):
        if self.DBMS == DBMS_ORACLE:
            for object in self.DROP_OBJECTS:
                for statement in flatten(ppms.db_select(self.DROP_QUERY.format(object=object))):
                    try:
                        ppms.db_modify(statement)
                    except ppms.DatabaseError as e:
                        if 'ORA-00942' not in str(e):
                            return self.fail("Exception occured when executing statement \"{statement}\": \"{exception}\"".format(statement=statement, exception=e))

        elif self.DBMS == DBMS_MSSQL:
            self.apply_rollback_ddl_file(self.sql_planta_de_dbms, 'WI10922_fix_earned_value_schema.sql', True)

        for filename in self.DDL_FILENAMES:
            self.apply_fix_ddl_file(self.sql_planta_de_dbms, filename, True)

        self.apply_fix_ddl_file(self.sql_planta_de_dbms, 'WI10922_P_RUNNINGSUM_827.sql')

        return self.success()


class FixEarnedValueCU(SQLPacket):
    """Port customizing used in EVA"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 10922

    DML_FILENAME = 'WI10922_fix_earned_value.sql'

    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):
        self.apply_dml_file(self.sql_planta_de_dbms, self.DML_FILENAME)

        return self.success()