<?php

namespace Tests\Tenant\Controllers;

use App\Facades\Feature;
use Carbon\Carbon;
use Tests\TenantTestCase;
use Tests\UserForTenant;

class DashboardPopularTest extends TenantTestCase
{
    use UserForTenant;

    public function testReturnsVehiclesAndGatesOffersAndNewCarsByFeature(): void
    {
        $response = $this->postPopular();

        $response->assertStatus(200);
        $response->assertJsonStructure(['vehicles' => ['views', 'enquiries']]);
        $this->assertArrayNotHasKey('offers', $response->json());
        $this->assertArrayNotHasKey('new_cars', $response->json());
    }

    public function testIncludesOffersAndNewCarsWhenFeatureEnabled(): void
    {
        Feature::setEnabled(tenant(), ['new-vehicles']);

        $response = $this->postPopular();

        $response->assertStatus(200);
        $response->assertJsonStructure([
            'vehicles' => ['views', 'enquiries'],
            'offers' => ['views', 'enquiries'],
            'new_cars' => ['enquiries'],
        ]);
    }

    private function postPopular()
    {
        return $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->postJson(route('tenant.dashboard.popular'), [
                'from_date' => Carbon::now()->subDays(30)->format('Y-m-d'),
                'to_date' => Carbon::now()->format('Y-m-d'),
            ]);
    }
}
