<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Mtc\MercuryDataModels\Factories\ImportMapFactory;
use Mtc\MercuryDataModels\Traits\EnsuresSlug;
use OwenIt\Auditing\Contracts\Auditable;

class ImportMap extends Model implements Auditable
{
    use HasFactory;
    use EnsuresSlug;
    use \OwenIt\Auditing\Auditable;

    protected $fillable = [
        'name',
        'slug',
        'type',
        'data',
    ];

    protected $casts = [
        'data' => 'array',
    ];

    #[\Override]
    protected static function boot()
    {
        parent::boot();

        self::saving(function (self $dealership) {
            $dealership->ensureSlug();
        });
    }

    protected static function newFactory()
    {
        return ImportMapFactory::new();
    }

    public function fields(): HasMany
    {
        return $this->hasMany(ImportMapField::class, 'map_id');
    }

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