<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Models\Perhitungan;
use Illuminate\Http\Request;

class PerhitunganController extends Controller
{
    public function index(Request $request)
    {
        $query = Perhitungan::latest();

        if ($request->search) {
            $query->where('nama_pewaris', 'like', "%{$request->search}%");
        }

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

        $data = $query->paginate($request->per_page ?? 15);

        return response()->json([
            'success' => true,
            'data'    => $data,
        ]);
    }

    public function show($id)
    {
        $perhitungan = Perhitungan::findOrFail($id);

        return response()->json([
            'success' => true,
            'data'    => $perhitungan,
        ]);
    }

    public function destroy($id)
    {
        $perhitungan = Perhitungan::findOrFail($id);
        $perhitungan->delete();

        return response()->json([
            'success' => true,
            'message' => 'Data perhitungan berhasil dihapus.',
        ]);
    }

    public function bulkDestroy(Request $request)
    {
        $request->validate(['ids' => 'required|array']);
        Perhitungan::whereIn('id', $request->ids)->delete();

        return response()->json([
            'success' => true,
            'message' => count($request->ids) . ' data berhasil dihapus.',
        ]);
    }
}
