<?php

namespace App\Traits;

use App\Models\PartHistory;

trait LogsPartHistory
{
    protected static function bootLogsPartHistory()
    {
        static::created(function ($part) {
            PartHistory::logEvent(
                $part->id,
                PartHistory::EVENT_CREATED,
                [
                    'new_values' => $part->toArray(),
                    'notes' => 'Part created',
                ]
            );
        });

        static::updated(function ($part) {
            $changes = $part->getChanges();
            $original = $part->getOriginal();
            
            if (empty($changes)) {
                return;
            }

            // Track quantity changes separately
            if (isset($changes['quantity_on_hand'])) {
                $oldQuantity = $original['quantity_on_hand'] ?? 0;
                $newQuantity = $changes['quantity_on_hand'];
                $quantityChange = $newQuantity - $oldQuantity;

                PartHistory::logEvent(
                    $part->id,
                    $quantityChange > 0 ? PartHistory::EVENT_STOCK_IN : PartHistory::EVENT_STOCK_OUT,
                    [
                        'quantity_change' => $quantityChange,
                        'quantity_before' => $oldQuantity,
                        'quantity_after' => $newQuantity,
                        'notes' => 'Stock quantity adjusted',
                        'reference_type' => 'manual_adjustment',
                    ]
                );
            }

            // Track price changes
            if (isset($changes['cost_price']) || isset($changes['sell_price'])) {
                PartHistory::logEvent(
                    $part->id,
                    PartHistory::EVENT_PRICE_CHANGE,
                    [
                        'old_values' => array_intersect_key($original, $changes),
                        'new_values' => $changes,
                        'cost_before' => $original['cost_price'] ?? null,
                        'cost_after' => $changes['cost_price'] ?? $part->cost_price,
                        'notes' => 'Price updated',
                    ]
                );
            }

            // Track supplier changes
            if (isset($changes['supplier_id'])) {
                PartHistory::logEvent(
                    $part->id,
                    PartHistory::EVENT_SUPPLIER_CHANGE,
                    [
                        'old_values' => ['supplier_id' => $original['supplier_id']],
                        'new_values' => ['supplier_id' => $changes['supplier_id']],
                        'notes' => 'Supplier changed',
                    ]
                );
            }

            // Track other general updates
            $generalChanges = array_diff_key($changes, [
                'quantity_on_hand' => null,
                'cost_price' => null,
                'sell_price' => null,
                'supplier_id' => null,
                'updated_at' => null,
            ]);

            if (!empty($generalChanges)) {
                PartHistory::logEvent(
                    $part->id,
                    PartHistory::EVENT_UPDATED,
                    [
                        'old_values' => array_intersect_key($original, $generalChanges),
                        'new_values' => $generalChanges,
                        'notes' => 'Part information updated',
                    ]
                );
            }
        });

        static::deleted(function ($part) {
            PartHistory::logEvent(
                $part->id,
                PartHistory::EVENT_DELETED,
                [
                    'old_values' => $part->toArray(),
                    'notes' => 'Part deleted',
                ]
            );
        });
    }
}
