<?php

namespace App\Observers;

use App\Models\OrderItem;
use App\Services\StockAdjustmentService;

class OrderItemObserver
{
    public static bool $enableInConsole = false;

    public function created(OrderItem $item): void
    {
        if (StockAdjustmentService::$skipObserver) {
            return;
        }

        if (app()->runningInConsole() && !self::$enableInConsole) {
            return;
        }

        $order = $item->order;
        if (!$order || in_array($order->status, ['cancelled', 'returned'])) {
            return;
        }

        // Deduct stock for the new item
        if ($item->product_variant_id && $item->variant) {
            StockAdjustmentService::adjust($item->variant, -$item->quantity, 'order_placed', [
                'order_id' => $order->id,
                'notes' => "Deducted variant stock (Order item #{$item->id} added in admin panel)",
            ]);
        }
        if ($item->product) {
            StockAdjustmentService::adjust($item->product, -$item->quantity, 'order_placed', [
                'order_id' => $order->id,
                'notes' => "Deducted product stock (Order item #{$item->id} added in admin panel)",
            ]);
        }
    }

    public function updated(OrderItem $item): void
    {
        if (StockAdjustmentService::$skipObserver) {
            return;
        }

        if (app()->runningInConsole() && !self::$enableInConsole) {
            return;
        }

        $order = $item->order;
        if (!$order || in_array($order->status, ['cancelled', 'returned'])) {
            return;
        }

        if ($item->wasChanged('quantity')) {
            $oldQty = (int) $item->getOriginal('quantity');
            $newQty = (int) $item->quantity;
            $difference = $newQty - $oldQty;

            if ($difference !== 0) {
                // Adjust by negative difference (e.g. increase in quantity results in stock deduction)
                if ($item->product_variant_id && $item->variant) {
                    StockAdjustmentService::adjust($item->variant, -$difference, 'order_placed', [
                        'order_id' => $order->id,
                        'notes' => "Adjusted variant stock (Order item #{$item->id} quantity updated {$oldQty} -> {$newQty} in admin panel)",
                    ]);
                }
                if ($item->product) {
                    StockAdjustmentService::adjust($item->product, -$difference, 'order_placed', [
                        'order_id' => $order->id,
                        'notes' => "Adjusted product stock (Order item #{$item->id} quantity updated {$oldQty} -> {$newQty} in admin panel)",
                    ]);
                }
            }
        }
    }

    public function deleted(OrderItem $item): void
    {
        if (StockAdjustmentService::$skipObserver) {
            return;
        }

        if (app()->runningInConsole() && !self::$enableInConsole) {
            return;
        }

        $order = $item->order;
        if (!$order || in_array($order->status, ['cancelled', 'returned'])) {
            return;
        }

        // Restore stock since item is removed from an active order
        if ($item->product_variant_id && $item->variant) {
            StockAdjustmentService::adjust($item->variant, $item->quantity, 'order_cancelled', [
                'order_id' => $order->id,
                'notes' => "Restored variant stock (Order item #{$item->id} removed in admin panel)",
            ]);
        }
        if ($item->product) {
            StockAdjustmentService::adjust($item->product, $item->quantity, 'order_cancelled', [
                'order_id' => $order->id,
                'notes' => "Restored product stock (Order item #{$item->id} removed in admin panel)",
            ]);
        }
    }
}
