<?php

// app/Http/Controllers/FormController.php

namespace App\Http\Controllers;

use App\Models\Form;
use App\Models\FormResponse;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\DB;
class RespuestasController extends Controller
{

public function recibirRespuesta(Request $request)
{
    try {
        $validated = $request->validate([
            'form_id' => 'required|integer|exists:forms,id',
            'contenido_json' => 'required|json',
        ]);

        $usuarioLogueado = auth()->user()->idUsuario;

        $formId = DB::table('respuestas_formulario')->insertGetId([
            'form_id' => $validated['form_id'],
            'id_usuario' => $usuarioLogueado,
            'contenido_json' => $validated['contenido_json'],
            'created_at' => now()
        ]);

        return response()->json([
            'success' => true,
            'message' => 'Respuesta guardada correctamente',
            'id' => $formId
        ]);
    } catch (\Exception $e) {
        return response()->json([
            'success' => false,
            'message' => 'Error interno del servidor',
            'error' => $e->getMessage()
        ], 500);
    }
}




}
