<?php

declare(strict_types=1);

namespace KupShop\SalesBundle\Resources\upgrade;

use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Util\Contexts;

class SalesUpgrade extends \UpgradeNew
{
    public function check_SalesTable(): bool
    {
        return $this->checkTableExists('sales');
    }

    /** Create `sales` and `sale_items` tables */
    public function upgrade_SalesTable(): void
    {
        sqlQuery('CREATE TABLE IF NOT EXISTS sales (
            id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
            code VARCHAR(20) NOT NULL UNIQUE KEY,
            date_created DATETIME DEFAULT CURRENT_TIMESTAMP(),
            id_user INT(11) UNSIGNED DEFAULT NULL,
            id_order INT(11) UNSIGNED DEFAULT NULL,
            id_delivery_type INT(11) UNSIGNED DEFAULT NULL,
            total_price DECIMAL(15,4) DEFAULT 0,
            total_price_without_vat DECIMAL(15,4) DEFAULT 0,
            note MEDIUMTEXT DEFAULT NULL,
            data MEDIUMTEXT DEFAULT NULL,
            CONSTRAINT FK_sales_id_user FOREIGN KEY (id_user) REFERENCES users (id) ON UPDATE CASCADE ON DELETE CASCADE,
            CONSTRAINT FK_sales_id_order FOREIGN KEY (id_order) REFERENCES orders (id) ON UPDATE CASCADE ON DELETE CASCADE,
            CONSTRAINT FK_sales_id_delivery_type FOREIGN KEY (id_delivery_type) REFERENCES delivery_type (id) ON UPDATE CASCADE ON DELETE CASCADE
        )');

        sqlQuery('CREATE TABLE IF NOT EXISTS sales_items (
            id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
            id_sale INT(11) UNSIGNED NOT NULL,
            id_product INT(11) DEFAULT NULL,
            id_variation INT(11) DEFAULT NULL,
            pieces MEDIUMINT DEFAULT 1,
            piece_price DECIMAL(15,4) DEFAULT 0,
            total_price DECIMAL(15,4) DEFAULT 0,
            tax FLOAT DEFAULT 0,
            name VARCHAR(255) NOT NULL,
            data MEDIUMTEXT DEFAULT NULL,
            CONSTRAINT FK_sale_items_id_sale FOREIGN KEY (id_sale) REFERENCES sales (id) ON UPDATE CASCADE ON DELETE CASCADE,
            CONSTRAINT FK_sale_items_id_product FOREIGN KEY (id_product) REFERENCES products (id) ON UPDATE CASCADE ON DELETE CASCADE,
            CONSTRAINT FK_sale_items_id_variation FOREIGN KEY (id_variation) REFERENCES products_variations (id) ON UPDATE CASCADE ON DELETE CASCADE
        )');

        $this->upgradeOK();
    }

    public function check_LanguageColumn(): bool
    {
        return findModule(\Modules::TRANSLATIONS) && $this->checkColumnExists('sales', 'id_language');
    }

    /** Add `sales.id_language` column */
    public function upgrade_LanguageColumn(): void
    {
        sqlQuery('ALTER TABLE sales ADD COLUMN id_language VARCHAR(2) NOT NULL DEFAULT "'.Contexts::get(LanguageContext::class)->getDefaultId().'" AFTER id');
        sqlQuery('ALTER TABLE sales ADD CONSTRAINT FK_sales_id_language FOREIGN KEY (id_language) REFERENCES languages (id) ON UPDATE CASCADE ON DELETE CASCADE');

        $this->upgradeOK();
    }

    public function check_CurrencyColumn(): bool
    {
        return $this->checkColumnExists('sales', 'currency');
    }

    /** Add `sales.currency` column */
    public function upgrade_CurrencyColumn(): void
    {
        sqlQuery('ALTER TABLE sales ADD COLUMN currency VARCHAR(3) NOT NULL DEFAULT "'.Contexts::get(CurrencyContext::class)->getDefaultId().'" AFTER id');

        $this->upgradeOK();
    }
}
