<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Invitation extends Model
{
    use HasFactory;
    
    protected $guarded = ['id'];
    protected $casts = [
        'expires_at' => 'datetime',
        'accepted_at' => 'datetime'
    ];

    public function status(): string
    {
        return !empty($this->accepted_at) ? 'accepted' : 'pending';
    }

    public function isPending(): bool
    {
        return empty($this->accepted_at);
    }

    public function accept(): void
    {
        $this->accepted_at = now();
        $this->save();
    }

    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'invited_by', 'id');
    }

    public function getInviteUrl(): string
    {
        return route('register', ['code' => $this->code]);
    }
}
