<?php

namespace Tests\Tenant\Controllers;

use Carbon\Carbon;
use Mtc\MercuryDataModels\Enquiry;
use Tests\TenantTestCase;
use Tests\UserForTenant;

class EnquiryTimingEndpointsTest extends TenantTestCase
{
    use UserForTenant;

    public function testWeekdayEndpointReturnsSevenZeroFilledDays(): void
    {
        $monday = Carbon::now()->startOfDay()->subDays(Carbon::now()->dayOfWeekIso - 1 + 7);
        Enquiry::factory()->create(['ingested_at' => $monday->copy()->setTime(10, 0)]);

        $response = $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->postJson(route('tenant.reports.crm.weekday-breakdown'), [
                'from' => Carbon::now()->subDays(90)->format('Y-m-d'),
                'to' => Carbon::now()->format('Y-m-d'),
            ]);

        $response->assertStatus(200);
        $this->assertCount(7, $response->json('results'));
        $this->assertSame('Monday', $response->json('results.0.label'));
        $this->assertSame(1, $response->json('results.0.enquiry_count'));
    }

    public function testTimeOfDayEndpointReturns24ZeroFilledHours(): void
    {
        $day = Carbon::now()->startOfDay()->subDays(Carbon::now()->dayOfWeekIso - 1 + 7);
        Enquiry::factory()->create(['ingested_at' => $day->copy()->setTime(10, 30)]);

        $response = $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->postJson(route('tenant.reports.crm.time-of-day-breakdown'), [
                'from' => Carbon::now()->subDays(90)->format('Y-m-d'),
                'to' => Carbon::now()->format('Y-m-d'),
            ]);

        $response->assertStatus(200);
        $this->assertCount(24, $response->json('results'));
        $this->assertSame('10:00', $response->json('results.10.label'));
        $this->assertSame(1, $response->json('results.10.enquiry_count'));
    }
}
