<?php

namespace App\Http\Controllers;

use App\Models\Kriteria;
use App\Models\Penilaian;
use App\Models\Alternatif;
use App\Models\SubKriteria;
use App\Models\MemberKelompokTani;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PenilaianController extends Controller
{
    public function index()
    {
        $user = Auth::user();

        // Tentukan daftar alternatif berdasarkan role
        if ($user->role === 'super_admin') {
            $alternatifs = Alternatif::all();
        } elseif ($user->role === 'admin') {
            $member = MemberKelompokTani::where('user_id', $user->id)->first();
            if ($member) {
                $userIds = MemberKelompokTani::where('kelompok_tani_id', $member->kelompok_tani_id)
                    ->pluck('user_id');
                $alternatifs = Alternatif::whereIn('user_id', $userIds)->get();
            } else {
                $alternatifs = collect(); // kosong jika admin belum terdaftar di kelompok
            }
        } else {
            $alternatifs = Alternatif::where('user_id', $user->id)->get();
        }

        $kriterias = Kriteria::all();
        $subKriterias = SubKriteria::all();

        return view('admin.penilaian', [
            'alternatifs' => $alternatifs,
            'kriterias' => $kriterias,
            'subKriterias' => $subKriterias,
            'title' => 'Penilaian Alternatif'
        ]);
    }

    public function store(Request $request)
    {
        $user = Auth::user();

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

        foreach ($request->penilaian as $alternatifId => $kriteriaData) {
            $alternatif = Alternatif::find($alternatifId);

            // Pastikan user hanya menilai alternatif yang berhak dia akses
            if (!$alternatif) continue;

            if ($user->role === 'super_admin') {
                // Super admin bisa menilai semua
            } elseif ($user->role === 'admin') {
                $adminGroup = MemberKelompokTani::where('user_id', $user->id)->first();
                $targetGroup = MemberKelompokTani::where('user_id', $alternatif->user_id)->first();

                if (!$adminGroup || !$targetGroup || $adminGroup->kelompok_tani_id !== $targetGroup->kelompok_tani_id) {
                    continue; // skip jika beda kelompok
                }
            } elseif ($alternatif->user_id !== $user->id) {
                continue; // user hanya bisa menilai lahan sendiri
            }

            // Simpan atau update penilaian
            foreach ($kriteriaData as $kriteriaId => $subKriteriaId) {
                Penilaian::updateOrCreate(
                    [
                        'alternatif_id' => $alternatifId,
                        'kriteria_id' => $kriteriaId,
                    ],
                    [
                        'subkriteria_id' => $subKriteriaId,
                    ]
                );
            }
        }

        return redirect()->route('penilaian.index')->with('success', 'Penilaian berhasil disimpan!');
    }
}
