<?php

namespace App\Http\Controllers\Web\Ekosistem;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Pertanian\Tanaman;
use App\Models\Pertanian\Komoditas;
use App\Models\Pertanian\Lahan;
use App\Models\Profile;
use Illuminate\Support\Facades\Validator;

class TanamanController extends Controller
{
    public function index(Request $request)
    {
        $query = Tanaman::with(['komoditas', 'lahan', 'profile'])->orderBy('created_at', 'desc');

        if ($request->search) {
            $query->where('nama', 'like', '%' . $request->search . '%')
                  ->orWhereHas('lahan', function($q) use ($request) {
                      $q->where('nama', 'like', '%' . $request->search . '%');
                  });
        }

        $tanaman = $query->paginate($request->per_page ?? 10);
        return view('master.tanaman.index', compact('tanaman'));
    }

    public function create()
    {
        $komoditas = Komoditas::orderBy('nama', 'asc')->get();
        $profiles  = Profile::orderBy('nama', 'asc')->get();
        $lahan     = Lahan::orderBy('nama', 'asc')->get();

        return view('master.tanaman.add', compact('komoditas', 'profiles', 'lahan'));
    }

    public function getLahan($id)
    {
        $lahan = Lahan::findOrFail($id);
        return response()->json([
            'status' => 'success',
            'path'   => $lahan->path
        ]);
    }

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'nama'            => ['required', 'string', 'max:255'],
            'komoditas_id'    => ['required'],
            'profile_id'      => ['required'],
            'lahan_id'        => ['required'],
            'kondisi_tanaman' => ['required', 'in:sehat,sakit,mati'],
            'latitude'        => ['required', 'numeric'],
            'longitude'       => ['required', 'numeric'],
        ], [
            'latitude.required' => 'Titik koordinat wajib diisi. Silakan klik area pada peta.'
        ]);

        if ($validator->fails()) {
            return response()->json(['status' => 'error', 'errors' => $validator->errors()], 422);
        }

        Tanaman::create([
            'nama'            => $request->nama,
            'komoditas_id'    => $request->komoditas_id,
            'profile_id'      => $request->profile_id,
            'lahan_id'        => $request->lahan_id,
            'kondisi_tanaman' => $request->kondisi_tanaman,
            'latitude'        => $request->latitude,
            'longitude'       => $request->longitude,
            'created_by'      => auth()->id(),
            'updated_by'      => auth()->id(),
        ]);

        return response()->json(['status' => 'success', 'message' => 'Data Tanaman berhasil ditambahkan.']);
    }

    public function show($id)
    {
        $tanaman = Tanaman::with(['komoditas', 'lahan', 'profile'])->findOrFail($id);
        return view('master.tanaman.view', compact('tanaman'));
    }
}