<?php

namespace App\Http\Controllers\Api\V1\Showroom;

use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Services\Showroom\ShowroomPosService;
use Illuminate\Http\Request;

class PosController extends Controller
{
    public function __construct(private readonly ShowroomPosService $posService)
    {
    }

    public function quote(Request $request)
    {
        $data = $request->validate([
            'customer_id' => 'nullable|integer',
            'point_redeem_points' => 'nullable|integer|min:0',
            'items' => 'required|array|min:1',
            'items.*.item_id' => 'required|integer',
            'items.*.quantity' => 'required|integer|min:1',
            'items.*.variation_type' => 'nullable|string|max:191',
            'items.*.unit_price' => 'nullable|numeric|min:0',
            'items.*.discount_amount' => 'nullable|numeric|min:0',
            'items.*.tax_amount' => 'nullable|numeric|min:0',
        ]);

        return response()->json($this->posService->quote($data));
    }

    public function sale(Request $request)
    {
        $data = $request->validate([
            'customer_id' => 'nullable|integer',
            'customer_mobile' => 'required_without:customer_id|string|max:40',
            'customer_name' => 'required_without:customer_id|string|max:191',
            'store_id' => 'nullable|integer',
            'module_id' => 'nullable|integer',
            'showroom_id' => 'nullable|string|max:191',
            'idempotency_key' => 'nullable|string|max:160',
            'point_redeem_points' => 'nullable|integer|min:0',
            'items' => 'required|array|min:1',
            'items.*.item_id' => 'required|integer',
            'items.*.quantity' => 'required|integer|min:1',
            'items.*.variation_type' => 'nullable|string|max:191',
            'items.*.unit_price' => 'nullable|numeric|min:0',
            'items.*.discount_amount' => 'nullable|numeric|min:0',
            'items.*.tax_amount' => 'nullable|numeric|min:0',
            'payments' => 'required|array|min:1',
            'payments.*.payment_method' => 'required|string|in:cash,bkash,nagad,bank,card,bank_card,bangla_qr',
            'payments.*.amount' => 'required|numeric|min:0.01',
            'payments.*.transaction_ref' => 'nullable|string|max:191',
            'payments.*.payment_transaction_id' => 'nullable|integer',
            'payments.*.provider_name' => 'nullable|string|max:80',
            'payments.*.provider_transaction_id' => 'nullable|string|max:160',
            'payments.*.qr_reference' => 'nullable|string|max:160',
        ]);

        $order = $this->posService->createSale(
            $data,
            $request->attributes->get('showroom_admin'),
            $request->attributes->get('showroom_device')
        );

        return response()->json([
            'message' => translate('messages.order_placed_successfully'),
            'order_id' => $order->id,
            'invoice_no' => 'POS-' . $order->id,
            'order' => $order,
        ], 201);
    }

    public function history(Request $request)
    {
        $admin = $request->attributes->get('showroom_admin');
        $orders = Order::with(['customer', 'payments'])
            ->where('sale_channel', 'showroom')
            ->when((int) $admin->role_id !== 1, fn ($query) => $query->where('pos_created_by_admin_id', $admin->id))
            ->latest()
            ->paginate(min((int) $request->query('limit', 20), 100));

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

    public function invoice(Order $order)
    {
        abort_if($order->sale_channel !== 'showroom', 404);

        return response()->json($order->load(['customer', 'details', 'payments']));
    }
}
