<?php

namespace App\Services;

use App\Models\Feed;
use App\Models\PlatformAccount;
use App\Models\PlatformChannel;
use App\Models\Route;
use App\Models\Setting;
use Illuminate\Support\Facades\Cache;

class OnboardingService
{
    public function needsOnboarding(): bool
    {
        return Cache::remember('onboarding_needed', 300, function () {
            return $this->checkOnboardingStatus();
        });
    }

    public function clearCache(): void
    {
        Cache::forget('onboarding_needed');
    }

    private function checkOnboardingStatus(): bool
    {
        $onboardingSkipped = Setting::where('key', 'onboarding_skipped')->value('value') === 'true';
        $onboardingCompleted = Setting::where('key', 'onboarding_completed')->exists();

        // If skipped or completed, no onboarding needed
        if ($onboardingCompleted || $onboardingSkipped) {
            return false;
        }

        // Check if all components exist
        $hasPlatformAccount = PlatformAccount::where('is_active', true)->exists();
        $hasFeed = Feed::where('is_active', true)->exists();
        $hasChannel = PlatformChannel::where('is_active', true)->exists();
        $hasRoute = Route::where('is_active', true)->exists();

        $hasAllComponents = $hasPlatformAccount && $hasFeed && $hasChannel && $hasRoute;

        return ! $hasAllComponents;
    }
}
