<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20260824210000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Socle Passe 1 : familles, parents, enfants et terminaux.';
    }

    public function up(Schema $schema): void
    {
        $this->abortIf(!($this->connection->getDatabasePlatform() instanceof AbstractMySQLPlatform), 'Cette migration cible MySQL 8.4.');

        $this->addSql('CREATE TABLE families (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(120) NOT NULL, timezone VARCHAR(64) NOT NULL, created_at DATETIME NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
        $this->addSql('CREATE TABLE parent_users (id INT AUTO_INCREMENT NOT NULL, email VARCHAR(180) NOT NULL, display_name VARCHAR(100) NOT NULL, password VARCHAR(255) NOT NULL, active TINYINT DEFAULT 1 NOT NULL, created_at DATETIME NOT NULL, family_id INT NOT NULL, INDEX IDX_D8B2314AC35E566A (family_id), UNIQUE INDEX uniq_parent_email (email), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
        $this->addSql('CREATE TABLE children (id INT AUTO_INCREMENT NOT NULL, first_name VARCHAR(80) NOT NULL, active TINYINT DEFAULT 1 NOT NULL, display_order INT DEFAULT 0 NOT NULL, created_at DATETIME NOT NULL, family_id INT NOT NULL, INDEX IDX_A197B1BAC35E566A (family_id), INDEX idx_child_family_active (family_id, active), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
        $this->addSql('CREATE TABLE devices (id INT AUTO_INCREMENT NOT NULL, label VARCHAR(100) NOT NULL, status VARCHAR(16) NOT NULL, pairing_code_digest VARCHAR(64) DEFAULT NULL, pairing_expires_at DATETIME DEFAULT NULL, pairing_attempts INT DEFAULT 0 NOT NULL, credential_digest VARCHAR(64) DEFAULT NULL, credential_expires_at DATETIME DEFAULT NULL, paired_at DATETIME DEFAULT NULL, last_seen_at DATETIME DEFAULT NULL, revoked_at DATETIME DEFAULT NULL, created_at DATETIME NOT NULL, family_id INT NOT NULL, INDEX IDX_11074E9AC35E566A (family_id), INDEX idx_device_family_status (family_id, status), UNIQUE INDEX uniq_device_credential (credential_digest), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4');
        $this->addSql('ALTER TABLE parent_users ADD CONSTRAINT FK_D8B2314AC35E566A FOREIGN KEY (family_id) REFERENCES families (id) ON DELETE RESTRICT');
        $this->addSql('ALTER TABLE children ADD CONSTRAINT FK_A197B1BAC35E566A FOREIGN KEY (family_id) REFERENCES families (id) ON DELETE RESTRICT');
        $this->addSql('ALTER TABLE devices ADD CONSTRAINT FK_11074E9AC35E566A FOREIGN KEY (family_id) REFERENCES families (id) ON DELETE RESTRICT');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE parent_users DROP FOREIGN KEY FK_D8B2314AC35E566A');
        $this->addSql('ALTER TABLE children DROP FOREIGN KEY FK_A197B1BAC35E566A');
        $this->addSql('ALTER TABLE devices DROP FOREIGN KEY FK_11074E9AC35E566A');
        $this->addSql('DROP TABLE devices');
        $this->addSql('DROP TABLE children');
        $this->addSql('DROP TABLE parent_users');
        $this->addSql('DROP TABLE families');
    }
}
