<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

class Agreement extends Model implements HasMedia
{
    use HasFactory, SoftDeletes, InteractsWithMedia;

    protected $fillable = [
        'company_id',
        'corporation_id',
        'name',
        'content',
        'date'
    ];

    protected $casts = [
        'date' => 'date'
    ];

    protected $appends = [
        'has_any_relation'
    ];

    public function company()
    {
        return $this->belongsTo(Company::class);
    }

    public function corporation()
    {
        return $this->belongsTo(Corporation::class);
    }

    public function registerMediaCollections(): void
    {
        $this->addMediaCollection('agreement')
            ->singleFile();
    }

    public function getHasAnyRelationAttribute()
    {
        return $this->company()->exists();
    }
}
