<?php
namespace App\Http\Requests;
use App\Enums\{KeywordDevice,KeywordStatus};use App\Models\Keyword;use App\Support\KeywordTargetOptions;use Illuminate\Foundation\Http\FormRequest;use Illuminate\Validation\{Rule,Validator};
class UpdateKeywordRequest extends FormRequest{public function keyword():?Keyword{return $this->route('customerKeyword')?:$this->route('keyword');}public function authorize():bool{$k=$this->keyword();return $k&&$k->archived_at===null&&$k->status!==KeywordStatus::Archived&&($this->user()?->can('update',$k)??false);}public function rules():array{return ['phrase'=>['required','string','max:190'],'country_code'=>['required',Rule::in(array_keys(KeywordTargetOptions::countries()))],'language_code'=>['required',Rule::in(array_keys(KeywordTargetOptions::languages()))],'device'=>['required',Rule::enum(KeywordDevice::class)],'status'=>['required',Rule::in([KeywordStatus::Active->value,KeywordStatus::Inactive->value])]];}public function withValidator(Validator $v):void{$v->after(function(Validator $v){if($this->filled(['country_code','language_code'])&&!KeywordTargetOptions::supports($this->input('country_code'),$this->input('language_code')))$v->errors()->add('language_code',__('keywords.validation.target_pair'));});}}
