<?php

namespace App\Services;

class AiSolutionService
{
    public function generateSolution(string $questionText, array $context = []): array
    {
        $questionType = $context['question_type'] ?? null;
        $needsSteps = in_array($questionType, ['short', 'answer', 'calc', 'calculation', 'word_problem', 'proof'], true);

        if ($needsSteps) {
            return $this->generateSolutionSteps($questionText);
        }

        return [
            'solution' => '',
            'steps' => [],
            'source' => 'placeholder',
        ];
    }

    public function generateStepRubric(string $questionText, array $context = []): array
    {
        return [
            'steps' => [],
            'total_score' => $context['total_score'] ?? null,
        ];
    }

    public function validateAnswer(string $questionText, string $answer, array $context = []): array
    {
        return [
            'is_correct' => null,
            'confidence' => null,
            'notes' => [],
        ];
    }

    public function generateSolutionSteps(string $questionText): array
    {
        $prompt = app(QuestionPromptService::class)->buildSolutionStepsPrompt($questionText);

        try {
            $result = app(AiClientService::class)->callJson($prompt);
        } catch (\Throwable $e) {
            return [
                'solution' => '',
                'steps' => [],
                'source' => 'placeholder',
            ];
        }

        return [
            'solution' => $result['solution'] ?? '',
            'steps' => $result['steps'] ?? [],
            'source' => 'ai',
        ];
    }
}
