<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Order;

class CheckoutController extends Controller
{
    public function store(Request $request)
    {
        $cart = session()->get('cart', []);

        if (empty($cart)) {
            return redirect()->route('cart.index')->with('error', 'Keranjang kamu kosong.');
        }

        // Simpan ke database
        Order::create([
            'user_id' => Auth::id(),
            'items' => $cart,
        ]);

        // Kosongkan keranjang
        session()->forget(['cart', 'cart_count']);

        return redirect()->route('cart.index')->with('success', 'Checkout berhasil! Pesanan kamu telah disimpan.');
    }
}
