enabled(TenantServiceKey::AiGuestMail, $tenant)) { return null; } $service = self::service($tenant); if ($service === null || $service->check_status === self::INVALID) { return null; } return self::fromService($service); } /** * The access as configured, regardless of the last check (for the check itself). */ public static function fromService(TenantService $service): ?self { $settings = (array) $service->settings; $provider = AiProvider::tryFrom((string) ($settings['provider'] ?? '')); $small = trim((string) ($settings['small_model'] ?? '')); $review = trim((string) ($settings['review_model'] ?? '')); if ($provider === null || blank($service->api_key) || $small === '' || $review === '') { return null; } return new self($provider, (string) $service->api_key, $small, $review); } public static function service(Tenant $tenant): ?TenantService { return TenantService::query()->where('tenant_id', $tenant->id)->where('service', TenantServiceKey::AiGuestMail->value)->first(); } /** * The provider rejected the key: the extraction stays off until the landlord saves or checks the access again. */ public static function markInvalid(Tenant $tenant, string $message): void { self::service($tenant)?->forceFill(['check_status' => self::INVALID, 'check_message' => mb_substr($message, 0, 500), 'checked_at' => now()])->save(); } public function __debugInfo(): array { return ['provider' => $this->provider->value, 'key' => '***', 'smallModel' => $this->smallModel, 'reviewModel' => $this->reviewModel]; } }