<?php

namespace Tests\Feature;

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

class OwnerReportsTabsTest extends TestCase
{
    protected function setUp(): void
    {
        parent::setUp();
        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' => [
                    ['date' => '2026-04-01', 'outlet_id' => 1, 'outlet_name' => 'O', 'order_count' => 1, 'revenue' => 10, 'cogs' => 2, 'gross_profit' => 8],
                ],
                'totals' => ['order_count' => 1, 'revenue' => 10, 'cogs' => 2, 'gross_profit' => 8],
            ], 200),
            'http://esb.test/api/reports/top-items*' => Http::response([
                'items' => [
                    ['product_id' => 1, 'product_name' => 'Nasi', 'qty_sold' => 5, 'revenue' => 50, 'cogs' => 10, 'gross_profit' => 40],
                ],
            ], 200),
            'http://esb.test/api/reports/consolidated*' => Http::response([
                'date_from' => '2026-04-01',
                'date_to' => '2026-04-30',
                'per_outlet' => [
                    ['outlet_id' => 1, 'outlet_name' => 'Outlet A', 'order_count' => 1, 'revenue' => 100, 'cogs' => 20, 'gross_profit' => 80],
                ],
                '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,
                'daily' => [
                    ['date' => '2026-04-01', 'gross_revenue' => 50, 'transaction_count' => 2, 'total_commission' => 10, 'margin_after_commission' => 40],
                ],
                'top_services' => [
                    ['service_id' => 1, 'service_name' => 'Full', 'gross_revenue' => 50, 'transaction_count' => 2, 'total_commission' => 10, 'margin_after_commission' => 40],
                ],
            ], 200),
        ]);
    }

    public function test_report_tabs_return_ok(): void
    {
        $user = User::factory()->create();
        $q = ['date_from' => '2026-04-01', 'date_to' => '2026-04-30'];

        $this->actingAs($user)->get(route('owner.reports', $q))->assertOk()->assertSee('Ringkasan');
        $this->actingAs($user)->get(route('owner.reports.pl', $q))->assertOk()->assertSee('Laba rugi');
        $this->actingAs($user)->get(route('owner.reports.daily', $q))->assertOk()->assertSee('Harian');
        $this->actingAs($user)->get(route('owner.reports.items', $q))->assertOk()->assertSee('Item FnB');
        $this->actingAs($user)->get(route('owner.reports.services', $q))->assertOk()->assertSee('Layanan');
        $this->actingAs($user)->get(route('owner.reports.sources', $q))->assertOk()->assertSee('Detail sumber');
    }

    public function test_daily_tab_supports_pagination_query(): void
    {
        $user = User::factory()->create();
        $q = ['date_from' => '2026-04-01', 'date_to' => '2026-04-30', 'page' => 1];

        $this->actingAs($user)->get(route('owner.reports.daily', $q))->assertOk();
    }
}
