<?php

namespace Tests\Feature\Http\Controllers\Api\V1;

use App\Models\Article;
use App\Models\ArticlePublication;
use App\Models\Feed;
use App\Models\PlatformChannel;
use App\Models\Route;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class DashboardControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_stats_returns_successful_response(): void
    {
        $this
            ->getJson('/api/v1/dashboard/stats')
            ->assertStatus(200)
            ->assertJsonStructure([
                'success',
                'data' => [
                    'article_stats' => [
                        'articles_fetched',
                        'articles_published',
                        'published_percentage',
                    ],
                    'system_stats' => [
                        'total_feeds',
                        'active_feeds',
                        'total_platform_channels',
                        'active_platform_channels',
                        'total_routes',
                        'active_routes',
                    ],
                    'available_periods',
                    'current_period',
                ],
                'message',
            ]);
    }

    public function test_stats_with_different_periods(): void
    {
        $periods = ['today', 'week', 'month', 'year', 'all'];

        foreach ($periods as $period) {
            $this
                ->getJson("/api/v1/dashboard/stats?period={$period}")
                ->assertStatus(200)
                ->assertJson([
                    'success' => true,
                    'data' => [
                        'current_period' => $period,
                    ],
                ]);
        }
    }

    public function test_stats_with_sample_data(): void
    {
        // Get initial counts
        $initialArticles = Article::count();
        $initialFeeds = Feed::count();
        $initialChannels = PlatformChannel::count();
        $initialRoutes = Route::count();
        $initialPublications = ArticlePublication::count();

        // Create test data
        $feed = Feed::factory()->create(['is_active' => true]);
        $channel = PlatformChannel::factory()->create(['is_active' => true]);
        $route = Route::factory()->create(['is_active' => true]);

        // Create articles
        $articles = Article::factory()->count(3)->create(['feed_id' => $feed->id]);

        // Publish one article
        ArticlePublication::factory()->create([
            'article_id' => $articles->first()->id,
            'platform_channel_id' => $channel->id,
            'published_at' => now(),
        ]);

        $response = $this->getJson('/api/v1/dashboard/stats?period=all');

        $response->assertStatus(200)
            ->assertJson([
                'success' => true,
                'data' => [
                    'article_stats' => [
                        'articles_fetched' => $initialArticles + 3,
                        'articles_published' => $initialPublications + 1,
                    ],
                ],
            ]);

        // Just verify structure and that we have more items than we started with
        $responseData = $response->json('data');
        $this->assertGreaterThanOrEqual($initialFeeds + 1, $responseData['system_stats']['total_feeds']);
        $this->assertGreaterThanOrEqual($initialChannels + 1, $responseData['system_stats']['total_platform_channels']);
        $this->assertGreaterThanOrEqual($initialRoutes + 1, $responseData['system_stats']['total_routes']);
    }

    public function test_stats_returns_empty_data_with_no_records(): void
    {
        $this
            ->getJson('/api/v1/dashboard/stats')
            ->assertStatus(200)
            ->assertJson([
                'success' => true,
                'data' => [
                    'article_stats' => [
                        'articles_fetched' => 0,
                        'articles_published' => 0,
                        'published_percentage' => 0.0,
                    ],
                    'system_stats' => [
                        'total_feeds' => 0,
                        'active_feeds' => 0,
                        'total_platform_accounts' => 0,
                        'active_platform_accounts' => 0,
                        'total_platform_channels' => 0,
                        'active_platform_channels' => 0,
                        'total_routes' => 0,
                        'active_routes' => 0,
                    ],
                ],
            ]);
    }
}
