<?php

namespace App\Http\Controllers\Customer;

use App\Core\Repositories\PaymentRepository;
use App\Http\Controllers\Controller;
use App\Models\Payment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;

class CustomerPaymentController extends Controller
{
    public function __construct(
        private PaymentRepository $paymentRepository
    ) {}

    /**
     * Display a listing of customer's payments
     */
    public function index(Request $request): View
    {
        $customer = Auth::guard('customer')->user();

        $filters = [
            'customer_id' => $customer->id,
            'status' => $request->get('status'),
            'invoice_id' => $request->get('invoice_id'),
            'date_from' => $request->get('date_from'),
            'date_to' => $request->get('date_to'),
        ];

        $payments = $this->paymentRepository->getPaginated(15, $filters);

        return view('customer.payments.index', [
            'payments' => $payments,
        ]);
    }

    /**
     * Display the specified payment
     */
    public function show(int $id): View
    {
        $customer = Auth::guard('customer')->user();
        $payment = Payment::with(['invoice', 'customer'])->find($id);

        // Ensure customer can only view their own payments
        if (! $payment || $payment->customer_id !== $customer->id) {
            abort(404);
        }

        return view('customer.payments.show', [
            'payment' => $payment,
        ]);
    }
}
