<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;

use App\Models\Map\Lahan;

class LahanController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(): JsonResponse
    {
        Gate::authorize('petani');

        $user = Auth::user();
        $profile = $user->profile;

        $listLahan = Lahan::where('profile_id', $profile->id)->get();

        return response()->json($listLahan);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request): JsonResponse
    {
        Gate::authorize('petani');

        $user = Auth::user();
        $profile = $user->profile;

        $validated = $request->validate([
            'nama' => ['required', 'string', 'max:255'],
            'status_kepemilikan' => ['required', 'string'],
            'desa_kelurahan_id' => ['numeric'],
            'path' => ['string'],
            'luas_lahan' => ['numeric'],
        ]);

        $lahan = Lahan::create([
            'nama' => $validated['nama'],
            'status_kepemilikan' => $validated['status_kepemilikan'],
            'profile_id' => $profile->id,
            'desa_kelurahan_id' => $validated['desa_kelurahan_id'] ?? null,
            'path' => $validated['path'] ?? null,
            'luas_lahan' => $validated['luas_lahan'] ?? null,
        ]);

        return response()->json($lahan, 201);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        Gate::authorize('petani');

        $lahan = Lahan::findOrFail($id);

        return response()->json($lahan);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        Gate::authorize('petani');

        $lahan = Lahan::findOrFail($id);

        $validated = $request->validate([
            'nama' => ['required', 'string', 'max:255'],
            'status_kepemilikan' => ['required', 'string'],
            'desa_kelurahan_id' => ['numeric'],
            'path' => ['string'],
            'luas_lahan' => ['numeric'],
        ]);

        $lahan->update($validated);

        return response()->json($lahan);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        Gate::authorize('petani');

        $lahan = Lahan::findOrFail($id);        
        $lahan->delete();

        return response()->json(null, 204);
    }

    public function batchUpsert(Request $request): JsonResponse
    {
        Gate::authorize('petani');
        $user = Auth::user();
        $profile = $user->profile;

        $validated = $request->validate([
            'lahans' => ['required', 'array'],
            'lahans.*.id' => ['numeric'],
            'lahans.*.nama' => ['required', 'string', 'max:255'],
            'lahans.*.status_kepemilikan' => ['required', 'numeric'],
            'lahans.*.desa_kelurahan_id' => ['numeric'],
            'lahans.*.path' => ['string'],
            'lahans.*.luas_lahan' => ['numeric'],
        ]);

        $lahans = [];
        foreach ($validated['lahans'] as $lahan) {
            $lahans[] = [
                'id' => $lahan['id'] ?? null,
                'nama' => $lahan['nama'],
                'status_kepemilikan' => $lahan['status_kepemilikan'],
                'profile_id' => $profile->id,
                'desa_kelurahan_id' => $lahan['desa_kelurahan_id'] ?? null,
                'path' => $lahan['path'] ?? null,
                'luas_lahan' => $lahan['luas_lahan'] ?? null,
            ];
        }

        Lahan::upsert($lahans, ['id'], ['nama', 'status_kepemilikan', 'profile_id', 'desa_kelurahan_id', 'path', 'luas_lahan']);

        return response()->json(null, 204);
    }
}
