<?php

/* For licensing terms, see /license.txt */

declare(strict_types=1);

namespace Chamilo\CoreBundle\Migrations\Schema\V200;

use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Doctrine\DBAL\Schema\Schema;

final class Version20250905073102 extends AbstractMigrationChamilo
{
    public function getDescription(): string
    {
        return 'Insert or update platform setting title/comment for disclose_ai_assistance.';
    }

    public function up(Schema $schema): void
    {
        $settings = [
            [
                'name' => 'disclose_ai_assistance',
                'title' => 'Disclose AI assistance',
                'comment' => 'Show a tag on any content or feedback that has been generated or co-generated by any AI system, evidencing to the user that the content was built with the help of some AI system. Details about which AI system was used in which case are kept inside the database for audit, but are not directly accessible by the final user.',
            ],
        ];

        foreach ($settings as $setting) {
            $variable = addslashes($setting['name']);
            $title = addslashes($setting['title']);
            $comment = addslashes($setting['comment']);

            $sqlCheck = \sprintf(
                "SELECT COUNT(*) AS count
                 FROM settings
                 WHERE variable = '%s'
                   AND subkey IS NULL
                   AND access_url = 1",
                $variable
            );

            $stmt = $this->connection->executeQuery($sqlCheck);
            $result = $stmt->fetchAssociative();

            if ($result && (int) $result['count'] > 0) {
                $this->addSql(\sprintf(
                    "UPDATE settings
                     SET title = '%s',
                         comment = '%s',
                         category = 'ai_helpers'
                     WHERE variable = '%s'
                       AND subkey IS NULL
                       AND access_url = 1",
                    $title,
                    $comment,
                    $variable
                ));
                $this->write(\sprintf('Updated setting: %s', $setting['name']));
            } else {
                $this->addSql(\sprintf(
                    "INSERT INTO settings
                        (variable, subkey, type, category, selected_value, title, comment, access_url_changeable, access_url_locked, access_url)
                     VALUES
                        ('%s', NULL, NULL, 'ai_helpers', 'true', '%s', '%s', 1, 0, 1)",
                    $variable,
                    $title,
                    $comment
                ));
                $this->write(\sprintf('Inserted setting: %s', $setting['name']));
            }
        }
    }

    public function down(Schema $schema): void
    {
        // Rollback: remove the setting entry (keeps system functional; defaults can apply)
        $variables = [
            'disclose_ai_assistance',
        ];

        foreach ($variables as $variable) {
            $this->addSql(\sprintf(
                "DELETE FROM settings
                 WHERE variable = '%s'
                   AND subkey IS NULL
                   AND access_url = 1",
                addslashes($variable)
            ));
            $this->write(\sprintf('Removed setting: %s.', $variable));
        }
    }
}
