<?php

namespace App\Services\Manage;

use App\Models\StockMutation;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;

class StockMutationService
{
    public function record(
        Model $stockable,
        string $type,
        float|int $quantity,
        float|int $stockBefore,
        float|int $stockAfter,
        ?Model $source = null,
        ?string $stockQuality = null,
        ?string $description = null,
        ?User $user = null,
    ): StockMutation {
        return StockMutation::create([
            'stockable_type' => $stockable->getMorphClass(),
            'stockable_id' => $stockable->id,
            'type' => $type,
            'source_type' => $source?->getMorphClass(),
            'source_id' => $source?->id,
            'quantity' => $quantity,
            'stock_before' => $stockBefore,
            'stock_after' => $stockAfter,
            'stock_quality' => $stockQuality,
            'description' => $description,
            'user_id' => $user?->id ?? auth()->id(),
        ]);
    }
}
