<?php

namespace Tests\Tenant\Controllers;

use App\Facades\Feature;
use Carbon\Carbon;
use Illuminate\Support\Str;
use Mtc\MercuryDataModels\Booking;
use Mtc\MercuryDataModels\ReportingStat;
use Mtc\VehicleReservations\Reservation;
use Tests\TenantTestCase;
use Tests\UserForTenant;

class DashboardRangeKpisTest extends TenantTestCase
{
    use UserForTenant;

    public function testEnquiryCountAndAvgInventoryReflectTheSelectedRange(): void
    {
        // Enquiries: 5 inside the range, 99 well outside it.
        $this->stat('enquiries', Carbon::today()->subDays(2), 5);
        $this->stat('enquiries', Carbon::today()->subDays(40), 99);

        // Inventory: average of 10 and 20 = 15 inside the range.
        $this->stat('vehicle-count', Carbon::today()->subDays(1), 10);
        $this->stat('vehicle-count', Carbon::today()->subDays(2), 20);

        $kpis = $this->requestKpis()->keyBy('name');

        $this->assertEquals(5, $kpis['Enquiries']['value']);
        $this->assertEquals(15, $kpis['Avg. inventory']['value']);

        // Subtitle reflects the chosen range rather than the fixed "last 30 days".
        $this->assertNotSame('last 30 days', $kpis['Enquiries']['subtitle']);
    }

    public function testReservationAndServiceBookingPeriodKpisAreFeatureGated(): void
    {
        $names = $this->requestKpis()->pluck('name');
        $this->assertNotContains('Reservations', $names);
        $this->assertNotContains('Service Bookings', $names);

        Feature::setEnabled(tenant(), ['reservations', 'service-booking']);

        Reservation::factory()->count(2)->create([
            'confirmed_at' => Carbon::today()->subDays(2)->setTime(10, 0),
            'amount' => 100,
        ]);
        Reservation::factory()->create([
            'confirmed_at' => Carbon::today()->subDays(40)->setTime(10, 0),
            'amount' => 500,
        ]);
        Booking::factory()->create([
            'uuid' => (string) Str::uuid(),
            'ingested_at' => Carbon::today()->subDays(1)->setTime(10, 0),
        ]);

        $kpis = $this->requestKpis()->keyBy('name');

        $this->assertEquals(2, $kpis['Reservations']['value']);
        $this->assertEquals(200, $kpis['Revenue']['value']);
        $this->assertSame('currency', $kpis['Revenue']['format']);
        $this->assertEquals(1, $kpis['Service Bookings']['value']);
    }

    private function requestKpis()
    {
        $response = $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->postJson(route('tenant.dashboard.range-kpis'), [
                'from_date' => Carbon::today()->subDays(7)->format('Y-m-d'),
                'to_date' => Carbon::today()->format('Y-m-d'),
            ]);

        $response->assertStatus(200);

        return collect($response->json());
    }

    private function stat(string $key, Carbon $date, int $value): void
    {
        ReportingStat::create(['key' => $key, 'date' => $date->format('Y-m-d'), 'value' => $value]);
    }
}
