<?php

namespace Tests\Tenant\Reporting;

use App\Reporting\ReservationsRevenueTodayKPI;
use App\Reporting\ReservationsTodayKPI;
use App\Reporting\ServiceBookingsTodayKPI;
use Carbon\Carbon;
use Illuminate\Support\Str;
use Mtc\MercuryDataModels\Booking;
use Mtc\VehicleReservations\Reservation;
use Tests\TenantTestCase;

class DashboardKpiTest extends TenantTestCase
{
    public function testReservationsTodayKpiCountsConfirmedReservations(): void
    {
        Reservation::factory()->count(2)->create(['confirmed_at' => Carbon::today()->setTime(10, 0)]);
        Reservation::factory()->create(['confirmed_at' => Carbon::yesterday()->setTime(10, 0)]);
        // Unconfirmed reservation should be ignored.
        Reservation::factory()->create(['confirmed_at' => null]);

        $tile = (new ReservationsTodayKPI())->toArray();

        $this->assertSame(2, $tile['value']);
        $this->assertSame(1, $tile['secondary_value']);
        $this->assertSame('Yesterday', $tile['secondary_name']);
    }

    public function testReservationsRevenueTodayKpiSumsAmountAndFormatsCurrency(): void
    {
        Reservation::factory()->create(['confirmed_at' => Carbon::today()->setTime(10, 0), 'amount' => 100]);
        Reservation::factory()->create(['confirmed_at' => Carbon::today()->setTime(11, 0), 'amount' => 150]);
        Reservation::factory()->create(['confirmed_at' => Carbon::yesterday()->setTime(10, 0), 'amount' => 50]);

        $tile = (new ReservationsRevenueTodayKPI())->toArray();

        $this->assertSame(250.0, $tile['value']);
        $this->assertSame(50.0, $tile['secondary_value']);
        $this->assertSame('currency', $tile['format']);
    }

    public function testServiceBookingsTodayKpiCountsBookings(): void
    {
        Booking::factory()->count(3)
            ->state(fn () => ['uuid' => (string) Str::uuid(), 'ingested_at' => Carbon::today()->setTime(10, 0)])
            ->create();
        Booking::factory()->create(['uuid' => (string) Str::uuid(), 'ingested_at' => Carbon::yesterday()->setTime(10, 0)]);

        $tile = (new ServiceBookingsTodayKPI())->toArray();

        $this->assertSame(3, $tile['value']);
        $this->assertSame(1, $tile['secondary_value']);
    }
}
