<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\TransaksiPenempatanRequest;
use App\Services\TransaksiPenempatanService;
use Carbon\Carbon;
use Exception;
use Illuminate\Http\Request;

class TransaksiPenempatanController extends Controller
{
    protected $transaksiPenempatanService;

    public function __construct(TransaksiPenempatanService $transaksiPenempatanService)
    {
        $this->transaksiPenempatanService = $transaksiPenempatanService;
    }

    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->transaksiPenempatanService->getAll($tgl_awal, $tgl_akhir, $rekening);
    }

    public function create()
    {
        //
    }

    public function store(TransaksiPenempatanRequest $request)
    {
        try {
            $validated = $request->validated();

            $this->transaksiPenempatanService->createTransaksi($validated);

            return response()->json($validated, 201);
        } catch (Exception $e) {
            return response()->json(['message' => $e->getMessage()], 400);
        }
    }

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

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

    public function update(Request $request, string $id)
    {
        //
    }

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