<?php
namespace App\Models;
use App\Enums\{CheckFrequency,KeywordDevice,KeywordStatus,SearchEngine};use Illuminate\Database\Eloquent\{Builder,Model};use Illuminate\Database\Eloquent\Concerns\HasUlids;use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Relations\{BelongsTo,HasMany};
class Keyword extends Model {use HasFactory,HasUlids;protected $fillable=['phrase','normalized_phrase','search_engine','country_code','language_code','device'];protected function casts():array{return ['search_engine'=>SearchEngine::class,'device'=>KeywordDevice::class,'status'=>KeywordStatus::class,'archived_at'=>'datetime','tracking_enabled'=>'boolean','check_frequency'=>CheckFrequency::class,'next_check_at'=>'datetime','last_queued_at'=>'datetime'];}public function uniqueIds():array{return ['ulid'];}public function getRouteKeyName():string{return 'ulid';}public function project():BelongsTo{return $this->belongsTo(Project::class);}public function serpChecks():HasMany{return $this->hasMany(SerpCheck::class);}public function scopeForProject(Builder $q,Project|int $p):Builder{return $q->where('project_id',$p instanceof Project?$p->id:$p);}public function scopeActive(Builder $q):Builder{return $q->where('status',KeywordStatus::Active)->whereNull('archived_at');}public function scopeArchived(Builder $q):Builder{return $q->where('status',KeywordStatus::Archived)->whereNotNull('archived_at');}public function archive():bool{return $this->forceFill(['status'=>KeywordStatus::Archived,'archived_at'=>now()])->save();}public function restoreKeyword():bool{return $this->forceFill(['status'=>KeywordStatus::Active,'archived_at'=>null])->save();}}