<?php

namespace App\Console\Commands;

use App\Models\Question;
use App\Models\QuestionAsset;
use App\Services\SvgConverterService;
use Illuminate\Console\Command;

class SyncQuestionAssetsCommand extends Command
{
    protected $signature = 'question:sync-assets';
    protected $description = 'Extract and sync SVG/image assets for questions';

    public function handle(SvgConverterService $svgService): int
    {
        $questions = Question::query()->get(['id', 'stem']);

        foreach ($questions as $question) {
            $assets = $svgService->extractSvgAssets($question->stem ?? '');

            foreach ($assets as $asset) {
                QuestionAsset::firstOrCreate([
                    'question_id' => $question->id,
                    'asset_type' => $asset['type'] ?? 'svg',
                    'path' => $asset['path'] ?? '',
                ], [
                    'meta' => $asset['meta'] ?? [],
                ]);
            }
        }

        $this->info('Question assets synced.');

        return self::SUCCESS;
    }
}
