<?php

namespace App\Http\Controllers;

use App\Models\BankWithdrawal;
use App\Models\Currency;
use Illuminate\Http\Request;

class BankWithdrawalController extends Controller
{
    public function index()
    {
        $withdrawals = BankWithdrawal::with('currency')->latest()->paginate(20);

        return view('bank_withdrawals.index', compact('withdrawals'));
    }

    public function create()
    {
        $currencies = Currency::all();

        return view('bank_withdrawals.create', compact('currencies'));
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'withdrawal_date' => ['required', 'date'],
            'amount'          => ['required', 'numeric', 'min:0.01'],
            'currency_id'     => ['nullable', 'exists:currencies,id'],
            'bank_account'    => ['nullable', 'string', 'max:255'],
            'reference'       => ['nullable', 'string', 'max:255'],
            'notes'           => ['nullable', 'string'],
        ]);

        BankWithdrawal::create($data);

        return redirect()->route('bank-withdrawals.index')->with('success', __('Bank withdrawal recorded.'));
    }

    public function destroy(BankWithdrawal $bankWithdrawal)
    {
        $bankWithdrawal->delete();

        return redirect()->route('bank-withdrawals.index')->with('success', __('Record deleted.'));
    }
}
