<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Campaign extends Model
{
    protected $guarded = [];

    protected $casts = [
        'is_active' => 'boolean',
    ];

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

    /** The shareable link that tags every completion with this campaign. */
    public function shareUrl(): string
    {
        return url('/assessments/'.$this->assessment.'?campaign='.$this->slug);
    }

    public static function activeBySlug(?string $slug): ?self
    {
        if (! $slug) {
            return null;
        }

        return static::where('slug', $slug)->where('is_active', true)->first();
    }
}
