<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20260507100000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Add token table for share and hard-delete flows';
    }

    public function up(Schema $schema): void
    {
        $this->addSql("CREATE TABLE token (
            uuid VARCHAR(36) NOT NULL,
            type VARCHAR(30) NOT NULL,
            image_id INT NOT NULL,
            recipient_user_id INT DEFAULT NULL,
            recipient_email VARCHAR(180) DEFAULT NULL,
            expires_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
            used_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL,
            PRIMARY KEY(uuid)
        )");
        $this->addSql("COMMENT ON COLUMN token.expires_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql("COMMENT ON COLUMN token.used_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql('ALTER TABLE token ADD CONSTRAINT fk_token_image FOREIGN KEY (image_id) REFERENCES image (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE token ADD CONSTRAINT fk_token_recipient FOREIGN KEY (recipient_user_id) REFERENCES "user" (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('CREATE INDEX idx_token_recipient ON token (recipient_user_id)');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE token DROP CONSTRAINT fk_token_image');
        $this->addSql('ALTER TABLE token DROP CONSTRAINT fk_token_recipient');
        $this->addSql('DROP TABLE token');
    }
}
