<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\Migrations\Main;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20250410145342 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'share saved_exports with users and groups';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('CREATE TABLE chill_main_saved_export_users (savedexport_id UUID NOT NULL, user_id INT NOT NULL, PRIMARY KEY(savedexport_id, user_id))');
        $this->addSql('CREATE INDEX IDX_4A2B71EC24ECEDCA ON chill_main_saved_export_users (savedexport_id)');
        $this->addSql('CREATE INDEX IDX_4A2B71ECA76ED395 ON chill_main_saved_export_users (user_id)');
        $this->addSql('COMMENT ON COLUMN chill_main_saved_export_users.savedexport_id IS \'(DC2Type:uuid)\'');
        $this->addSql('CREATE TABLE chill_main_saved_export_usergroups (savedexport_id UUID NOT NULL, usergroup_id INT NOT NULL, PRIMARY KEY(savedexport_id, usergroup_id))');
        $this->addSql('CREATE INDEX IDX_A12F30824ECEDCA ON chill_main_saved_export_usergroups (savedexport_id)');
        $this->addSql('CREATE INDEX IDX_A12F308D2112630 ON chill_main_saved_export_usergroups (usergroup_id)');
        $this->addSql('COMMENT ON COLUMN chill_main_saved_export_usergroups.savedexport_id IS \'(DC2Type:uuid)\'');
        $this->addSql('ALTER TABLE chill_main_saved_export_users ADD CONSTRAINT FK_4A2B71EC24ECEDCA FOREIGN KEY (savedexport_id) REFERENCES chill_main_saved_export (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE chill_main_saved_export_users ADD CONSTRAINT FK_4A2B71ECA76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE chill_main_saved_export_usergroups ADD CONSTRAINT FK_A12F30824ECEDCA FOREIGN KEY (savedexport_id) REFERENCES chill_main_saved_export (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE chill_main_saved_export_usergroups ADD CONSTRAINT FK_A12F308D2112630 FOREIGN KEY (usergroup_id) REFERENCES chill_main_user_group (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE chill_main_saved_export_users DROP CONSTRAINT FK_4A2B71EC24ECEDCA');
        $this->addSql('ALTER TABLE chill_main_saved_export_users DROP CONSTRAINT FK_4A2B71ECA76ED395');
        $this->addSql('ALTER TABLE chill_main_saved_export_usergroups DROP CONSTRAINT FK_A12F30824ECEDCA');
        $this->addSql('ALTER TABLE chill_main_saved_export_usergroups DROP CONSTRAINT FK_A12F308D2112630');
        $this->addSql('DROP TABLE chill_main_saved_export_users');
        $this->addSql('DROP TABLE chill_main_saved_export_usergroups');
    }
}
