<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * ProjectManagement module (LST-65 slice 3): add Timestampable/Blamable columns
 * to task and project.
 *
 * Task and Project adopt TimestampableBlamableTrait (after TimeEntry). This
 * migration is purely additive — nullable columns + nullable FK to "user" with
 * ON DELETE SET NULL. No DROP/ALTER on existing data. Columns are lowercase
 * snake_case. Hand-written to guarantee zero destructive instruction.
 */
final class Version20260620161500 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'ProjectManagement: add timestampable/blamable columns to task and project (additive)';
    }

    public function up(Schema $schema): void
    {
        // task
        $this->addSql('ALTER TABLE task ADD created_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL');
        $this->addSql('ALTER TABLE task ADD updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL');
        $this->addSql('ALTER TABLE task ADD created_by INT DEFAULT NULL');
        $this->addSql('ALTER TABLE task ADD updated_by INT DEFAULT NULL');
        $this->addSql('ALTER TABLE task ADD CONSTRAINT FK_527EDB25DE12AB56 FOREIGN KEY (created_by) REFERENCES "user" (id) ON DELETE SET NULL NOT DEFERRABLE');
        $this->addSql('ALTER TABLE task ADD CONSTRAINT FK_527EDB2516FE72E1 FOREIGN KEY (updated_by) REFERENCES "user" (id) ON DELETE SET NULL NOT DEFERRABLE');
        $this->addSql('CREATE INDEX IDX_527EDB25DE12AB56 ON task (created_by)');
        $this->addSql('CREATE INDEX IDX_527EDB2516FE72E1 ON task (updated_by)');
        $this->addSql("COMMENT ON COLUMN task.created_at IS 'Creation timestamp (Timestampable, set on prePersist)'");
        $this->addSql("COMMENT ON COLUMN task.updated_at IS 'Last update timestamp (Timestampable, set on prePersist/preUpdate)'");
        $this->addSql("COMMENT ON COLUMN task.created_by IS 'User who created the entry (Blamable, FK user.id, SET NULL on delete)'");
        $this->addSql("COMMENT ON COLUMN task.updated_by IS 'User who last updated the entry (Blamable, FK user.id, SET NULL on delete)'");

        // project
        $this->addSql('ALTER TABLE project ADD created_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL');
        $this->addSql('ALTER TABLE project ADD updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL');
        $this->addSql('ALTER TABLE project ADD created_by INT DEFAULT NULL');
        $this->addSql('ALTER TABLE project ADD updated_by INT DEFAULT NULL');
        $this->addSql('ALTER TABLE project ADD CONSTRAINT FK_2FB3D0EEDE12AB56 FOREIGN KEY (created_by) REFERENCES "user" (id) ON DELETE SET NULL NOT DEFERRABLE');
        $this->addSql('ALTER TABLE project ADD CONSTRAINT FK_2FB3D0EE16FE72E1 FOREIGN KEY (updated_by) REFERENCES "user" (id) ON DELETE SET NULL NOT DEFERRABLE');
        $this->addSql('CREATE INDEX IDX_2FB3D0EEDE12AB56 ON project (created_by)');
        $this->addSql('CREATE INDEX IDX_2FB3D0EE16FE72E1 ON project (updated_by)');
        $this->addSql("COMMENT ON COLUMN project.created_at IS 'Creation timestamp (Timestampable, set on prePersist)'");
        $this->addSql("COMMENT ON COLUMN project.updated_at IS 'Last update timestamp (Timestampable, set on prePersist/preUpdate)'");
        $this->addSql("COMMENT ON COLUMN project.created_by IS 'User who created the entry (Blamable, FK user.id, SET NULL on delete)'");
        $this->addSql("COMMENT ON COLUMN project.updated_by IS 'User who last updated the entry (Blamable, FK user.id, SET NULL on delete)'");
    }

    public function down(Schema $schema): void
    {
        // task
        $this->addSql('ALTER TABLE task DROP CONSTRAINT FK_527EDB25DE12AB56');
        $this->addSql('ALTER TABLE task DROP CONSTRAINT FK_527EDB2516FE72E1');
        $this->addSql('DROP INDEX IDX_527EDB25DE12AB56');
        $this->addSql('DROP INDEX IDX_527EDB2516FE72E1');
        $this->addSql('ALTER TABLE task DROP created_at');
        $this->addSql('ALTER TABLE task DROP updated_at');
        $this->addSql('ALTER TABLE task DROP created_by');
        $this->addSql('ALTER TABLE task DROP updated_by');

        // project
        $this->addSql('ALTER TABLE project DROP CONSTRAINT FK_2FB3D0EEDE12AB56');
        $this->addSql('ALTER TABLE project DROP CONSTRAINT FK_2FB3D0EE16FE72E1');
        $this->addSql('DROP INDEX IDX_2FB3D0EEDE12AB56');
        $this->addSql('DROP INDEX IDX_2FB3D0EE16FE72E1');
        $this->addSql('ALTER TABLE project DROP created_at');
        $this->addSql('ALTER TABLE project DROP updated_at');
        $this->addSql('ALTER TABLE project DROP created_by');
        $this->addSql('ALTER TABLE project DROP updated_by');
    }
}
