<?php

namespace App\Services;

use App\Models\Feed;
use App\Models\PlatformChannel;
use App\Models\Route;
use App\Models\Setting;

class SystemStatusService
{
    /**
     * @return array{is_enabled: bool, status: string, status_class: string, reasons: array<int, string>}
     */
    public function getSystemStatus(): array
    {
        $reasons = [];
        $isEnabled = true;

        if (! Setting::isArticleProcessingEnabled()) {
            $isEnabled = false;
            $reasons[] = 'Manually disabled by user';
        }

        if (! Feed::where('is_active', true)->exists()) {
            $isEnabled = false;
            $reasons[] = 'No active feeds configured';
        }

        if (! PlatformChannel::where('is_active', true)->exists()) {
            $isEnabled = false;
            $reasons[] = 'No active platform channels configured';
        }

        if (! Route::where('is_active', true)->exists()) {
            $isEnabled = false;
            $reasons[] = 'No active feed-to-channel routes configured';
        }

        return [
            'is_enabled' => $isEnabled,
            'status' => $isEnabled ? 'Enabled' : 'Disabled',
            'status_class' => $isEnabled ? 'text-green-600' : 'text-red-600',
            'reasons' => $reasons,
        ];
    }

    public function canProcessArticles(): bool
    {
        return $this->getSystemStatus()['is_enabled'];
    }
}
