<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20260507230001 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Replace device.wake_hour (single int hour) with device.wake_times (JSON array of minutes-since-midnight, 0-1439)';
    }

    public function up(Schema $schema): void
    {
        $this->addSql("ALTER TABLE device ADD wake_times JSON NOT NULL DEFAULT '[]'");
        $this->addSql("UPDATE device SET wake_times = json_build_array(wake_hour * 60) WHERE wake_hour IS NOT NULL");
        $this->addSql('ALTER TABLE device DROP COLUMN wake_hour');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE device ADD wake_hour INT DEFAULT NULL');
        $this->addSql("UPDATE device SET wake_hour = ((wake_times->>0)::int / 60) WHERE jsonb_array_length(wake_times::jsonb) > 0");
        $this->addSql('ALTER TABLE device DROP COLUMN wake_times');
    }
}
