<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20260321180000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Create billet_buyer and billet_buyer_item tables';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('CREATE TABLE billet_buyer (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, event_id INT NOT NULL, user_id INT DEFAULT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, reference VARCHAR(23) NOT NULL, total_ht INT NOT NULL, status VARCHAR(20) NOT NULL, stripe_session_id VARCHAR(255) DEFAULT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, paid_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE UNIQUE INDEX UNIQ_BILLET_BUYER_REF ON billet_buyer (reference)');
        $this->addSql('CREATE INDEX IDX_BILLET_BUYER_EVENT ON billet_buyer (event_id)');
        $this->addSql('CREATE INDEX IDX_BILLET_BUYER_USER ON billet_buyer (user_id)');
        $this->addSql('ALTER TABLE billet_buyer ADD CONSTRAINT FK_BILLET_BUYER_EVENT FOREIGN KEY (event_id) REFERENCES event (id) NOT DEFERRABLE');
        $this->addSql('ALTER TABLE billet_buyer ADD CONSTRAINT FK_BILLET_BUYER_USER FOREIGN KEY (user_id) REFERENCES "user" (id) NOT DEFERRABLE');

        $this->addSql('CREATE TABLE billet_buyer_item (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, billet_buyer_id INT NOT NULL, billet_id INT NOT NULL, quantity INT NOT NULL, unit_price_ht INT NOT NULL, billet_name VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE INDEX IDX_BBI_BUYER ON billet_buyer_item (billet_buyer_id)');
        $this->addSql('CREATE INDEX IDX_BBI_BILLET ON billet_buyer_item (billet_id)');
        $this->addSql('ALTER TABLE billet_buyer_item ADD CONSTRAINT FK_BBI_BUYER FOREIGN KEY (billet_buyer_id) REFERENCES billet_buyer (id) ON DELETE CASCADE NOT DEFERRABLE');
        $this->addSql('ALTER TABLE billet_buyer_item ADD CONSTRAINT FK_BBI_BILLET FOREIGN KEY (billet_id) REFERENCES billet (id) NOT DEFERRABLE');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE billet_buyer_item DROP CONSTRAINT FK_BBI_BUYER');
        $this->addSql('ALTER TABLE billet_buyer_item DROP CONSTRAINT FK_BBI_BILLET');
        $this->addSql('DROP TABLE billet_buyer_item');
        $this->addSql('ALTER TABLE billet_buyer DROP CONSTRAINT FK_BILLET_BUYER_EVENT');
        $this->addSql('ALTER TABLE billet_buyer DROP CONSTRAINT FK_BILLET_BUYER_USER');
        $this->addSql('DROP TABLE billet_buyer');
    }
}
