<?php

namespace App\Models;

use App\Enums\PricelistFileLineStatusEnum;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class PricelistFileLine extends BaseAuditable
{
    protected $casts = [
        'status' => PricelistFileLineStatusEnum::class,
        'row_number' => 'integer',
        'product_id' => 'integer',
        'product_group_id' => 'integer',
        'producer_id' => 'integer',
        'payload' => 'array',
        'validation_messages' => 'array',
        'diff' => 'array',
    ];

    public function pricelistFile(): BelongsTo
    {
        return $this->belongsTo(PricelistFile::class);
    }

    public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class);
    }

    public function productGroup(): BelongsTo
    {
        return $this->belongsTo(ProductGroup::class);
    }

    public function producer(): BelongsTo
    {
        return $this->belongsTo(Producer::class);
    }

    /**
     * Hibaüzenet hozzáadása a sorhoz
     */
    public function addValidationError(string $field, string $message): void
    {
        $messages = $this->validation_messages ?? [];
        $messages[$field] = $message;
        $this->validation_messages = $messages;
        $this->status = PricelistFileLineStatusEnum::error;
    }
}
