<?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 Version20260610113711 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Change clinic_logo from JSON array to single URL string';
    }

    public function up(Schema $schema): void
    {
        // Extract first URL from JSON array into a plain temp column
        $this->addSql("ALTER TABLE clinics ADD COLUMN clinic_logo_new VARCHAR(500) DEFAULT NULL");
        $this->addSql("UPDATE clinics SET clinic_logo_new = JSON_UNQUOTE(JSON_EXTRACT(clinic_logo, '$[0].url')) WHERE clinic_logo IS NOT NULL AND JSON_LENGTH(clinic_logo) > 0");
        $this->addSql("ALTER TABLE clinics DROP COLUMN clinic_logo");
        $this->addSql("ALTER TABLE clinics CHANGE clinic_logo_new clinic_logo VARCHAR(500) DEFAULT NULL");
    }

    public function down(Schema $schema): void
    {
        $this->addSql("ALTER TABLE clinics ADD COLUMN clinic_logo_old LONGTEXT DEFAULT NULL COMMENT '(DC2Type:json)'");
        $this->addSql("UPDATE clinics SET clinic_logo_old = JSON_ARRAY(JSON_OBJECT('url', clinic_logo)) WHERE clinic_logo IS NOT NULL");
        $this->addSql("ALTER TABLE clinics DROP COLUMN clinic_logo");
        $this->addSql("ALTER TABLE clinics CHANGE clinic_logo_old clinic_logo LONGTEXT DEFAULT NULL COMMENT '(DC2Type:json)'");
    }
}
