<?php

namespace KupShop\CommentsBundle\Resources\upgrade;

class CommentsUpgrade extends \UpgradeNew
{
    public function check_CommentsTable()
    {
        return $this->checkTableExists('comments');
    }

    /** Create 'comments' table */
    public function upgrade_CommentsTable()
    {
        sqlQuery('CREATE TABLE IF NOT EXISTS comments (
            id INT(11) AUTO_INCREMENT PRIMARY KEY,
            id_language VARCHAR(2) DEFAULT NULL,
            id_parent INT(11) DEFAULT NULL,
            id_product INT(11) DEFAULT NULL,
            id_article INT(11) DEFAULT NULL,
            id_user INT(11) UNSIGNED DEFAULT NULL,
            id_admin INT(11) DEFAULT NULL,
            content MEDIUMTEXT NOT NULL,
            status TINYINT(1) NOT NULL DEFAULT 0,
            solved TINYINT(1) NOT NULL DEFAULT 0,
            date_add DATETIME DEFAULT CURRENT_TIMESTAMP,
            FOREIGN KEY (id_parent) REFERENCES comments(id) ON DELETE CASCADE ON UPDATE CASCADE, 
            FOREIGN KEY (id_product) REFERENCES products(id) ON DELETE CASCADE ON UPDATE CASCADE, 
            FOREIGN KEY (id_article) REFERENCES articles(id) ON DELETE CASCADE ON UPDATE CASCADE, 
            FOREIGN KEY (id_user) REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE, 
            FOREIGN KEY (id_admin) REFERENCES admins(id) ON DELETE CASCADE ON UPDATE CASCADE 
        );');

        $this->upgradeOK();
    }

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

    /** Add 'data' column to 'comments' table */
    public function upgrade_dataColumn()
    {
        sqlQuery('ALTER TABLE comments ADD COLUMN data MEDIUMTEXT DEFAULT NULL');

        $this->upgradeOK();
    }
}
