<?php

namespace App\Http\Controllers;

use App\Models\Order;
use App\Models\Product;
use Illuminate\Http\Request;
use Stripe\Stripe;
use Stripe\StripeClient;

class OrderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    public function checkout(Request $request){
        $stripe = new StripeClient('sk_test_51MDkt0IiNXZoTzKZj271c5xzWI4NeOufK3GSJ6fiRqvcJKNgzXJYriBt9cRVyFY4krvMpDClfIMmhV8dUMlpG17J00tYb8Rh98');
        $products=Product::find([1,2,3]);
        $array=[];
        foreach($products as $product){
          
           array_push($array, array([
            'price_data' => [
                'currency' => 'usd',
                'product_data' => ['name' => $product->name_en],
                'unit_amount' => $product->selling_price,
            ],
            'quantity' => 2
        ]));
    }
     

        

// return $array;

        $checkout_session = $stripe->checkout->sessions->create([
            'line_items' => $array

            //     [
            //     'price_data' => ['currency' => 'usd',
            //     'product_data' => ['name' =>'elias',],
            //     'unit_amount' => 100,
            //     ],
            //     'quantity' => 1
            // ],
            // [
            //     'price_data' => ['currency' => 'usd',
            //     'product_data' => ['name' =>'celina',],
            //     'unit_amount' => 100,
            //     ],
            //     'quantity' => 1
            // ],
            // [
            //     'price_data' => ['currency' => 'usd',
            //     'product_data' => ['name' =>'Hla',],
            //     'unit_amount' => 100,
            //     ],
            //     'quantity' => 1
            // ],
        ,
        'mode' => 'payment',
        'success_url' => 'http://localhost:8000/api/success',
        'cancel_url' => 'http://localhost:8000/api/cancel',
        ]);
        return redirect($checkout_session->url);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Order  $order
     * @return \Illuminate\Http\Response
     */
    public function show(Order $order)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Order  $order
     * @return \Illuminate\Http\Response
     */
    public function edit(Order $order)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Order  $order
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Order $order)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Order  $order
     * @return \Illuminate\Http\Response
     */
    public function destroy(Order $order)
    {
        //
    }
}
