<?php

namespace Tests\Feature;

use Mtc\MercuryDataModels\Dealership;
use Mtc\MercuryDataModels\NewCar;
use Tests\DatabaseTestCase;

class NewCarControllerDealerNumberTest extends DatabaseTestCase
{
    public function testCarShowsDealershipNumber()
    {
        $this->withoutExceptionHandling();
        $dealership1 = Dealership::factory()->create([
            'contact_no' => '123123',
        ]);
        $dealership2 = Dealership::factory()->create([
            'contact_no' => null
        ]);
        $offer = NewCar::factory()->create(['published' => true]);
        $offer1 = NewCar::factory()->create(['published' => true, 'dealership_id' => $dealership1]);
        $offer2 = NewCar::factory()->create(['published' => true, 'dealership_id' => $dealership2]);

        $response = $this->asTenant(tenant())
            ->getJson(route('new-cars.show', $offer));
        $response->assertStatus(200);
        $this->assertNull($response->json('contact_number'));

        $response = $this->asTenant(tenant())
            ->getJson(route('new-cars.show', $offer1));
        $response->assertStatus(200);
        $this->assertIsArray($response->json('contact_number'));
        $this->assertArrayHasKey('number', $response->json('contact_number'));
        $this->assertArrayHasKey('display', $response->json('contact_number'));
        $this->assertStringContainsString('123123', $response->json('contact_number.number'));

        $response = $this->asTenant(tenant())
            ->getJson(route('new-cars.show', $offer2));
        $response->assertStatus(200);
        $this->assertNull($response->json('contact_number'));
    }
}
