<?php

declare(strict_types=1);

namespace App\Models;

use App\Models\Concerns\HasDberpAuditTrail;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class DberpInventoryReplenishmentSuggestion extends Model
{
    use HasDberpAuditTrail;

    protected $table = 'dberp_inventory_replenishment_suggestions';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'product_id',
        'warehouse_id',
        'system_suggested_quantity',
        'adjusted_quantity',
        'trigger_reason',
        'status',
        'generated_at',
        'processed_at',
        'metric_snapshot',
        'remark',
    ];

    protected function casts(): array
    {
        return [
            'product_id' => 'integer',
            'warehouse_id' => 'integer',
            'system_suggested_quantity' => 'decimal:4',
            'adjusted_quantity' => 'decimal:4',
            'generated_at' => 'datetime',
            'processed_at' => 'datetime',
            'metric_snapshot' => 'array',
        ];
    }

    public function getDberpAuditActionPrefix(): string
    {
        return 'dberp.inventory_replenishment_suggestion';
    }

    /**
     * @return list<string>
     */
    public function getDberpAuditFields(): array
    {
        return $this->getFillable();
    }

    public function product(): BelongsTo
    {
        return $this->belongsTo(DberpProduct::class, 'product_id');
    }

    public function warehouse(): BelongsTo
    {
        return $this->belongsTo(DberpWarehouse::class, 'warehouse_id');
    }

    public function markReviewed(?string $remark = null): self
    {
        $this->forceFill([
            'status' => 'reviewed',
            'remark' => $remark ?: $this->remark,
        ])->save();

        return $this->refresh();
    }

    public function markProcessed(?string $remark = null): self
    {
        $this->forceFill([
            'status' => 'processed',
            'processed_at' => now(),
            'remark' => $remark ?: $this->remark,
        ])->save();

        return $this->refresh();
    }

    public function markIgnored(?string $remark = null): self
    {
        $this->forceFill([
            'status' => 'ignored',
            'remark' => $remark ?: $this->remark,
        ])->save();

        return $this->refresh();
    }
}