<?php

namespace App\Services;

use App\Models\Article;
use App\Models\ArticlePublication;
use App\Models\Feed;
use App\Models\PlatformAccount;
use App\Models\PlatformChannel;
use App\Models\Route;
use Carbon\Carbon;

class DashboardStatsService
{
    /**
     * @return array<string, mixed>
     */
    public function getStats(string $period = 'today'): array
    {
        $dateRange = $this->getDateRange($period);

        // Get articles fetched for the period
        $articlesFetchedQuery = Article::query();
        if ($dateRange) {
            $articlesFetchedQuery->whereBetween('created_at', $dateRange);
        }
        $articlesFetched = $articlesFetchedQuery->count();

        // Get articles published for the period
        $articlesPublishedQuery = ArticlePublication::query()
            ->whereNotNull('published_at');
        if ($dateRange) {
            $articlesPublishedQuery->whereBetween('published_at', $dateRange);
        }
        $articlesPublished = $articlesPublishedQuery->count();

        // Calculate published percentage
        $publishedPercentage = $articlesFetched > 0 ? round(($articlesPublished / $articlesFetched) * 100, 1) : 0.0;

        return [
            'articles_fetched' => $articlesFetched,
            'articles_published' => $articlesPublished,
            'published_percentage' => $publishedPercentage,
        ];
    }

    /**
     * @return array<string, string>
     */
    public function getAvailablePeriods(): array
    {
        return [
            'today' => 'Today',
            'week' => 'This Week',
            'month' => 'This Month',
            'year' => 'This Year',
            'all' => 'All Time',
        ];
    }

    /**
     * @return array{0: Carbon, 1: Carbon}|null
     */
    private function getDateRange(string $period): ?array
    {
        $now = Carbon::now();

        return match ($period) {
            'today' => [$now->copy()->startOfDay(), $now->copy()->endOfDay()],
            'week' => [$now->copy()->startOfWeek(), $now->copy()->endOfWeek()],
            'month' => [$now->copy()->startOfMonth(), $now->copy()->endOfMonth()],
            'year' => [$now->copy()->startOfYear(), $now->copy()->endOfYear()],
            'all' => null, // No date filtering for all-time stats
            default => [$now->copy()->startOfDay(), $now->copy()->endOfDay()],
        };
    }

    /**
     * @return array<string, int>
     */
    public function getSystemStats(): array
    {
        $totalFeeds = Feed::query()->count();
        $activeFeeds = Feed::query()->where('is_active', 1)->count();
        $totalPlatformAccounts = PlatformAccount::query()->count();
        $activePlatformAccounts = PlatformAccount::query()->where('is_active', 1)->count();
        $totalPlatformChannels = PlatformChannel::query()->count();
        $activePlatformChannels = PlatformChannel::query()->where('is_active', 1)->count();
        $totalRoutes = Route::query()->count();
        $activeRoutes = Route::query()->where('is_active', 1)->count();

        return [
            'total_feeds' => $totalFeeds,
            'active_feeds' => $activeFeeds,
            'total_platform_accounts' => $totalPlatformAccounts,
            'active_platform_accounts' => $activePlatformAccounts,
            'total_platform_channels' => $totalPlatformChannels,
            'active_platform_channels' => $activePlatformChannels,
            'total_routes' => $totalRoutes,
            'active_routes' => $activeRoutes,
        ];
    }
}
