<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * افزودن نام تمپلت کاوه‌نگار (VerifyLookup) و نگاشت متغیر→token به تمپلت‌های پیامک سیستمی،
 * و پرکردن مقادیر برای تگ‌های موجود.
 */
final class Version20260705072835 extends AbstractMigration
{
    private const SEED = [
        'otp'                 => ['clinicpro-otp',           ['code' => 'token', 'site' => 'token10']],
        'payment'             => ['clinicpro-payment',       ['doctor' => 'token10', 'date' => 'token20']],
        'clinic_invitation'   => ['clinicpro-clinic-invite', ['clinic' => 'token10', 'link' => 'token']],
        'pre_registration'    => ['clinicpro-pre-register',  ['username' => 'token', 'password' => 'token2', 'link' => 'token3']],
        'notification_mobile' => ['clinicpro-notify-code',   ['code' => 'token']],
        'secretary'           => ['clinicpro-secretary',     ['owner' => 'token10', 'username' => 'token', 'link' => 'token3']],
        'doctor_appointment'  => ['clinicpro-doctor-appt',   ['patient' => 'token10', 'date' => 'token2', 'time' => 'token3']],
        'welcome'             => ['clinicpro-welcome',       ['name' => 'token10', 'site' => 'token20']],
    ];

    public function getDescription(): string
    {
        return 'sms_message_templates: افزودن kavenegar_template و token_map + پرکردن تگ‌های موجود';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('ALTER TABLE sms_message_templates ADD kavenegar_template VARCHAR(100) DEFAULT NULL, ADD token_map JSON DEFAULT NULL');

        foreach (self::SEED as $tag => [$template, $map]) {
            $this->addSql(
                'UPDATE sms_message_templates SET kavenegar_template = :tpl, token_map = :map WHERE tag = :tag',
                ['tpl' => $template, 'map' => json_encode($map, JSON_UNESCAPED_UNICODE), 'tag' => $tag]
            );
        }

        $this->addSql("UPDATE sms_message_templates SET token_map = '[]' WHERE token_map IS NULL");
        $this->addSql('ALTER TABLE sms_message_templates MODIFY token_map JSON NOT NULL');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE sms_message_templates DROP kavenegar_template, DROP token_map');
    }
}
