<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
use Vinkla\Hashids\Facades\Hashids;

class Group extends Model
{
    use HasFactory;
    use HasTranslations;

    protected $guarded = ['id'];
    protected $appends = ['hashid'];
    public $translatable = ['title', 'file'];

    protected static function booted()
    {
        static::deleting(function ($group) {
            $group->imports()->delete();
            $group->exports()->delete();
        });
    }

    public function exports()
    {
        return $this->hasMany(Export::class);
    }

    public function imports()
    {
        return $this->hasMany(Import::class);
    }

    public function getFilenameAttribute()
    {
        $locale = app()->getLocale();
        return now()->format('Y-m-d') . "-{$locale}-{$this->type}-{$this->hashid}.xlsx";
    }

    public function getHashidAttribute()
    {
        return Hashids::encode($this->id);
    }
}
