<?php

namespace App\Http\Controllers;

use App\Models\JawabanKuesioner;
use App\Models\PertanyaanKuesioner;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class KuesionerController extends Controller
{
    public function index()
    {
        $pertanyaans = PertanyaanKuesioner::with('dimensi')->orderBy('urutan')->get();

        return view('kuesioner.form', compact('pertanyaans'));
    }

    public function store(Request $request)
    {
        $week = now()->weekOfYear;
        $year = now()->year;
        $tanggal = now()->toDateString();

        $request->validate([
            'id_siswa' => 'required',
            'jawaban' => 'required|array',
        ]);

        foreach ($request->jawaban as $pertanyaan_id => $nilai) {
            JawabanKuesioner::create([
                'siswa_id' => $request['id_siswa'],
                'pertanyaan_id' => $pertanyaan_id,
                'nilai' => $nilai,
                'minggu_ke' => $week,
                'tahun' => $year,
                'tanggal_isi' => $tanggal,
            ]);
        }

        $notification = [
            'message' => "Data kuesioner berhasil di rekam!",
            'alert-type' => 'success'
        ];

        return redirect()->route('kuesioner.index', $request->id_siswa)->with($notification);
    }
}
