<?php

class Upgrade1 extends UpgradeNew
{
    protected $priority = -1000;

    protected function isAllowed()
    {
        return !defined('UPGRADE_DB_ONLY');
    }

    private function runCommand($command, $dir)
    {
        $cwd = getcwd();

        chdir($dir);
        exec($command, $out, $return);
        $out = join("\n", $out);
        if ($return != 0) {
            echo "Execution failed: {$command}\n{$out}\n";
        }

        chdir($cwd);

        return $out;
    }

    public function check_DBExists()
    {
        if (isFunctionalTests()) {
            return false;
        }

        $connection = sqlGetConnection();
        $db = $connection->getDatabase();

        return $this->ensureDbExists($db);
    }

    /** Create database */
    public function upgrade_DBExists()
    {
        $this->upgradeOK();
    }

    public function check_LoadDumpReview()
    {
        return file_exists('./review_dump.sql.bz2') && $this->checkTableExists('products');
    }

    /** Load database dump from review */
    public function upgrade_LoadDumpReview()
    {
        global $cfg;
        $command = sprintf(
            'bzcat %s | mysql -u %s -p%s -h%s %s',
            './review_dump.sql.bz2',
            $cfg['Connection']['user'],
            $cfg['Connection']['password'],
            $cfg['Connection']['host'],
            $cfg['Connection']['database']
        );
        exec($command, $output, $return);

        // Turn off CDN
        sqlQuery('UPDATE settings SET value = JSON_SET(value, "$.active", "N") WHERE key_name = "cdn"');

        $this->addNote("Import exit code: {$return}, output: {$output}");
    }

    public function check_SymlinkDumpReview()
    {
        return file_exists('./review_dump.sql.bz2');
    }

    /** Symlink database dump from review */
    public function upgrade_SymlinkDumpReview()
    {
        mkdir('./data/backup');
        symlink(realpath('./review_dump.sql.bz2'), './data/backup/review_dump.sql.bz2');
        $this->upgradeOK();
    }

    public function check_LoadUpstreamDump()
    {
        return isDevelopment() && $this->checkTableExists('products');
    }

    /** Load database dump using dump_database */
    public function upgrade_LoadUpstreamDump()
    {
        $command = 'AUTO_DUMP=0 dump_database.sh 2>&1';

        passthru($command, $return);

        $this->addNote("Import exit code: {$return}");
    }

    public function check_LoadDump()
    {
        if (isFunctionalTests()) {
            return false;
        }

        return $this->checkTableExists('products');
    }

    /** Load database dump */
    public function upgrade_LoadDump()
    {
        global $cfg;

        $command = sprintf(
            'engine/bin/import_demo.sh %s %s %s %s',
            $cfg['Connection']['user'],
            $cfg['Connection']['password'],
            $cfg['Connection']['host'],
            $cfg['Connection']['database']
        );

        exec($command, $output, $return);
        $output = join("\n", $output);

        $this->addNote("Import exit code: {$return}, output: {$output}");
    }

    public function check_ClearCache()
    {
        // Clearovat cache je třeba jen na local dev, na produkci se cache invaliduje použitím jinýho prefixu s BUILD_ID
        return isDevelopment();
    }

    /** Clear cache */
    public function upgrade_ClearCache()
    {
        clearCache('', true);

        global $dbcfg;
        try {
            $dbcfg = Settings::getDefault();
        } catch (Exception $e) {
        }

        $this->upgradeOK();
    }
}
