<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Vote extends Model
{
    protected $fillable = [
        'title', 'description', 'type',
        'related_id', 'status', 'deadline', 'created_by',
    ];

    protected $casts = [
        'deadline' => 'datetime',
    ];

    protected static function booted(): void
    {
        static::creating(function (Vote $vote) {
            $vote->created_by ??= auth()->id();
        });
    }

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

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