<?php

namespace App\Modules\Inventory\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;


class Warehouse extends Model
{
    /** @use HasFactory<\Database\Factories\WarehouseFactory> */
    use HasFactory;
    use \Spatie\Activitylog\Traits\LogsActivity;

    protected $fillable = [
        'code',
        'name',
        'type',
        'address',
        'description',
        'license_plate',
        'driver_name',
    ];

    protected $casts = [
        'type' => \App\Enums\WarehouseType::class,
    ];

    public function getActivitylogOptions(): \Spatie\Activitylog\LogOptions
    {
        return \Spatie\Activitylog\LogOptions::defaults()
            ->logAll()
            ->logOnlyDirty()
            ->dontSubmitEmptyLogs();
    }

    public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
    {
        $properties = $activity->properties;
        
        $snapshot = $properties['snapshot'] ?? [];
        $snapshot['name'] = $this->name;
        $properties['snapshot'] = $snapshot;

        $activity->properties = $properties;
    }

    public function inventories(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(Inventory::class);
    }



    public function products(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
    {
        return $this->belongsToMany(Product::class, 'inventories')
            ->withPivot(['quantity', 'safety_stock', 'location'])
            ->withTimestamps();
    }
}
