<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20260427003000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Ajoute image hero, sections, niveaux, années scolaires et classes pour les madrassas.';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('ALTER TABLE ecoles ADD hero_image VARCHAR(255) DEFAULT NULL');

        $this->addSql('CREATE TABLE sections (id INT AUTO_INCREMENT NOT NULL, ecole_id INT NOT NULL, nom VARCHAR(160) NOT NULL, slug VARCHAR(160) NOT NULL, description LONGTEXT DEFAULT NULL, type VARCHAR(40) NOT NULL, public_cible VARCHAR(160) DEFAULT NULL, active TINYINT(1) DEFAULT 1 NOT NULL, ordre INT DEFAULT 0 NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', updated_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_2D737AE2716F1E3 (ecole_id), UNIQUE INDEX UNIQ_SECTION_ECOLE_SLUG (ecole_id, slug), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
        $this->addSql('CREATE TABLE niveaux (id INT AUTO_INCREMENT NOT NULL, ecole_id INT NOT NULL, section_id INT DEFAULT NULL, nom VARCHAR(160) NOT NULL, slug VARCHAR(160) NOT NULL, description LONGTEXT DEFAULT NULL, ordre INT DEFAULT 0 NOT NULL, active TINYINT(1) DEFAULT 1 NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', updated_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_4E82760B716F1E3 (ecole_id), INDEX IDX_4E82760BD823E37A (section_id), UNIQUE INDEX UNIQ_NIVEAU_ECOLE_SLUG (ecole_id, slug), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
        $this->addSql('CREATE TABLE annees_scolaires (id INT AUTO_INCREMENT NOT NULL, ecole_id INT NOT NULL, section_id INT DEFAULT NULL, libelle VARCHAR(160) NOT NULL, date_debut DATE DEFAULT NULL, date_fin DATE DEFAULT NULL, active TINYINT(1) DEFAULT 1 NOT NULL, statut VARCHAR(30) DEFAULT \'ACTIVE\' NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', updated_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_4C3DBF6B716F1E3 (ecole_id), INDEX IDX_4C3DBF6BD823E37A (section_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
        $this->addSql('CREATE TABLE classes (id INT AUTO_INCREMENT NOT NULL, ecole_id INT NOT NULL, section_id INT NOT NULL, niveau_id INT DEFAULT NULL, annee_scolaire_id INT DEFAULT NULL, nom VARCHAR(160) NOT NULL, slug VARCHAR(160) NOT NULL, langue VARCHAR(40) DEFAULT NULL, sexe VARCHAR(20) DEFAULT NULL, mode VARCHAR(40) DEFAULT NULL, capacite INT DEFAULT NULL, jours_cours LONGTEXT DEFAULT NULL, heure_debut TIME DEFAULT NULL, heure_fin TIME DEFAULT NULL, tarif_defaut NUMERIC(10, 2) DEFAULT NULL, active TINYINT(1) DEFAULT 1 NOT NULL, created_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', updated_at DATETIME NOT NULL COMMENT \'(DC2Type:datetime_immutable)\', INDEX IDX_8F87BF96716F1E3 (ecole_id), INDEX IDX_8F87BF96D823E37A (section_id), INDEX IDX_8F87BF96B3E9C81 (niveau_id), INDEX IDX_8F87BF96E0C82FDD (annee_scolaire_id), UNIQUE INDEX UNIQ_CLASSE_ECOLE_SLUG (ecole_id, slug), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');

        $this->addSql('ALTER TABLE sections ADD CONSTRAINT FK_2D737AE2716F1E3 FOREIGN KEY (ecole_id) REFERENCES ecoles (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE niveaux ADD CONSTRAINT FK_4E82760B716F1E3 FOREIGN KEY (ecole_id) REFERENCES ecoles (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE niveaux ADD CONSTRAINT FK_4E82760BD823E37A FOREIGN KEY (section_id) REFERENCES sections (id) ON DELETE SET NULL');
        $this->addSql('ALTER TABLE annees_scolaires ADD CONSTRAINT FK_4C3DBF6B716F1E3 FOREIGN KEY (ecole_id) REFERENCES ecoles (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE annees_scolaires ADD CONSTRAINT FK_4C3DBF6BD823E37A FOREIGN KEY (section_id) REFERENCES sections (id) ON DELETE SET NULL');
        $this->addSql('ALTER TABLE classes ADD CONSTRAINT FK_8F87BF96716F1E3 FOREIGN KEY (ecole_id) REFERENCES ecoles (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE classes ADD CONSTRAINT FK_8F87BF96D823E37A FOREIGN KEY (section_id) REFERENCES sections (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE classes ADD CONSTRAINT FK_8F87BF96B3E9C81 FOREIGN KEY (niveau_id) REFERENCES niveaux (id) ON DELETE SET NULL');
        $this->addSql('ALTER TABLE classes ADD CONSTRAINT FK_8F87BF96E0C82FDD FOREIGN KEY (annee_scolaire_id) REFERENCES annees_scolaires (id) ON DELETE SET NULL');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE classes DROP FOREIGN KEY FK_8F87BF96716F1E3');
        $this->addSql('ALTER TABLE classes DROP FOREIGN KEY FK_8F87BF96D823E37A');
        $this->addSql('ALTER TABLE classes DROP FOREIGN KEY FK_8F87BF96B3E9C81');
        $this->addSql('ALTER TABLE classes DROP FOREIGN KEY FK_8F87BF96E0C82FDD');
        $this->addSql('ALTER TABLE annees_scolaires DROP FOREIGN KEY FK_4C3DBF6B716F1E3');
        $this->addSql('ALTER TABLE annees_scolaires DROP FOREIGN KEY FK_4C3DBF6BD823E37A');
        $this->addSql('ALTER TABLE niveaux DROP FOREIGN KEY FK_4E82760B716F1E3');
        $this->addSql('ALTER TABLE niveaux DROP FOREIGN KEY FK_4E82760BD823E37A');
        $this->addSql('ALTER TABLE sections DROP FOREIGN KEY FK_2D737AE2716F1E3');
        $this->addSql('DROP TABLE classes');
        $this->addSql('DROP TABLE annees_scolaires');
        $this->addSql('DROP TABLE niveaux');
        $this->addSql('DROP TABLE sections');
        $this->addSql('ALTER TABLE ecoles DROP hero_image');
    }
}
