<?php

declare(strict_types=1);

namespace KupShop\ReservationBundle\Resources\upgrade;

class ReservationUpgrade extends \UpgradeNew
{
    public function check_ReservationsTable(): bool
    {
        return $this->checkTableExists('reservations');
    }

    /** Create `reservations` table */
    public function upgrade_ReservationsTable(): void
    {
        sqlQuery('CREATE TABLE IF NOT EXISTS reservations (
              id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
              date_created DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
              id_user INT(11) UNSIGNED DEFAULT NULL,
              id_order INT(11) UNSIGNED DEFAULT NULL,
              id_product_reservation INT(11),
              id_product INT(11),
              id_variation INT(11) DEFAULT NULL,
              price DECIMAL(15,4) NOT NULL,
              tax FLOAT DEFAULT 0,
              email VARCHAR(100),
              name VARCHAR(50),
              surname VARCHAR(50),
              phone VARCHAR(20),
              data TEXT DEFAULT NULL,
              CONSTRAINT fk_reservations_id_user FOREIGN KEY (id_user) REFERENCES users (id) ON DELETE SET NULL ON UPDATE CASCADE,
              CONSTRAINT fk_reservations_id_order FOREIGN KEY (id_order) REFERENCES orders (id) ON DELETE SET NULL ON UPDATE CASCADE,
              CONSTRAINT fk_reservations_id_product_reservation FOREIGN KEY (id_product_reservation) REFERENCES product_reservations (id) ON DELETE SET NULL,
              CONSTRAINT fk_reservations_id_product FOREIGN KEY (id_product) REFERENCES products (id) ON DELETE SET NULL ON UPDATE CASCADE,
              CONSTRAINT fk_reservations_id_variation FOREIGN KEY (id_variation) REFERENCES products_variations (id) ON DELETE SET NULL ON UPDATE CASCADE
              ) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4;');

        // nastavim do databaze defaultni interval pro slucovani objednavek
        $dbcfg = \Settings::getDefault();
        $dbcfg->saveValue('reservations', ['merge_interval' => 20]);

        \Settings::clearCache();

        $this->upgradeOK();
    }

    public function check_ReservationsIdLanguageColumn(): bool
    {
        if (findModule(\Modules::CURRENCIES)) {
            return $this->checkColumnExists('reservations', 'id_language');
        }

        return false;
    }

    /** Add reservations.id_language, reservations.id_currency columns */
    public function upgrade_ReservationsIdLanguageColumn(): void
    {
        sqlQuery('ALTER TABLE reservations ADD COLUMN id_language VARCHAR(2) AFTER id');
        sqlQuery('ALTER TABLE reservations ADD CONSTRAINT fk_reservations_id_language FOREIGN KEY (id_language) REFERENCES languages(id) ON DELETE SET NULL ON UPDATE CASCADE');

        sqlQuery('ALTER TABLE reservations ADD COLUMN currency VARCHAR(3) AFTER id_language');
        sqlQuery('ALTER TABLE reservations ADD CONSTRAINT fk_reservations_currency FOREIGN KEY (currency) REFERENCES currencies(id) ON DELETE SET NULL ON UPDATE CASCADE');

        $this->upgradeOK();
    }
}
