<?php

namespace App\Http\Controllers;

use App\Models\CashModal;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class CashModalController extends Controller
{
    public function store(Request $request)
    {
        $validated = $request->validate([
            'amount' => 'required|numeric|min:0.01',
            'modal_date' => 'required|date',
            'type' => 'required|in:initial,topup',
            'account' => 'required|in:petty,cash,bank',
            'notes' => 'nullable|string|max:500',
        ]);

        $validated['user_id'] = Auth::id();

        DB::transaction(function () use ($validated) {
            CashModal::create($validated);
        });

        return redirect()->route('finance.cash')
            ->with('success', 'Modal berhasil ditambahkan');
    }
}