<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Auto-generated Migration: Please modify to your needs!
 */
final class Version20260624153709 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Directory: prestataire entity + website on client/prospect/prestataire + prestataire ownership on contacts/addresses/reports + prospect company-only';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('CREATE TABLE prestataire (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, name VARCHAR(255) NOT NULL, email VARCHAR(255) DEFAULT NULL, phone VARCHAR(50) DEFAULT NULL, website VARCHAR(255) DEFAULT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, updated_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, created_by INT DEFAULT NULL, updated_by INT DEFAULT NULL, PRIMARY KEY (id))');
        $this->addSql('CREATE INDEX IDX_60A26480DE12AB56 ON prestataire (created_by)');
        $this->addSql('CREATE INDEX IDX_60A2648016FE72E1 ON prestataire (updated_by)');
        $this->addSql('ALTER TABLE prestataire ADD CONSTRAINT FK_60A26480DE12AB56 FOREIGN KEY (created_by) REFERENCES "user" (id) ON DELETE SET NULL NOT DEFERRABLE');
        $this->addSql('ALTER TABLE prestataire ADD CONSTRAINT FK_60A2648016FE72E1 FOREIGN KEY (updated_by) REFERENCES "user" (id) ON DELETE SET NULL NOT DEFERRABLE');
        $this->addSql('ALTER TABLE client ADD website VARCHAR(255) DEFAULT NULL');
        $this->addSql('ALTER TABLE commercial_report ADD prestataire_id INT DEFAULT NULL');
        $this->addSql('ALTER TABLE commercial_report ADD CONSTRAINT FK_886919D8BE3DB2B7 FOREIGN KEY (prestataire_id) REFERENCES prestataire (id) ON DELETE CASCADE NOT DEFERRABLE');
        $this->addSql('CREATE INDEX IDX_886919D8BE3DB2B7 ON commercial_report (prestataire_id)');
        $this->addSql('ALTER TABLE directory_address ADD prestataire_id INT DEFAULT NULL');
        $this->addSql('ALTER TABLE directory_address ADD CONSTRAINT FK_6E5D9707BE3DB2B7 FOREIGN KEY (prestataire_id) REFERENCES prestataire (id) ON DELETE CASCADE NOT DEFERRABLE');
        $this->addSql('CREATE INDEX IDX_6E5D9707BE3DB2B7 ON directory_address (prestataire_id)');
        $this->addSql('ALTER TABLE directory_contact ADD prestataire_id INT DEFAULT NULL');
        $this->addSql('ALTER TABLE directory_contact ADD CONSTRAINT FK_2F711EBEBE3DB2B7 FOREIGN KEY (prestataire_id) REFERENCES prestataire (id) ON DELETE CASCADE NOT DEFERRABLE');
        $this->addSql('CREATE INDEX IDX_2F711EBEBE3DB2B7 ON directory_contact (prestataire_id)');

        // Prospect désormais société-only : on conserve la donnée existante en
        // recopiant le nom dans la société quand celle-ci est vide, avant de
        // rendre la colonne obligatoire et de supprimer la colonne name.
        $this->addSql('ALTER TABLE prospect ADD website VARCHAR(255) DEFAULT NULL');
        $this->addSql("UPDATE prospect SET company = name WHERE company IS NULL OR company = ''");
        $this->addSql('ALTER TABLE prospect ALTER company SET NOT NULL');
        $this->addSql('ALTER TABLE prospect DROP name');

        // Ownership CHECK constraints: chaque ligne appartient à un client,
        // un prospect OU un prestataire.
        $this->addSql('ALTER TABLE directory_contact DROP CONSTRAINT chk_contact_owner');
        $this->addSql('ALTER TABLE directory_contact ADD CONSTRAINT chk_contact_owner CHECK (client_id IS NOT NULL OR prospect_id IS NOT NULL OR prestataire_id IS NOT NULL)');
        $this->addSql('ALTER TABLE directory_address DROP CONSTRAINT chk_address_owner');
        $this->addSql('ALTER TABLE directory_address ADD CONSTRAINT chk_address_owner CHECK (client_id IS NOT NULL OR prospect_id IS NOT NULL OR prestataire_id IS NOT NULL)');
        $this->addSql('ALTER TABLE commercial_report DROP CONSTRAINT chk_report_owner');
        $this->addSql('ALTER TABLE commercial_report ADD CONSTRAINT chk_report_owner CHECK (client_id IS NOT NULL OR prospect_id IS NOT NULL OR prestataire_id IS NOT NULL)');
    }

    public function down(Schema $schema): void
    {
        // Rétablit les contraintes d'ownership client/prospect (sans prestataire).
        $this->addSql('ALTER TABLE directory_contact DROP CONSTRAINT chk_contact_owner');
        $this->addSql('ALTER TABLE directory_address DROP CONSTRAINT chk_address_owner');
        $this->addSql('ALTER TABLE commercial_report DROP CONSTRAINT chk_report_owner');

        $this->addSql('ALTER TABLE commercial_report DROP CONSTRAINT FK_886919D8BE3DB2B7');
        $this->addSql('DROP INDEX IDX_886919D8BE3DB2B7');
        $this->addSql('ALTER TABLE commercial_report DROP prestataire_id');
        $this->addSql('ALTER TABLE directory_address DROP CONSTRAINT FK_6E5D9707BE3DB2B7');
        $this->addSql('DROP INDEX IDX_6E5D9707BE3DB2B7');
        $this->addSql('ALTER TABLE directory_address DROP prestataire_id');
        $this->addSql('ALTER TABLE directory_contact DROP CONSTRAINT FK_2F711EBEBE3DB2B7');
        $this->addSql('DROP INDEX IDX_2F711EBEBE3DB2B7');
        $this->addSql('ALTER TABLE directory_contact DROP prestataire_id');

        $this->addSql('ALTER TABLE directory_contact ADD CONSTRAINT chk_contact_owner CHECK (client_id IS NOT NULL OR prospect_id IS NOT NULL)');
        $this->addSql('ALTER TABLE directory_address ADD CONSTRAINT chk_address_owner CHECK (client_id IS NOT NULL OR prospect_id IS NOT NULL)');
        $this->addSql('ALTER TABLE commercial_report ADD CONSTRAINT chk_report_owner CHECK (client_id IS NOT NULL OR prospect_id IS NOT NULL)');

        $this->addSql('ALTER TABLE prestataire DROP CONSTRAINT FK_60A26480DE12AB56');
        $this->addSql('ALTER TABLE prestataire DROP CONSTRAINT FK_60A2648016FE72E1');
        $this->addSql('DROP TABLE prestataire');
        $this->addSql('ALTER TABLE client DROP website');

        // Restaure la colonne name (recopiée depuis company) puis l'oblige.
        $this->addSql('ALTER TABLE prospect ADD name VARCHAR(255) DEFAULT NULL');
        $this->addSql('UPDATE prospect SET name = company');
        $this->addSql('ALTER TABLE prospect ALTER name SET NOT NULL');
        $this->addSql('ALTER TABLE prospect DROP website');
        $this->addSql('ALTER TABLE prospect ALTER company DROP NOT NULL');
    }
}
