# -*- coding: utf-8 -*-
from ppms.migration import SQLPacket, PacketCategory


class AddMissingColumnValuesInDT470(SQLPacket):
    """Adding Missing Column Value in DT470"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 14504

    STATEMENTS = {"UPDATE DT470 SET DI063586 = 411 WHERE DI060521 IS NOT NULL AND di060517 IS NULL",
                  "UPDATE DT470 SET DI063586 = 410 WHERE di060521 IS NULL AND di060517 IS NOT NULL",
                  "UPDATE DT470 SET DI063586 = 412 WHERE di060521 IS NOT NULL AND di060517 IS NOT NULL",
                  "UPDATE DT470 SET DI063586 = 413 WHERE di060521 IS NULL AND di060517 IS NULL", }

    def all_columns_exists(self):
        table_name = 'DT470'
        column_names = ['DI063586', 'DI060521', 'DI060517']

        for column_name in column_names:
            if not self.column_exists(table_name=table_name, column_name=column_name):
                return False

        return True

    def fix(self, runner):
        if self.all_columns_exists():
            for statement in self.STATEMENTS:
                self.modify_and_log(statement)

            return self.success()
        
        self.log('Columns necessary for this migration packet are missing')
        self.log('Please check if the following migration packets failed')
        self.log('after_db_import, DB 2: MigrateCostsAndBudget')
        self.log('customizing, DB 7: AddAMissingColumnInDT470')
        return self.fail('The system is missing necessary columns')
