<?php

namespace App\Models;

use App\Models\Concerns\BelongsToOwner;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;

class MeetPoll extends Model
{
    use BelongsToOwner;

    protected $table = 'meet_polls';

    protected $fillable = [
        'uuid', 'owner_ref', 'session_id', 'question', 'options', 'status',
    ];

    protected function casts(): array
    {
        return ['options' => 'array'];
    }

    protected static function booted(): void
    {
        static::creating(function (MeetPoll $poll) {
            if (empty($poll->uuid)) {
                $poll->uuid = (string) Str::uuid();
            }
        });
    }

    public function getRouteKeyName(): string
    {
        return 'uuid';
    }

    public function session(): BelongsTo
    {
        return $this->belongsTo(Session::class, 'session_id');
    }

    public function votes(): HasMany
    {
        return $this->hasMany(PollVote::class, 'poll_id');
    }
}
