<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;

class DashboardToolsTest extends TestCase
{
    public function test_integration_status_requires_authentication(): void
    {
        $response = $this->get(route('owner.reports.integration-status'));

        $response->assertRedirect(route('login'));
    }

    public function test_integration_status_returns_json_when_authenticated(): void
    {
        Config::set('integrations.esborder.base_url', 'http://esb.test');
        Config::set('integrations.esborder.api_key', 'k');
        Config::set('integrations.sysreflex.base_url', 'http://reflex.test');
        Config::set('integrations.sysreflex.api_token', 't');

        Http::fake([
            'http://esb.test/api/reports/sales*' => Http::response([
                'summary' => [],
                'totals' => ['order_count' => 0, 'revenue' => 0, 'cogs' => 0, 'gross_profit' => 0],
            ], 200),
            'http://esb.test/api/reports/consolidated*' => Http::response([
                'grand_total' => ['order_count' => 0, 'revenue' => 0, 'cogs' => 0, 'gross_profit' => 0],
            ], 200),
            'http://reflex.test/api/owner/summary*' => Http::response([
                'source' => 'sysreflex',
                'gross_revenue' => 0,
                'transaction_count' => 0,
                'total_commission' => 0,
                'margin_after_commission' => 0,
                'outlet_allocated_share' => 0,
            ], 200),
        ]);

        $user = User::factory()->create();

        $response = $this->actingAs($user)->getJson(route('owner.reports.integration-status'));

        $response->assertOk();
        $response->assertJsonPath('esborder.ok', true);
        $response->assertJsonPath('sysreflex.ok', true);
    }

    public function test_csv_export_requires_authentication(): void
    {
        $response = $this->get(route('owner.reports.export', [
            'date_from' => '2026-04-01',
            'date_to' => '2026-04-30',
        ]));

        $response->assertRedirect(route('login'));
    }

    public function test_csv_export_streams_when_authenticated(): void
    {
        Config::set('integrations.esborder.base_url', 'http://esb.test');
        Config::set('integrations.esborder.api_key', 'k');
        Config::set('integrations.sysreflex.base_url', 'http://reflex.test');
        Config::set('integrations.sysreflex.api_token', 't');
        Config::set('integrations.aggregation_cache_seconds', 0);

        Http::fake([
            'http://esb.test/api/reports/sales*' => Http::response([
                'summary' => [],
                'totals' => ['order_count' => 0, 'revenue' => 0, 'cogs' => 0, 'gross_profit' => 0],
            ], 200),
            'http://esb.test/api/reports/top-items*' => Http::response(['items' => []], 200),
            'http://esb.test/api/reports/consolidated*' => Http::response([
                'date_from' => '2026-04-01',
                'date_to' => '2026-04-30',
                'per_outlet' => [],
                'grand_total' => [
                    'order_count' => 1,
                    'revenue' => 100,
                    'cogs' => 20,
                    'gross_profit' => 80,
                ],
            ], 200),
            'http://reflex.test/api/owner/summary*' => Http::response([
                'source' => 'sysreflex',
                'gross_revenue' => 50,
                'transaction_count' => 2,
                'total_commission' => 10,
                'margin_after_commission' => 40,
                'outlet_allocated_share' => 37.5,
            ], 200),
        ]);

        $user = User::factory()->create();

        $response = $this->actingAs($user)->get(route('owner.reports.export', [
            'date_from' => '2026-04-01',
            'date_to' => '2026-04-30',
        ]));

        $response->assertOk();
        $response->assertHeader('content-type', 'text/csv; charset=UTF-8');
        $this->assertStringContainsString('combined', $response->streamedContent());
    }

    public function test_dashboard_page_renders_when_authenticated(): void
    {
        $user = User::factory()->create();

        $this->actingAs($user)
            ->get('/dashboard')
            ->assertOk()
            ->assertSee('Dashboard');
    }

    public function test_authenticated_root_redirects_to_dashboard(): void
    {
        $user = User::factory()->create();

        $this->actingAs($user)
            ->get('/')
            ->assertRedirect(route('owner.dashboard'));
    }

    public function test_access_guide_requires_authentication(): void
    {
        $this->get(route('owner.access-guide'))
            ->assertRedirect(route('login'));
    }
}
