<?php

declare(strict_types=1);

if (!\defined('_PS_VERSION_')) {
    exit;
}

function upgrade_module_4_5_0(myvetshopclinique $module): bool
{
    $db = \Db::getInstance();

    $nextInvoiceNumber = $db->getValue('SELECT MAX(`number`) + 1 FROM `' . _DB_PREFIX_ . 'order_invoice`');
    if (null === $nextInvoiceNumber) {
        $nextInvoiceNumber = 1;
    }

    Db::getInstance()->execute(
        'CREATE SEQUENCE IF NOT EXISTS `myvetshop_invoice_number` START ' . (int) $nextInvoiceNumber . ' NOCACHE'
    );

    $module->uninstallOverrides();
    $module->installOverrides();

    return true;
}
