<?php

namespace App\Http\Controllers;

use App\Models\Payment;
use App\Models\Student;
use App\Models\Fee;
use Illuminate\Http\Request;

class PaymentController extends Controller
{
    public function index()
    {
        $payments = Payment::with(['student', 'fee'])->latest()->paginate(20);
        return view('admin.payments.index', compact('payments'));
    }

    public function create()
    {
        $students = Student::all();
        $fees = Fee::all();
        return view('admin.payments.create', compact('students', 'fees'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'student_id' => 'required|exists:students,id',
            'fee_id' => 'required|exists:fees,id',
            'amount' => 'required|numeric|min:0',
            'payment_method' => 'required|string',
            'payment_date' => 'required|date',
            'reference' => 'required|string|unique:payments',
            'receipt_number' => 'required|string|unique:payments',
            'status' => 'required|in:pending,confirmed,failed',
            'notes' => 'nullable|string',
        ]);

        Payment::create($validated);

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

    public function show(Payment $payment)
    {
        return view('admin.payments.show', compact('payment'));
    }

    public function edit(Payment $payment)
    {
        $students = Student::all();
        $fees = Fee::all();
        return view('admin.payments.edit', compact('payment', 'students', 'fees'));
    }

    public function update(Request $request, Payment $payment)
    {
        $validated = $request->validate([
            'student_id' => 'required|exists:students,id',
            'fee_id' => 'required|exists:fees,id',
            'amount' => 'required|numeric|min:0',
            'payment_method' => 'required|string',
            'payment_date' => 'required|date',
            'reference' => 'required|string|unique:payments,reference,' . $payment->id,
            'receipt_number' => 'required|string|unique:payments,receipt_number,' . $payment->id,
            'status' => 'required|in:pending,confirmed,failed',
            'notes' => 'nullable|string',
        ]);

        $payment->update($validated);

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

    public function destroy(Payment $payment)
    {
        $payment->delete();
        return redirect()->route('admin.payments.index')->with('success', 'Payment deleted successfully!');
    }

    // Student Portal: View my fees
    public function studentFees()
    {
        $user = auth()->user();
        $student = $user->student;
        $payments = Payment::where('student_id', $student->id)->with('fee')->get();
        return view('student.fees', compact('payments', 'student'));
    }

    // Student Portal: Pay fee (placeholder for payment gateway)
    public function pay(Request $request)
    {
        // Integrate Paystack/Flutterwave here
        return redirect()->back()->with('success', 'Payment feature coming soon!');
    }
}