<?php

namespace App\Http\Controllers\Waiter;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Deal;
use App\Models\Item;
use App\Models\Kot;
use App\Models\KotItem;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\RestaurantTable;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;

class DashboardController extends Controller
{
    public function index(): View
    {
        return view('waiter.dashboard');
    }

    public function getTables(): JsonResponse
    {
        $branchId = auth()->user()->active_branch_id;
        $tables = RestaurantTable::forBranch($branchId)
            ->with(['activeOrder.items.kotItems'])
            ->get()->map(function ($table) {
                $status = $table->status;
                $hasReadyItems = false;

                if ($table->activeOrder) {
                    foreach ($table->activeOrder->items as $item) {
                        if ($item->kotItems->where('status', 'ready')->count() > 0) {
                            $hasReadyItems = true;
                            break;
                        }
                    }
                }

                return [
                    'id' => $table->id,
                    'name' => $table->name,
                    'status' => $status,
                    'has_ready_items' => $hasReadyItems,
                    'order_id' => $table->activeOrder?->id,
                ];
            });

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

    public function getCategories(): JsonResponse
    {
        $branchId = auth()->user()->active_branch_id;

        return response()->json(Category::forBranch($branchId)->where('is_active', true)->get());
    }

    public function getItems(Request $request): JsonResponse
    {
        $branchId = auth()->user()->active_branch_id;
        $query = Item::forBranch($branchId)->with('variants')->where('is_active', true);

        if ($request->category_id) {
            $query->where('category_id', $request->category_id);
        }

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

    public function getDeals(): JsonResponse
    {
        $branchId = auth()->user()->active_branch_id;
        $currentDay = now()->format('l');

        $deals = Deal::where('branch_id', $branchId)
            ->where('is_active', true)
            ->where(function ($query) use ($currentDay) {
                $query->whereNull('available_days')
                    ->orWhereJsonContains('available_days', $currentDay);
            })
            ->with(['slots.items.item.variants', 'slots.items.variant'])
            ->get();

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

    public function getOrder(RestaurantTable $table): JsonResponse
    {
        $order = Order::with(['items.kotItems', 'items.item', 'items.variant'])
            ->where('table_id', $table->id)
            ->where('status', 'active')
            ->first();

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

    public function submitKot(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'table_id' => 'required|exists:restaurant_tables,id',
            'cart' => 'required|array|min:1',
            'subtotal' => 'required|numeric',
            'gst_amount' => 'required|numeric',
            'grand_total' => 'required|numeric',
        ]);

        return DB::transaction(function () use ($validated) {
            $order = Order::updateOrCreate(
                [
                    'table_id' => $validated['table_id'],
                    'status' => 'active',
                ],
                [
                    'branch_id' => auth()->user()->active_branch_id,
                    'waiter_id' => auth()->id(),
                    'order_type' => 'dine_in',
                    'subtotal' => $validated['subtotal'],
                    'gst_amount' => $validated['gst_amount'],
                    'grand_total' => $validated['grand_total'],
                ]
            );

            $existingItemIds = [];
            foreach ($validated['cart'] as $cartItem) {
                $isDeal = isset($cartItem['deal_id']);
                $orderItem = OrderItem::updateOrCreate(
                    [
                        'order_id' => $order->id,
                        'item_id' => $isDeal ? null : $cartItem['id'],
                        'variant_id' => $cartItem['variant_id'] ?? null,
                        'deal_id' => $cartItem['deal_id'] ?? null,
                    ],
                    [
                        'name' => $cartItem['name'],
                        'price' => $cartItem['price'],
                        'quantity' => $cartItem['qty'],
                        'subtotal' => $cartItem['price'] * $cartItem['qty'],
                        'notes' => $cartItem['notes'] ?? null,
                    ]
                );
                $existingItemIds[] = $orderItem->id;

                // Handle selections (items inside a deal)
                if (isset($cartItem['selections']) && is_array($cartItem['selections'])) {
                    foreach ($cartItem['selections'] as $selection) {
                        // Create a separate KotItem entry for each selection later in the KOT phase
                        // We store these in a temporary way or handle during KOT creation
                    }
                }
            }

            // Sync: Remove items not in cart that don't have KOTs
            $order->items()->whereNotIn('id', $existingItemIds)
                ->whereDoesntHave('kotItems')
                ->delete();

            // Recalculate order totals based on ALL items currently in the order
            $orderSubtotal = $order->items()->sum('subtotal');
            $orderGstAmount = $orderSubtotal * 0.05; // 5% GST
            $orderGrandTotal = $orderSubtotal + $orderGstAmount;

            $order->update([
                'subtotal' => $orderSubtotal,
                'gst_amount' => $orderGstAmount,
                'grand_total' => $orderGrandTotal,
            ]);

            RestaurantTable::where('id', $validated['table_id'])->update(['status' => 'occupied']);

            // Now handle incremental KOT
            $itemsToSend = [];
            $order->load('items.kotItems');
            foreach ($order->items as $item) {
                $alreadySentQty = $item->kotItems->sum('quantity');
                $qtyToSend = $item->quantity - $alreadySentQty;

                if ($qtyToSend > 0) {
                    // Check if this is a deal
                    $cartItem = collect($validated['cart'])->first(function ($c) use ($item) {
                        if ($item->deal_id) {
                            return isset($c['deal_id']) && $c['deal_id'] == $item->deal_id;
                        }

                        return $c['id'] == $item->item_id;
                    });

                    if ($item->deal_id && isset($cartItem['selections'])) {
                        foreach ($cartItem['selections'] as $selection) {
                            $itemsToSend[] = [
                                'order_item_id' => $item->id,
                                'item_id' => $selection['item_id'],
                                'name_override' => $selection['name'],
                                'quantity' => $qtyToSend,
                                'notes' => $item->notes,
                                'deal_id' => $item->deal_id,
                            ];
                        }
                    } else {
                        $itemsToSend[] = [
                            'order_item_id' => $item->id,
                            'quantity' => $qtyToSend,
                            'notes' => $item->notes,
                        ];
                    }
                }
            }

            if (empty($itemsToSend)) {
                return response()->json(['success' => true, 'order_id' => $order->id, 'message' => 'Nothing new.']);
            }

            $kot = Kot::create([
                'branch_id' => $order->branch_id,
                'order_id' => $order->id,
                'waiter_id' => auth()->id(),
                'status' => 'pending',
            ]);

            foreach ($itemsToSend as $itemData) {
                KotItem::create([
                    'kot_id' => $kot->id,
                    'order_item_id' => $itemData['order_item_id'],
                    'item_id' => $itemData['item_id'] ?? $item->item_id, // Track specific item
                    'quantity' => $itemData['quantity'],
                    'notes' => ($itemData['name_override'] ?? '').' '.($itemData['notes'] ?? ''),
                    'status' => 'pending',
                    'deal_id' => $itemData['deal_id'] ?? null,
                ]);
            }

            return response()->json(['success' => true, 'order_id' => $order->id]);
        });
    }

    public function serveItem(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'order_item_id' => 'required|exists:order_items,id',
        ]);

        KotItem::where('order_item_id', $validated['order_item_id'])
            ->where('status', 'ready')
            ->update(['status' => 'served']);

        return response()->json(['success' => true]);
    }
}
