<?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\Ticket;

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

final class Version20250603085035 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Add a state to tickets';
    }

    public function up(Schema $schema): void
    {
        $this->addSql(<<<'SQL'
            CREATE SEQUENCE chill_ticket.state_history_id_seq INCREMENT BY 1 MINVALUE 1 START 1
        SQL);
        $this->addSql(<<<'SQL'
            CREATE TABLE chill_ticket.state_history (id INT NOT NULL, ticket_id INT NOT NULL,
            endDate TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, state VARCHAR(255) NOT NULL,
            startDate TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
            createdAt TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL,
            createdBy_id INT DEFAULT NULL, PRIMARY KEY(id))
        SQL);
        $this->addSql(<<<'SQL'
            CREATE INDEX IDX_B1DCD379700047D2 ON chill_ticket.state_history (ticket_id)
        SQL);
        $this->addSql(<<<'SQL'
            CREATE INDEX IDX_B1DCD3793174800F ON chill_ticket.state_history (createdBy_id)
        SQL);
        $this->addSql(<<<'SQL'
            COMMENT ON COLUMN chill_ticket.state_history.endDate IS '(DC2Type:datetime_immutable)'
        SQL);
        $this->addSql(<<<'SQL'
            COMMENT ON COLUMN chill_ticket.state_history.startDate IS '(DC2Type:datetime_immutable)'
        SQL);
        $this->addSql(<<<'SQL'
            COMMENT ON COLUMN chill_ticket.state_history.createdAt IS '(DC2Type:datetime_immutable)'
        SQL);
        $this->addSql(<<<'SQL'
            ALTER TABLE chill_ticket.state_history ADD CONSTRAINT FK_B1DCD379700047D2 FOREIGN KEY (ticket_id)
            REFERENCES chill_ticket.ticket (id) NOT DEFERRABLE INITIALLY IMMEDIATE
        SQL);
        $this->addSql(<<<'SQL'
            ALTER TABLE chill_ticket.state_history ADD CONSTRAINT FK_B1DCD3793174800F FOREIGN KEY (createdBy_id)
            REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE
        SQL);
        $this->addSql(<<<'SQL'
            ALTER TABLE chill_ticket.state_history ADD CONSTRAINT ticket_state_not_overlaps
            exclude using gist (ticket_id with =, tsrange(startdate, enddate) with &&)
            deferrable initially deferred
        SQL);
        $this->addSql(<<<'SQL'
            INSERT INTO chill_ticket.state_history  (id, ticket_id, state, startDate, createdAt, createdBy_id)
            SELECT nextval('chill_ticket.state_history_id_seq'), id, 'open', createdAt, createdAt, createdBy_id
            FROM chill_ticket.ticket
        SQL);
    }

    public function down(Schema $schema): void
    {
        $this->addSql(<<<'SQL'
            DROP SEQUENCE chill_ticket.state_history_id_seq CASCADE
        SQL);
        $this->addSql(<<<'SQL'
            ALTER TABLE chill_ticket.state_history DROP CONSTRAINT FK_B1DCD379700047D2
        SQL);
        $this->addSql(<<<'SQL'
            ALTER TABLE chill_ticket.state_history DROP CONSTRAINT FK_B1DCD3793174800F
        SQL);
        $this->addSql(<<<'SQL'
            ALTER TABLE chill_ticket.state_history DROP CONSTRAINT ticket_state_not_overlaps
        SQL);
        $this->addSql(<<<'SQL'
            DROP TABLE chill_ticket.state_history
        SQL);
    }
}
