<?php

namespace App\Modules\Integration\Actions;

use App\Modules\Integration\Models\SalesOrder;
use App\Modules\Integration\Models\SalesOrderItem;
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
use App\Modules\Inventory\Contracts\ProductServiceInterface;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;
use Illuminate\Validation\ValidationException;

class SyncOrderAction
{
    protected $inventoryService;
    protected $productService;

    public function __construct(
        InventoryServiceInterface $inventoryService,
        ProductServiceInterface $productService
    ) {
        $this->inventoryService = $inventoryService;
        $this->productService = $productService;
    }

    /**
     * 執行訂單同步
     *
     * @param array $data
     * @return array 包含 orders 建立結果的資訊
     * @throws ValidationException
     * @throws \Exception
     */
    public function execute(array $data)
    {
        $externalOrderId = $data['external_order_id'];

        // 使用 Cache::lock 防護高併發，鎖定該訂單號 10 秒
        // 此處需要 cache store 支援鎖 (如 memcached, dynamodb, redis, database, file, array)
        // Laravel 預設的 file/redis 都支援。若無法取得鎖，表示有另一個相同的請求正在處理
        $lock = Cache::lock("sync_order_{$externalOrderId}", 10);

        if (!$lock->get()) {
            throw ValidationException::withMessages([
                'external_order_id' => ["The order {$externalOrderId} is currently being processed by another transaction. Please try again later."]
            ]);
        }

        try {
            // 冪等性處理：若訂單已存在，回傳已建立的訂單資訊
            $existingOrder = SalesOrder::where('external_order_id', $externalOrderId)->first();
            if ($existingOrder) {
                return [
                    'status' => 'exists',
                    'message' => 'Order already exists',
                    'order_id' => $existingOrder->id,
                ];
            }

            // --- 預檢 (Pre-flight check) N+1 優化 ---
            $items = $data['items'];
            $posProductIds = array_column($items, 'pos_product_id');
            
            // 一次性查出所有相關的 Product
            $products = $this->productService->findByExternalPosIds($posProductIds)->keyBy('external_pos_id');
            
            $missingIds = [];
            foreach ($posProductIds as $id) {
                if (!$products->has($id)) {
                    $missingIds[] = $id;
                }
            }

            if (!empty($missingIds)) {
                // 回報所有缺漏的 ID
                throw ValidationException::withMessages([
                    'items' => ["The following products are not found: " . implode(', ', $missingIds) . ". Please sync products first."]
                ]);
            }

            // --- 執行寫入交易 ---
            $result = DB::transaction(function () use ($data, $items, $products) {
                // 1. 建立訂單
                $order = SalesOrder::create([
                    'external_order_id' => $data['external_order_id'],
                    'status' => 'completed',
                    'payment_method' => $data['payment_method'] ?? 'cash',
                    'total_amount' => 0,
                    'sold_at' => $data['sold_at'] ?? now(),
                    'raw_payload' => $data,
                    'source' => $data['source'] ?? 'pos',
                    'source_label' => $data['source_label'] ?? null,
                ]);

                // 2. 查找倉庫
                $warehouseCode = $data['warehouse_code'];
                $warehouses = $this->inventoryService->getWarehousesByCodes([$warehouseCode]);
                
                if ($warehouses->isEmpty()) {
                    throw ValidationException::withMessages([
                        'warehouse_code' => ["Warehouse with code {$warehouseCode} not found."]
                    ]);
                }
                $warehouseId = $warehouses->first()->id;

                $totalAmount = 0;

                // 3. 處理訂單明細
                $orderItemsData = [];
                foreach ($items as $itemData) {
                    $product = $products->get($itemData['pos_product_id']);
                    
                    $qty = $itemData['qty'];
                    $price = $itemData['price'];
                    $lineTotal = $qty * $price;
                    $totalAmount += $lineTotal;

                    $orderItemsData[] = [
                        'sales_order_id' => $order->id,
                        'product_id' => $product->id,
                        'product_name' => $product->name,
                        'quantity' => $qty,
                        'price' => $price,
                        'total' => $lineTotal,
                        'created_at' => now(),
                        'updated_at' => now(),
                    ];

                    // 4. 扣除庫存（強制模式，允許負庫存）
                    $this->inventoryService->decreaseStock(
                        $product->id,
                        $warehouseId,
                        $qty,
                        "POS Order: " . $order->external_order_id,
                        true
                    );
                }

                // Batch insert order items
                SalesOrderItem::insert($orderItemsData);

                $order->update(['total_amount' => $totalAmount]);

                return [
                    'status' => 'created',
                    'message' => 'Order synced and stock deducted successfully',
                    'order_id' => $order->id,
                ];
            });

            return $result;
        } finally {
            // 無論成功失敗，最後釋放鎖定
            $lock->release();
        }
    }
}
