<?php

namespace App\Models;

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

class WebinarRegistration extends Model
{
    use BelongsToOwner;

    protected $table = 'meet_webinar_registrations';

    protected $fillable = [
        'uuid', 'owner_ref', 'room_id', 'email', 'display_name', 'status',
        'access_token', 'approved_at', 'metadata',
    ];

    protected function casts(): array
    {
        return ['approved_at' => 'datetime', 'metadata' => 'array'];
    }

    protected static function booted(): void
    {
        static::creating(function (WebinarRegistration $reg) {
            if (empty($reg->uuid)) {
                $reg->uuid = (string) Str::uuid();
            }
            if (empty($reg->access_token)) {
                $reg->access_token = Str::random(48);
            }
        });
    }

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

    public function room(): BelongsTo
    {
        return $this->belongsTo(Room::class, 'room_id');
    }

    public function isApproved(): bool
    {
        return $this->status === 'approved';
    }
}
