from ppms import ppms
from ppms.constants import MSG_REPLY_YES
from ppms.migration import PacketCategory, SinglePacketRunner, BasePacket, MinVersionDependency, VersionType, \
                           PacketState


class ForceCtgCleanup(BasePacket):
    """Forces the execution of the DB39.5.5 packet CleanupOldKoAGrFields, which had wrong dependencies."""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 22278

    PACKET_PATH = "migration.customizing.DB_39_5_5.WI9158_cleanup_old_koagr_fields.CleanupOldKoAGrFields"
    
    def get_dependencies(self):
        return MinVersionDependency(version='39.5.10', version_type=VersionType.CUSTOMIZING)

    def fix(self, packet_runner):
        cleanup_package = BasePacket.from_packet_path(self.PACKET_PATH)
        self.log('Running package: {pk_name}'.format(pk_name=cleanup_package))
        if cleanup_package.last_state == PacketState.UNNECESSARY:
            with ppms.autoreply_to_message(message_id="1146", reply=MSG_REPLY_YES):
                runner = SinglePacketRunner(cleanup_package.migrationrule)
                runner.run()
        
        return self.success()
