<?php

namespace App\Http\Controllers\Panel;

use App\Http\Controllers\Controller;
use App\Models\Order;
use Illuminate\Support\Facades\Auth;

class OrderController extends Controller
{
    public function index()
    {
        $orders = Auth::user()
            ->orders()
            ->with([
                'items.product',
                'cancelRequests' => fn ($q) => $q->where('status', 'pending'),
            ])
            ->latest()
            ->paginate(10);

        return view('panel.orders.index', compact('orders'));
    }

    public function show(Order $order)
    {
        if ($order->user_id !== Auth::id()) {
            abort(403);
        }

        $order->load([
            'items.product',
            'transactions',
            'cancelRequests',
            'shippingMethod',
        ]);

        $cancelRequest = $order->cancelRequests->first();

        return view('panel.orders.show', compact('order', 'cancelRequest'));
    }
}
