<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SikkBankSoalController extends Controller
{
    public function index(Request $request)
    {
        $urusan = $request->get('urusan');
        $status = $request->get('status');

        $query = DB::table('bank_soal_sikk')
            ->orderBy('kode_ikk', 'asc');

        if ($urusan) {
            $query->where('urusan', $urusan);
        }

        if ($status) {
            $query->where('status', $status);
        }

        $bankSoal = $query->get();

        // Get unique urusan for filter
        $urusanList = DB::table('bank_soal_sikk')
            ->select('urusan')
            ->distinct()
            ->pluck('urusan');

        return view('sikk.bank_soal.index', compact('bankSoal', 'urusanList', 'urusan', 'status'));
    }

    public function create()
    {
        return view('sikk.bank_soal.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'kode_ikk' => 'required|string|unique:bank_soal_sikk,kode_ikk',
            'urusan' => 'required|string',
            'bidang' => 'required|string',
            'ikk_output' => 'required|string',
            'ikk_outcome' => 'required|string',
            'rumus_perhitungan' => 'required|string',
            'satuan' => 'required|string',
            'sumber_data' => 'required|string',
            'opd_penanggung_jawab' => 'required|string',
            'tahun_mulai' => 'required|integer|min:2020',
            'status' => 'required|in:Aktif,Tidak Aktif'
        ]);

        DB::table('bank_soal_sikk')->insert([
            'kode_ikk' => $request->kode_ikk,
            'urusan' => $request->urusan,
            'bidang' => $request->bidang,
            'ikk_output' => $request->ikk_output,
            'ikk_outcome' => $request->ikk_outcome,
            'rumus_perhitungan' => $request->rumus_perhitungan,
            'satuan' => $request->satuan,
            'sumber_data' => $request->sumber_data,
            'opd_penanggung_jawab' => $request->opd_penanggung_jawab,
            'tahun_mulai' => $request->tahun_mulai,
            'status' => $request->status,
            'created_at' => now(),
            'updated_at' => now()
        ]);

        session()->flash('success', 'Bank Soal SIKK berhasil ditambahkan');
        return redirect()->route('sikk.bank-soal.index');
    }

    public function show($id)
    {
        $soal = DB::table('bank_soal_sikk')->where('id', $id)->first();

        if (!$soal) {
            return redirect()->route('sikk.bank-soal.index')
                ->with('error', 'Data tidak ditemukan');
        }

        return view('sikk.bank_soal.show', compact('soal'));
    }

    public function edit($id)
    {
        $soal = DB::table('bank_soal_sikk')->where('id', $id)->first();

        if (!$soal) {
            return redirect()->route('sikk.bank-soal.index')
                ->with('error', 'Data tidak ditemukan');
        }

        return view('sikk.bank_soal.edit', compact('soal'));
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'kode_ikk' => 'required|string|unique:bank_soal_sikk,kode_ikk,' . $id,
            'urusan' => 'required|string',
            'bidang' => 'required|string',
            'ikk_output' => 'required|string',
            'ikk_outcome' => 'required|string',
            'rumus_perhitungan' => 'required|string',
            'satuan' => 'required|string',
            'sumber_data' => 'required|string',
            'opd_penanggung_jawab' => 'required|string',
            'tahun_mulai' => 'required|integer|min:2020',
            'status' => 'required|in:Aktif,Tidak Aktif'
        ]);

        DB::table('bank_soal_sikk')
            ->where('id', $id)
            ->update([
                'kode_ikk' => $request->kode_ikk,
                'urusan' => $request->urusan,
                'bidang' => $request->bidang,
                'ikk_output' => $request->ikk_output,
                'ikk_outcome' => $request->ikk_outcome,
                'rumus_perhitungan' => $request->rumus_perhitungan,
                'satuan' => $request->satuan,
                'sumber_data' => $request->sumber_data,
                'opd_penanggung_jawab' => $request->opd_penanggung_jawab,
                'tahun_mulai' => $request->tahun_mulai,
                'status' => $request->status,
                'updated_at' => now()
            ]);

        session()->flash('success', 'Bank Soal SIKK berhasil diperbarui');
        return redirect()->route('sikk.bank-soal.index');
    }

    public function destroy($id)
    {
        DB::table('bank_soal_sikk')->where('id', $id)->delete();

        return redirect()->route('sikk.bank-soal.index')
            ->with('success', 'Bank Soal SIKK berhasil dihapus');
    }
}