<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ProductReturn;
use Illuminate\Http\Request;

class ReturnController extends Controller
{
    public function index()
    {
        $returns = ProductReturn::with(['user', 'order'])->latest()->paginate(20);
        return view('admin.returns.index', compact('returns'));
    }

    public function show(ProductReturn $return)
    {
        $return->load(['user', 'order', 'returnItems']);
        return view('admin.returns.show', compact('return'));
    }

    public function updateStatus(Request $request, ProductReturn $return)
    {
        $validated = $request->validate([
            'status' => 'required|in:pending,approved,rejected,processing,refunded',
            'refund_status' => 'nullable|in:pending,processing,completed,failed',
            'admin_notes' => 'nullable|string',
            'refund_amount' => 'nullable|numeric|min:0'
        ]);

        $return->update($validated);

        return redirect()->route('admin.returns.show', $return)
            ->with('success', 'Return status updated successfully');
    }

    public function destroy(ProductReturn $return)
    {
        $return->delete();
        return redirect()->route('admin.returns.index')
            ->with('success', 'Return deleted successfully');
    }
}
