<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Suppression de la feature « portail client » : table client_ticket,
 * table user_allowed_projects, colonne user.client_id, et toutes les
 * relations vers client_ticket (task, task_document, time_entry, notification).
 */
final class Version20260522110000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Drop client portal feature (client_ticket, user_allowed_projects, related columns)';
    }

    public function up(Schema $schema): void
    {
        // time_entry → client_ticket
        $this->addSql('ALTER TABLE time_entry DROP CONSTRAINT IF EXISTS FK_6E537C0C9B2097DD');
        $this->addSql('DROP INDEX IF EXISTS IDX_6E537C0C9B2097DD');
        $this->addSql('ALTER TABLE time_entry DROP COLUMN IF EXISTS client_ticket_id');

        // task → client_ticket
        $this->addSql('ALTER TABLE task DROP CONSTRAINT IF EXISTS FK_527EDB259B2097DD');
        $this->addSql('DROP INDEX IF EXISTS IDX_527EDB259B2097DD');
        $this->addSql('ALTER TABLE task DROP COLUMN IF EXISTS client_ticket_id');

        // notification → client_ticket
        $this->addSql('ALTER TABLE notification DROP CONSTRAINT IF EXISTS FK_BF5476CAD8C11BC9');
        $this->addSql('DROP INDEX IF EXISTS IDX_BF5476CAD8C11BC9');
        $this->addSql('ALTER TABLE notification DROP COLUMN IF EXISTS related_ticket_id');

        // task_document → client_ticket : les documents rattachés uniquement à un
        // ticket (sans tâche) disparaissent avec la feature.
        $this->addSql('ALTER TABLE task_document DROP CONSTRAINT IF EXISTS FK_98A9603A9B2097DD');
        $this->addSql('DROP INDEX IF EXISTS IDX_98A9603A9B2097DD');
        $this->addSql('ALTER TABLE task_document DROP CONSTRAINT IF EXISTS chk_document_owner');
        $this->addSql('DELETE FROM task_document WHERE task_id IS NULL');
        $this->addSql('ALTER TABLE task_document DROP COLUMN IF EXISTS client_ticket_id');
        $this->addSql('ALTER TABLE task_document ALTER COLUMN task_id SET NOT NULL');

        // user → client
        $this->addSql('ALTER TABLE "user" DROP CONSTRAINT IF EXISTS FK_8D93D64919EB6921');
        $this->addSql('DROP INDEX IF EXISTS IDX_8D93D64919EB6921');
        $this->addSql('ALTER TABLE "user" DROP COLUMN IF EXISTS client_id');

        // tables dédiées au portail
        $this->addSql('DROP TABLE IF EXISTS user_allowed_projects');
        $this->addSql('DROP TABLE IF EXISTS client_ticket');
    }

    public function down(Schema $schema): void
    {
        // Recreate client_ticket
        $this->addSql('CREATE TABLE client_ticket (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, number INT NOT NULL, type VARCHAR(20) NOT NULL, title VARCHAR(255) NOT NULL, description TEXT NOT NULL, url VARCHAR(255) DEFAULT NULL, status VARCHAR(20) NOT NULL, status_comment TEXT DEFAULT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, project_id INT NOT NULL, submitted_by_id INT DEFAULT NULL, PRIMARY KEY (id))');
        $this->addSql('CREATE INDEX IDX_C206E610166D1F9C ON client_ticket (project_id)');
        $this->addSql('CREATE INDEX IDX_C206E61079F7D87D ON client_ticket (submitted_by_id)');
        $this->addSql('CREATE UNIQUE INDEX uniq_client_ticket_project_number ON client_ticket (project_id, number)');
        $this->addSql('ALTER TABLE client_ticket ADD CONSTRAINT FK_C206E610166D1F9C FOREIGN KEY (project_id) REFERENCES project (id) ON DELETE CASCADE NOT DEFERRABLE');
        $this->addSql('ALTER TABLE client_ticket ADD CONSTRAINT FK_C206E61079F7D87D FOREIGN KEY (submitted_by_id) REFERENCES "user" (id) ON DELETE SET NULL NOT DEFERRABLE');

        // Recreate user_allowed_projects
        $this->addSql('CREATE TABLE user_allowed_projects (user_id INT NOT NULL, project_id INT NOT NULL, PRIMARY KEY (user_id, project_id))');
        $this->addSql('CREATE INDEX IDX_B3E0FC97A76ED395 ON user_allowed_projects (user_id)');
        $this->addSql('CREATE INDEX IDX_B3E0FC97166D1F9C ON user_allowed_projects (project_id)');
        $this->addSql('ALTER TABLE user_allowed_projects ADD CONSTRAINT FK_B3E0FC97A76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE user_allowed_projects ADD CONSTRAINT FK_B3E0FC97166D1F9C FOREIGN KEY (project_id) REFERENCES project (id) ON DELETE CASCADE');

        // user.client_id
        $this->addSql('ALTER TABLE "user" ADD client_id INT DEFAULT NULL');
        $this->addSql('ALTER TABLE "user" ADD CONSTRAINT FK_8D93D64919EB6921 FOREIGN KEY (client_id) REFERENCES client (id) ON DELETE SET NULL NOT DEFERRABLE');
        $this->addSql('CREATE INDEX IDX_8D93D64919EB6921 ON "user" (client_id)');

        // task_document.client_ticket_id
        $this->addSql('ALTER TABLE task_document ALTER COLUMN task_id DROP NOT NULL');
        $this->addSql('ALTER TABLE task_document ADD client_ticket_id INT DEFAULT NULL');
        $this->addSql('ALTER TABLE task_document ADD CONSTRAINT FK_98A9603A9B2097DD FOREIGN KEY (client_ticket_id) REFERENCES client_ticket (id) ON DELETE CASCADE NOT DEFERRABLE');
        $this->addSql('CREATE INDEX IDX_98A9603A9B2097DD ON task_document (client_ticket_id)');
        $this->addSql('ALTER TABLE task_document ADD CONSTRAINT chk_document_owner CHECK (task_id IS NOT NULL OR client_ticket_id IS NOT NULL)');

        // notification.related_ticket_id
        $this->addSql('ALTER TABLE notification ADD related_ticket_id INT DEFAULT NULL');
        $this->addSql('ALTER TABLE notification ADD CONSTRAINT FK_BF5476CAD8C11BC9 FOREIGN KEY (related_ticket_id) REFERENCES client_ticket (id) ON DELETE SET NULL NOT DEFERRABLE');
        $this->addSql('CREATE INDEX IDX_BF5476CAD8C11BC9 ON notification (related_ticket_id)');

        // task.client_ticket_id
        $this->addSql('ALTER TABLE task ADD client_ticket_id INT DEFAULT NULL');
        $this->addSql('ALTER TABLE task ADD CONSTRAINT FK_527EDB259B2097DD FOREIGN KEY (client_ticket_id) REFERENCES client_ticket (id) ON DELETE SET NULL NOT DEFERRABLE');
        $this->addSql('CREATE INDEX IDX_527EDB259B2097DD ON task (client_ticket_id)');

        // time_entry.client_ticket_id
        $this->addSql('ALTER TABLE time_entry ADD client_ticket_id INT DEFAULT NULL');
        $this->addSql('ALTER TABLE time_entry ADD CONSTRAINT FK_6E537C0C9B2097DD FOREIGN KEY (client_ticket_id) REFERENCES client_ticket (id) ON DELETE SET NULL NOT DEFERRABLE');
        $this->addSql('CREATE INDEX IDX_6E537C0C9B2097DD ON time_entry (client_ticket_id)');
    }
}
