<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\SesiKuis;
use App\Models\HasilSaw;
use App\Models\JawabanKuis;
use Barryvdh\DomPDF\Facade\Pdf;
use App\Models\Kriteria;
use App\Models\Alternatif;
use App\Models\Subkriteria;
use App\Models\DetailHasilSaw;

class PerhitunganController extends Controller
{
    /**
     * Display the SAW calculation results.
     */
    public function index()
    {
        $hasilsaw = HasilSaw::with(['sesikuis', 'alternatif.mapels'])->orderBy('sesi_kuis_id')->orderBy('peringkat')->get();
        $sesikuis = SesiKuis::with('user', 'kurikulum')->orderBy('created_at', 'desc')->get();
        $jawaban = JawabanKuis::with(['sesikuis', 'subkriteria'])->orderBy('sesi_kuis_id')->get();
        
        return view('admin.perhitungan', compact('hasilsaw', 'sesikuis', 'jawaban'));
    }
    
    /**
     * Get detail of session quiz.
     */
    public function getSesiDetail($id)
    {
        $sesi = SesiKuis::with('kurikulum', 'user')->findOrFail($id);
        
        // Get SAW results for this session
        $hasil = HasilSaw::with('alternatif.mapels')
        ->where('sesi_kuis_id', $id)
        ->orderBy('peringkat', 'asc')
        ->get();
        
        // Get answers for this session
        $jawaban = JawabanKuis::with('subkriteria')
            ->where('sesi_kuis_id', $id)
            ->get();
        
        return response()->json([
            'sesi' => $sesi,
            'hasil' => $hasil,
            'jawaban' => $jawaban
        ]);
    }
    
    // Add these methods to your SawController or relevant controller
public function printSesiPDF($id)
{
    // Get the session data
    $sesi = SesiKuis::with(['kurikulum'])->findOrFail($id);
    
    // Get the results for this session
    $hasil = HasilSaw::with(['alternatif'])
                ->where('sesi_kuis_id', $id)
                ->orderBy('peringkat', 'asc')
                ->get();
                
    // Get the answers for this session
    $jawaban = JawabanKuis::with(['subkriteria'])
                ->where('sesi_kuis_id', $id)
                ->get();
    
    // Generate PDF
    $pdf = PDF::loadView('admin.sesi_detail_pdf', compact('sesi', 'hasil', 'jawaban'));
    
    // Download the PDF
    return $pdf->download('hasil_sesi_' . $sesi->nama . '.pdf');
}

public function printAllSesiPDF()
{
    // Get all session data
    $allSesi = SesiKuis::with(['kurikulum'])->orderBy('created_at', 'desc')->get();
    
    // Collect all results and answers for each session
    $data = [];
    foreach ($allSesi as $sesi) {
        $hasil = HasilSaw::with(['alternatif'])
                    ->where('sesi_kuis_id', $sesi->id)
                    ->orderBy('peringkat', 'asc')
                    ->get();
                    
        $jawaban = JawabanKuis::with(['subkriteria'])
                    ->where('sesi_kuis_id', $sesi->id)
                    ->get();
                    
        $data[] = [
            'sesi' => $sesi,
            'hasil' => $hasil,
            'jawaban' => $jawaban
        ];
    }
    
    // Generate PDF
    $pdf = PDF::loadView('admin.all_sesi_pdf', compact('data'));
    
    // Download the PDF
    return $pdf->download('semua_hasil_sesi.pdf');
}
}