<?php

namespace App\Console\Commands;

use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

#[Signature('migrate:xf:configure')]
#[Description("Configure forums map for migrating threads.")]
class MigrateXFConfigure extends Command
{
    public function handle()
    {
        $forums = DB::connection('old_xf')
            ->table('node')
            ->where('node_type_id', 'Forum')
            ->select('node_id', 'title')
            ->get()
        ;

        $this->info("{$forums->count()} forums found.");

        $forumMap = [];
        foreach ($forums as $forum) {
            $newId = $this->ask("New ID for {$forum->title}");
            if( $newId !== null && $newId !== '' ){
                $map[$forum->node_id] = (int) $newId;
            }
        }

        DB::table('migration_settings')
            ->updateOrInsert(
                [ 'key' => 'old_xf_node_to_new_xf_node'],
                [ 'value' => json_encode($map), 'updated_at' => now() ],
            );

        $this->info("Config saved.");
    }
}
