<?php

namespace OtomaticAi\Database\Migrations;

class AddGenerationStateColumnsMigration extends Migration
{
    /**
     * Run a new migration
     *
     * @return void
     */
    public function run()
    {
        global $wpdb;

        if (!$this->canRun()) {
            return;
        }

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

        $publications_table_name = $wpdb->prefix . "otomatic_ai_publications";
        $single_mode_generations_table_name = $wpdb->prefix . "otomatic_ai_single_mode_generations";
        $netlinking_table_name = $wpdb->prefix . "otomatic_ai_netlinking";
        $thumbnail_tasks_table_name = $wpdb->prefix . "otomatic_ai_thumbnail_tasks";

        $sql = "ALTER TABLE `{$publications_table_name}` ADD COLUMN `generation_state` longtext DEFAULT NULL AFTER `status`;";
        $wpdb->query($sql);

        $sql = "ALTER TABLE `{$single_mode_generations_table_name}` ADD COLUMN `generation_state` longtext DEFAULT NULL AFTER `status`;";
        $wpdb->query($sql);

        $sql = "ALTER TABLE `{$netlinking_table_name}` ADD COLUMN `generation_state` longtext DEFAULT NULL AFTER `status`;";
        $wpdb->query($sql);

        $sql = "ALTER TABLE `{$thumbnail_tasks_table_name}` ADD COLUMN `generation_state` longtext DEFAULT NULL AFTER `status`;";
        $wpdb->query($sql);
    }
}
