<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class CartController extends Controller
{
    private $baseUrl;

    public function __construct()
    {
        $this->baseUrl = env('API_BASE_URL');
    }

    private function getToken($request)
    {
        return $request->header('Authorization');
    }

    // ✅ CALCULATE
 public function calculate(Request $request)
{
    $token = $request->header('Authorization');

    $data = $request->json()->all(); // ✅ FIX

    $response = Http::withHeaders([
        'Authorization' => $token,
        'Content-Type' => 'application/json'
    ])->post($this->baseUrl . 'calculate', $data);

    return response()->json($response->json());
}

    // ✅ ADD TO CART
    public function addToCart(Request $request)
    {
        $token = $this->getToken($request);

        $response = Http::withHeaders([
            'Authorization' => $token
        ])->asForm()->post($this->baseUrl . 'cart/add', $request->all());

        return response()->json($response->json());
    }

    // ✅ UPDATE QUANTITY
    public function updateQuantity(Request $request)
    {
        $token = $this->getToken($request);

        $response = Http::withHeaders([
            'Authorization' => $token
        ])->asForm()->post($this->baseUrl . '/cart/update-quantity', $request->all());

        return response()->json($response->json());
    }

    // ✅ GET CART
   public function getCart(Request $request)
{
    $token = $this->getToken($request);

    $response = Http::withHeaders([
        'Authorization' => $token
    ])->get($this->baseUrl . '/cart');

    $cartData = $response->json()['data'] ?? [
        'items' => [],
        'price_details' => [
            'sub_total' => 0,
            'delivery_fee' => 0,
            'discount' => 0,
            'total' => 0
        ]
    ];

    // Pass data to Blade view
    return view('cart', ['cart' => $cartData]);
}

    // ✅ REMOVE CART
    public function removeCart(Request $request, $id)
    {
        $token = $this->getToken($request);

        $response = Http::withHeaders([
            'Authorization' => $token
        ])->delete($this->baseUrl . '/cart/remove/' . $id);

        return response()->json($response->json());
    }

    // ✅ COUPONS
    public function coupons(Request $request)
    {
        $token = $this->getToken($request);

        $response = Http::withHeaders([
            'Authorization' => $token
        ])->get($this->baseUrl . '/coupons');

        return response()->json($response->json());
    }

    // ✅ APPLY COUPON
    public function applyCoupon(Request $request)
    {
        $token = $this->getToken($request);

        $response = Http::withHeaders([
            'Authorization' => $token
        ])->asForm()->post($this->baseUrl . '/applyCoupon', $request->all());

        return response()->json($response->json());
    }

    // ✅ REMOVE COUPON
    public function removeCoupon(Request $request)
    {
        $token = $this->getToken($request);

        $response = Http::withHeaders([
            'Authorization' => $token
        ])->post($this->baseUrl . '/removeCoupon');

        return response()->json($response->json());
    }
}