<?php

namespace App\Http\Controllers\Student;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Lesson;
use App\Models\Course;
use App\Models\LessonQuestion;

class LessonQuestionController extends Controller
{
    /**
     * Store a new question from a student.
     */
    public function store(Request $request, Course $course, Lesson $lesson)
    {
        $request->validate([
            'question' => 'required|string|max:2000',
        ]);

        LessonQuestion::create([
            'lesson_id' => $lesson->id,
            'user_id' => Auth::id(),
            'question' => $request->question,
            'published' => 0,
        ]);

        return back()->with('success', 'Your question has been submitted.');
    }

    /**
     * Optional — allow teachers (or admins) to post an answer.
     */
    public function answer(Request $request, LessonQuestion $question)
    {
        $request->validate([
            'answer' => 'required|string|max:5000',
        ]);

        $question->update([
            'answer' => $request->answer,
            'answered_by' => Auth::id(),
            'answered_at' => now(),
        ]);

        return back()->with('success', 'Answer posted successfully.');
    }
}