<?php

namespace Tests\Tenant;

use App\Http\Middleware\ApiAuthenticatedRequest;
use App\Http\Middleware\InitializeTenancyBySiteInRequest;
use Mtc\MercuryDataModels\BodyStyleType;
use Mtc\MercuryDataModels\FuelType;
use Mtc\MercuryDataModels\TransmissionType;
use Mtc\MercuryDataModels\Vehicle;
use Mtc\MercuryDataModels\VehicleMake;
use Mtc\MercuryDataModels\VehicleModel;
use Tests\TenantTestCase;

class OutAndAboutExportTest extends TenantTestCase
{
    private array $excludedMiddlewares = [
        InitializeTenancyBySiteInRequest::class,
        ApiAuthenticatedRequest::class,
    ];

    public function testStockRouteReturnsExpectedVehicleData()
    {
        $make = VehicleMake::factory()->create(['name' => 'Mercedes']);
        $model = VehicleModel::factory()->create(['name' => 'Sprinter', 'make_id' => $make->id]);
        $bodyStyle = BodyStyleType::factory()->create(['name' => 'Campervan']);
        $fuelType = FuelType::factory()->create(['name' => 'Diesel']);
        $transmission = TransmissionType::factory()->create(['name' => 'Automatic']);

        Vehicle::factory()->create([
            'is_published' => true,
            'uuid' => 'TEST-123',
            'make_id' => $make->id,
            'model_id' => $model->id,
            'body_style_id' => $bodyStyle->id,
            'fuel_type_id' => $fuelType->id,
            'transmission_id' => $transmission->id,
            'price' => 45000,
            'odometer_mi' => 25000,
            'first_registration_date' => '2022-06-15',
            'is_new' => false,
            'type' => 'camper van',
        ]);

        $response = $this->withoutMiddleware($this->excludedMiddlewares)
            ->get(route('out-and-about.stock', ['tenant' => tenant('id')]));

        $response->assertStatus(200);
        $response->assertHeader('Content-Type', 'application/xml');

        $content = $response->getContent();
        $this->assertStringContainsString('<StockNumber>TEST-123</StockNumber>', $content);
        $this->assertStringContainsString('<Manufacturer>Mercedes</Manufacturer>', $content);
        $this->assertStringContainsString('<Model>Sprinter</Model>', $content);
        $this->assertStringContainsString('<Type>Campervan</Type>', $content);
        $this->assertStringContainsString('<Fuel>Diesel</Fuel>', $content);
        $this->assertStringContainsString('<Gears>Automatic</Gears>', $content);
        $this->assertStringContainsString('<Price>45000</Price>', $content);
        $this->assertStringContainsString('<Mileage>25000</Mileage>', $content);
        $this->assertStringContainsString('<RegistrationYear>2022</RegistrationYear>', $content);
        $this->assertStringContainsString('<Condition>Used</Condition>', $content);
    }

    public function testStockRouteReturnsExpectedVehicleCount()
    {
        Vehicle::factory(3)->create([
            'is_published' => true,
        ]);

        $response = $this->withoutMiddleware($this->excludedMiddlewares)
            ->get(route('out-and-about.stock', ['tenant' => tenant('id')]));

        $response->assertStatus(200);
        $response->assertHeader('Content-Type', 'application/xml');

        $xml = new \SimpleXMLElement($response->getContent());
        $this->assertCount(3, $xml->xpath('//Product'));
    }

    public function testStockRouteLogsApiNotification()
    {
        $this->assertDatabaseCount('api_notifications', 0);

        $response = $this->withoutMiddleware($this->excludedMiddlewares)
            ->get(route('out-and-about.stock', ['tenant' => tenant('id')]));

        $response->assertStatus(200);

        $this->assertDatabaseCount('api_notifications', 1);
        $this->assertDatabaseHas('api_notifications', [
            'provider' => 'out-and-about',
            'processed' => true,
            'data_model' => 'vehicle',
        ]);
    }
}
