<?php

declare(strict_types=1);

namespace KupShop\AgeVerifyBundle\Resources\upgrade;

use KupShop\AgeVerifyBundle\Utils\AgeVerifyUtil;

class AgeVerifyUpgrade extends \UpgradeNew
{
    public function check_userAgeVerification()
    {
        return $this->checkTableExists('users_age_verification');
    }

    /** Create age verification table */
    public function upgrade_userAgeVerification()
    {
        sqlQuery("create table users_age_verification(
            id_user int(11) unsigned primary key references users(id) on UPDATE cascade on delete CASCADE ,
            legal_age ENUM('Y', 'N') NOT NULL DEFAULT 'N',
            date_updated DATETIME ON UPDATE NOW() default NOW(),
            verification_type ENUM('bankId'));
        ");

        $this->upgradeOK();
    }

    public function check_verificationSources()
    {
        return $this->checkEnumOptions('users_age_verification', 'verification_type', array_keys(AgeVerifyUtil::$VERIFICATION_TYPES));
    }

    /** Update verification sources ENUM */
    public function upgrade_verificationSources()
    {
        $this->updateEnumOptions('users_age_verification', 'verification_type', array_keys(AgeVerifyUtil::$VERIFICATION_TYPES));

        $this->upgradeOK();
    }

    public function check_orderIdField()
    {
        return $this->checkColumnExists('users_age_verification', 'id_order');
    }

    /** Create id_order field on age verification */
    public function upgrade_orderIdField()
    {
        sqlQuery('ALTER TABLE users_age_verification ADD COLUMN id_order INT UNSIGNED NULL
            REFERENCES orders(id) ON UPDATE CASCADE ON DELETE CASCADE;');

        $this->upgradeOK();
    }

    public function check_ageVerificationData()
    {
        return $this->checkColumnExists('users_age_verification', 'data');
    }

    /** Add data field to users_age_verification */
    public function upgrade_ageVerificationData()
    {
        sqlQuery('ALTER TABLE users_age_verification ADD COLUMN data LONGTEXT NULL');

        $this->upgradeOK();
    }
}
