<?php

namespace App\Http\Controllers;

use App\Models\BankAccount;
use Illuminate\Http\Request;

class BankAccountController extends Controller
{
    public function store(Request $request)
    {
        $data = $request->validate([
            'label'          => 'nullable|string|max:100',
            'bank_name'      => 'required|string|max:100',
            'account_name'   => 'required|string|max:150',
            'account_number' => 'required|string|max:50',
            'branch'         => 'nullable|string|max:100',
            'swift_code'     => 'nullable|string|max:20',
            'is_active'      => 'nullable|boolean',
        ]);

        $data['is_active'] = $request->has('is_active');

        BankAccount::create($data);

        return redirect()->route('settings.index', ['#bank-details'])
            ->with('success', 'Bank account added successfully.');
    }

    public function update(Request $request, BankAccount $bankAccount)
    {
        $data = $request->validate([
            'label'          => 'nullable|string|max:100',
            'bank_name'      => 'required|string|max:100',
            'account_name'   => 'required|string|max:150',
            'account_number' => 'required|string|max:50',
            'branch'         => 'nullable|string|max:100',
            'swift_code'     => 'nullable|string|max:20',
            'is_active'      => 'nullable|boolean',
        ]);

        $data['is_active'] = $request->has('is_active');

        $bankAccount->update($data);

        return redirect()->route('settings.index', ['#bank-details'])
            ->with('success', 'Bank account updated successfully.');
    }

    public function destroy(BankAccount $bankAccount)
    {
        $bankAccount->delete();

        return redirect()->route('settings.index', ['#bank-details'])
            ->with('success', 'Bank account deleted.');
    }
}
