<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\PaymentMethod;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
use Illuminate\Support\Facades\Storage;

class PaymentMethodController extends Controller
{
    public function index(Request $request): View
    {
        $query = PaymentMethod::query();

        if ($request->filled('search')) {
            $query->where('name', 'like', '%' . $request->search . '%')
                  ->orWhere('code', 'like', '%' . $request->search . '%');
        }

        if ($request->filled('provider')) {
            $query->where('provider', $request->provider);
        }

        $paymentMethods = $query->latest()->paginate(10)->withQueryString();

        return view('pages.admin.payment_methods.index', compact('paymentMethods'));
    }

    public function create(): View
    {
        return view('pages.admin.payment_methods.create');
    }

    public function store(Request $request): RedirectResponse
    {
        $data = $request->validate([
            'name'            => 'required|string|max:100',
            'code'            => 'required|string|max:50|unique:payment_methods,code',
            'group'           => 'required|in:bank,ewallet,qris',
            'type'            => 'required|in:automatic,manual',
            'account_number'            => 'nullable|string|max:50',
            'account_name'            => 'nullable|string|max:50',
            'provider'        => 'required|in:tripay,duitku,paydisini,internal',
            'logo'            => 'nullable|image|max:2048',
            'fee_flat'        => 'required|integer|min:0',
            'fee_percent'     => 'required|numeric|min:0',
            'minimum_amount'  => 'required|integer|min:0',
            'maximum_amount'  => 'required|integer|min:0',
            'is_active'       => 'boolean',
        ]);

        if ($request->hasFile('logo')) {
            $data['logo'] = $request->file('logo')->store('payment-methods', 'public');
        }

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

        PaymentMethod::create($data);

        return redirect()
            ->route('admin.payment-methods.index')
            ->with('success', 'Payment Method berhasil ditambahkan.');
    }

    public function edit(PaymentMethod $paymentMethod): View
    {
        return view('pages.admin.payment_methods.edit', compact('paymentMethod'));
    }

    public function update(Request $request, PaymentMethod $paymentMethod): RedirectResponse
    {
        $data = $request->validate([
            'name'            => 'required|string|max:100',
            'code'            => 'required|string|max:50|unique:payment_methods,code,' . $paymentMethod->id,
            'group'           => 'required|in:bank,ewallet,qris',
            'type'            => 'required|in:automatic,manual',
            'provider'        => 'required|in:tripay,duitku,paydisini,internal',
            'logo'            => 'nullable|image|max:2048',
            'fee_flat'        => 'required|integer|min:0',
            'fee_percent'     => 'required|numeric|min:0',
            'minimum_amount'  => 'required|integer|min:0',
            'maximum_amount'  => 'required|integer|min:0',
            'is_active'       => 'boolean',
        ]);

        if ($request->hasFile('logo')) {
            if ($paymentMethod->logo) {
                Storage::disk('public')->delete($paymentMethod->logo);
            }
            $data['logo'] = $request->file('logo')->store('payment-methods', 'public');
        }

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

        $paymentMethod->update($data);

        return redirect()
            ->route('admin.payment-methods.index')
            ->with('success', 'Payment Method berhasil diperbarui.');
    }

    public function destroy(PaymentMethod $paymentMethod): RedirectResponse
    {
        if ($paymentMethod->logo) {
            Storage::disk('public')->delete($paymentMethod->logo);
        }

        $paymentMethod->delete();

        return back()->with('success', 'Payment Method berhasil dihapus.');
    }
}
