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

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


STATEMENT = {DBMS_MSSQL: "UPDATE DT516 SET DI027357 = ISNULL(DI010434, '') + ' ' + ISNULL(DI010433, '')",
             DBMS_ORACLE: "UPDATE DT516 SET DI027357 = DI010434 || ' ' || DI010433"}


class UpdatePersonNames(SQLPacket):
    """Updates naming convention from 'Lastname, Firstname' to 'Firstname Lastname'"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 19417

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

    def fix(self, packet_runner):
        self.modify_and_log(STATEMENT[self.DBMS])

        return self.success()
