<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20260507200000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Add shared_image table for family sharing';
    }

    public function up(Schema $schema): void
    {
        $this->addSql("CREATE TABLE shared_image (
            id SERIAL NOT NULL,
            source_image_id INT NOT NULL,
            recipient_user_id INT NOT NULL,
            shared_by_id INT NOT NULL,
            shared_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL,
            status VARCHAR(20) NOT NULL DEFAULT 'pending',
            PRIMARY KEY(id)
        )");
        $this->addSql("COMMENT ON COLUMN shared_image.shared_at IS '(DC2Type:datetime_immutable)'");
        $this->addSql('CREATE INDEX idx_shared_recipient_status ON shared_image (recipient_user_id, status)');
        $this->addSql('ALTER TABLE shared_image ADD CONSTRAINT fk_shared_source FOREIGN KEY (source_image_id) REFERENCES image (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE shared_image ADD CONSTRAINT fk_shared_recipient FOREIGN KEY (recipient_user_id) REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE shared_image ADD CONSTRAINT fk_shared_by FOREIGN KEY (shared_by_id) REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE shared_image DROP CONSTRAINT fk_shared_source');
        $this->addSql('ALTER TABLE shared_image DROP CONSTRAINT fk_shared_recipient');
        $this->addSql('ALTER TABLE shared_image DROP CONSTRAINT fk_shared_by');
        $this->addSql('DROP TABLE shared_image');
    }
}
