<?php

namespace App\Models;

use App\Events\ProjectDownloadStatus;
use App\Traits\HasStatus;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder;

class ProjectDownload extends Model
{
    use HasFactory;
    use HasStatus;


    protected $fillable = [
        'name',
        'path',
        'status'
    ];

    public function project(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(Project::class);
    }

    public function getDateAttribute():Carbon
    {
        return Carbon::parse(explode('.',$this->name)[0]);
    }

    protected static function booted(): void
    {
        parent::booted();
        self::updated(function (ProjectDownload $projectDownload) {
            event(new ProjectDownloadStatus($projectDownload));
        });
    }
}
