<?php

namespace Tests\Tenant\Reporting;

use App\Reporting\Repositories\CRMReportRepository;
use Carbon\Carbon;
use Illuminate\Support\Facades\App;
use Mtc\MercuryDataModels\Enquiry;
use Tests\TenantTestCase;

class CrmYearlyBreakdownTest extends TenantTestCase
{
    public function testReturnsEmptyCollectionWhenNoEnquiries(): void
    {
        // Regression for AUTO-1609: with no enquiry data the report used to 500 on
        // array_keys(null); it should return an empty result instead.
        $result = App::make(CRMReportRepository::class)->yearlyBreakdown();

        $this->assertTrue($result->isEmpty(), 'Expected an empty result when there is no enquiry data');
    }

    public function testAggregatesEnquiriesWhenDataExists(): void
    {
        Enquiry::factory()->create([
            'ingested_at' => Carbon::now()->startOfMonth()->addDay()->setTime(10, 0),
        ]);

        $result = App::make(CRMReportRepository::class)->yearlyBreakdown();

        $this->assertFalse($result->isEmpty());
        $this->assertTrue($result->has(__('labels.total')), 'Expected a total row when data exists');
    }
}
