<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\TransaksiTabunganRequest;
use App\Services\TransaksiTabunganService;
use Carbon\Carbon;
use Illuminate\Http\Request;

class TransaksiTabunganController extends Controller
{
    protected $transaksiTabunganService;

    public function __construct(TransaksiTabunganService $transaksiTabungan)
    {
        $this->transaksiTabunganService = $transaksiTabungan;
    }

    public function index(Request $request)
    {
        $tgl_awal = $request->input('tgl_awal', Carbon::now());
        $tgl_akhir = $request->input('tgl_akhir', Carbon::now());
        $rekening = $request->input('rekening', null);
        return $this->transaksiTabunganService->getAll($tgl_awal, $tgl_akhir, $rekening);
    }

    public function create() {}

    public function store(TransaksiTabunganRequest $request)
    {
        $validated = $request->validated();
        return $this->transaksiTabunganService->createTransaksi($validated);
    }

    public function show(string $id)
    {
        $transaksi = $this->transaksiTabunganService->findById($id);
        if (!$transaksi) {
            return response()->json(['message' => 'Transaksi tidak ditemukan'], 404);
        }
        return response()->json($transaksi);
    }

    public function edit(string $id)
    {
        //
    }

    public function update(Request $request, string $id)
    {
        return $this->transaksiTabunganService->validasiTransaksi($id);
    }

    public function destroy(string $id)
    {
        return $this->transaksiTabunganService->deleteTransaksi($id);
    }
}
