<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\TransaksiKasRequest;
use App\Services\TransaksiKasService;
use Carbon\Carbon;
use Exception;
use Illuminate\Http\Request;

class TransaksiKasController extends Controller
{

    protected $transaksiKasService;

    public function __construct(TransaksiKasService $transaksiKasService)
    {
        $this->transaksiKasService = $transaksiKasService;
    }

    public function index(Request $request)
    {
        $tgl_awal = $request->input('tgl_awal', Carbon::now());
        $tgl_akhir = $request->input('tgl_akhir', Carbon::now());
        $id = $request->input('id', null);

        if ($id) {
            return $this->transaksiKasService->getAllTransaksiKas($tgl_awal, $tgl_akhir, $id);
        }
        return $this->transaksiKasService->getAll($tgl_awal, $tgl_akhir);
    }

    public function create()
    {
        //
    }

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

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

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

    public function show(string $id)
    {
        return $this->transaksiKasService->findTransaksiById($id);
    }

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

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

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