<?php

namespace App\Console\Commands;

use App\Models\Question;
use App\Models\QuestionMeta;
use Illuminate\Console\Command;

class BackfillQuestionMetaCommand extends Command
{
    protected $signature = 'question:backfill-meta';
    protected $description = 'Ensure question_meta rows exist for every question';

    public function handle(): int
    {
        $questions = Question::query()->get(['id']);
        $created = 0;

        foreach ($questions as $question) {
            $meta = QuestionMeta::firstOrCreate(
                ['question_id' => $question->id],
                [
                    'abilities' => [],
                    'generation_info' => [],
                    'review_status' => 'pending',
                ]
            );

            if ($meta->wasRecentlyCreated) {
                $created++;
            }
        }

        $this->info(sprintf('Question meta backfilled: %d created', $created));

        return self::SUCCESS;
    }
}
