<?php

namespace App\Models;

use App\Enums\PricelistFileStatusEnum;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class PricelistFile extends BaseAuditable
{
    use SoftDeletes;

    protected $casts = [
        'status' => PricelistFileStatusEnum::class,
        'processing_current_step_percentage' => 'integer',
        'available_date' => 'date',
        'workflow_steps' => 'array',
        'file_meta' => 'array',
    ];

    protected static function booted()
    {
        static::creating(function ($pricelistFile) {
            if (empty($pricelistFile->workflow_steps)) {
                $pricelistFile->workflow_steps = \App\Enums\PricelistWorkflowStep::defaultSteps();
            }
        });
    }

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

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

    public function lines(): HasMany
    {
        return $this->hasMany(PricelistFileLine::class);
    }
}
