<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ShopPayment;
use Illuminate\Http\Request;
use Inertia\Inertia;

class AdminPaymentController extends Controller
{
    public function index()
    {
        $payments = ShopPayment::with('shopUser')
            ->latest('date')
            ->get();

        return Inertia::render('Admin/Pages/Payments/Index', [
            'payments' => $payments,
        ]);
    }

    public function create()
    {
        return Inertia::render('Admin/Pages/Payments/Create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'shop_user_id' => 'required|exists:shop_users,id',
            'amount' => 'required|numeric|min:0',
            'payment_method' => 'required|string',
            'reference' => 'nullable|string',
        ]);

        ShopPayment::create([
            'shop_user_id' => $request->shop_user_id,
            'amount' => $request->amount,
            'status' => 'completed',
            'payment_method' => $request->payment_method,
            'reference' => $request->reference,
            'date' => today(),
        ]);

        return redirect()->route('admin.payments.index')->with('success', 'Payment recorded!');
    }

    public function approve(ShopPayment $payment)
    {
        $payment->update(['status' => 'completed']);

        return redirect()->back()->with('success', 'Payment approved!');
    }
}
