<?php

namespace Modules\ChipOrder\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\ChipOrder\Models\ChipOrder;
use Modules\ChipOrder\Requests\Api\ChipOrderRequest;
use Modules\ChipOrder\Resources\Api\ChipOrderResource;
use Modules\ChipOrder\Services\ChipOrderService;
use Modules\Payment\Services\PaymentService;


class ChipOrderController extends Controller
{

    public function __construct(
        private ChipOrderService $chipOrderService,
        private PaymentService $paymentService,
    ) {
    }

    /**
     * Get Orders.
     * @param Request $request
     * @return JsonResponse
     */
    public function index(Request $request): void
    {


        $credentials['orderBy']['key'] = "updated_at";
        $credentials['orderBy']['value'] = $request->sort ?? "desc";
        $credentials['api'] = true;
        $credentials['user_id'] = auth()->id();
        $query = $this->chipOrderService->get($credentials);

        $response = [
            "query" => $query,
            "request" => $request,
            "resourceClass" => ChipOrderResource::class,
            "returnName" => "orders"
        ];
        ResponseService::fetchPainateDate($response);
    }
    /**
     * Get Order.
     * @param Request $request
     * @param ChipOrder $chipOrder
     * @return JsonResponse
     */
    public function show(Request $request, ChipOrder $chipOrder)
    {



        $order = $this->chipOrderService->getItem($chipOrder->id);

        $response = [
            'status' => 200,
            'message' => '',
            'data' => new ChipOrderResource($order)
        ];
        return ResponseService::response($response);
    }


    /**
     * Create Order.
     * @param ChipOrderRequest $request
     * @return JsonResponse
     */
    public function createOrder(ChipOrderRequest $request)
    {

        $credentials = $request->only('address_id', 'products');
        $user = auth()->user();
        $credentials['user_id'] = $user->id;
        $total = $this->chipOrderService->getTotal($credentials);
        $orderId = uniqid('ORD_');

        $payment = $this->paymentService->createPayment(
            orderId: $orderId,
            amount: $total,
            currency: 'SAR',
            customer: [
                'name' => ($user->name ?? '') . ($user->last_name ?? ''),
                'email' => $user->email ?? '',
                'phone' => $user->phone ?? '',
            ],
            meta: $credentials,
        );

        if (isset($payment['order']['url'])) {
            session()->put('telr_ref', $payment['order']['ref']); // Save for status check
            $response = [
                'status' => 200,
                'message' => '',
                'data' => $payment['order']['url']
            ];
            return ResponseService::response($response);
        }
        return ResponseService::response([
            'status' => 400,
            'message' => $payment['error']['message'] ?? 'Failed to create payment',
        ]);
    }
    public function store(ChipOrderRequest $request)
    {

        $credentials = $request->only('address_id', 'products');
        $order = $this->chipOrderService->create($credentials);
        $response = [
            'status' => 200,
            'message' => '',
            'data' => new ChipOrderResource($order)
        ];
        return ResponseService::response($response);
    }


}
