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


class SetPortfolioDefaultPicture(SQLPacket):
    """Sets the portfolio logo as default portfolio picture for existing portfolios."""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 20255

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

    def fix(self, packet_runner):
        update_statement = """update DT870 set PORTFOLIO_PICTURE = (select p.uuid from PICTURE p 
                                            where p.PICTURE_TYPE = 870 and p.DEFAULT_PICTURE = 1) 
                                            where PORTFOLIO_PICTURE is null"""

        self.modify_and_log(update_statement)
        return self.success()
