<?php

namespace App\Http\Controllers;

use App\Models\LelangPersonil;
use App\Models\PaketLelang;
use App\Models\Personil;
use App\Support\Dropdown;
use Illuminate\Http\Request;

class LelangPersonilController extends Controller
{
    public function create(PaketLelang $paketLelang)
    {
        $personils = Personil::all();
        $statusBerkas = Dropdown::statusBerkas();
        return view('lelang-personils.create', compact('paketLelang', 'personils', 'statusBerkas'));
    }

    public function store(Request $request, PaketLelang $paketLelang)
    {
        $validated = $request->validate([
            'personil_id' => 'required|exists:personils,id|unique:lelang_personils,personil_id,NULL,id,paket_lelang_id,' . $paketLelang->id,
            'posisi_diusulkan' => 'nullable',
            'jenjang_minimal' => 'nullable|integer',
            'jenjang_personil' => 'nullable|integer',
            'kesesuaian_subklasifikasi' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'skk_aktif' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'cv_format_lelang' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'ijazah' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'ktp' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'npwp' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'pengalaman_referensi' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'surat_kesediaan_personil' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'surat_kuasa_penggunaan_data' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'pakta_integritas' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'bukti_verifikasi_qr_siki' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'daftar_riwayat_hidup_ttd' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'link_folder_lelang' => 'nullable|url',
            'link_surat_kesediaan' => 'nullable|url',
            'link_cv_lelang' => 'nullable|url',
            'pic' => 'nullable',
            'deadline' => 'nullable|date',
            'catatan' => 'nullable',
        ]);

        $personil = Personil::find($validated['personil_id']);
        if ($personil && empty($validated['jenjang_personil'])) {
            $validated['jenjang_personil'] = $personil->jenjang;
        }

        $paketLelang->lelangPersonils()->create($validated);

        return redirect()->route('paket-lelangs.show', $paketLelang)->with('success', 'Personil berhasil ditambahkan ke paket.');
    }

    public function edit(PaketLelang $paketLelang, LelangPersonil $lelangPersonil)
    {
        $statusBerkas = Dropdown::statusBerkas();
        return view('lelang-personils.edit', compact('paketLelang', 'lelangPersonil', 'statusBerkas'));
    }

    public function update(Request $request, PaketLelang $paketLelang, LelangPersonil $lelangPersonil)
    {
        $validated = $request->validate([
            'posisi_diusulkan' => 'nullable',
            'jenjang_minimal' => 'nullable|integer',
            'jenjang_personil' => 'nullable|integer',
            'kesesuaian_subklasifikasi' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'skk_aktif' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'cv_format_lelang' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'ijazah' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'ktp' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'npwp' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'pengalaman_referensi' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'surat_kesediaan_personil' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'surat_kuasa_penggunaan_data' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'pakta_integritas' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'bukti_verifikasi_qr_siki' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'daftar_riwayat_hidup_ttd' => 'nullable|in:' . implode(',', Dropdown::statusBerkas()),
            'link_folder_lelang' => 'nullable|url',
            'link_surat_kesediaan' => 'nullable|url',
            'link_cv_lelang' => 'nullable|url',
            'pic' => 'nullable',
            'deadline' => 'nullable|date',
            'catatan' => 'nullable',
        ]);

        $lelangPersonil->update($validated);

        return redirect()->route('paket-lelangs.show', $paketLelang)->with('success', 'Data berhasil diperbarui.');
    }

    public function destroy(PaketLelang $paketLelang, LelangPersonil $lelangPersonil)
    {
        $lelangPersonil->delete();
        return redirect()->route('paket-lelangs.show', $paketLelang)->with('success', 'Personil berhasil dihapus dari paket.');
    }
}
