<?php

namespace Tests\Feature;

use App\Exports\ChannelFeedExport;
use App\Facades\Settings;
use App\Facades\Site;
use Carbon\Carbon;
use Database\Seeders\Global\CountrySeeder;
use Mtc\MercuryDataModels\Vehicle;
use Tests\DatabaseTestCase;

class ChannelFeedExportTest extends DatabaseTestCase
{
    #[\Override]
    protected function setUp(): void
    {
        parent::setUp();

        $this->seed(CountrySeeder::class);

        Settings::shouldReceive('get')->andReturn(null)->byDefault();
        Site::shouldReceive('vehicleUrl')->andReturn('https://example.test/vehicle')->byDefault();
    }

    #[\Override]
    protected function tearDown(): void
    {
        Carbon::setTestNow();
        parent::tearDown();
    }

    public function testDaysInStockUsesStockArrivalDateWhenSet(): void
    {
        Carbon::setTestNow('2026-07-07 00:00:00');

        $vehicle = Vehicle::factory()->create([
            'stock_arrival_date' => Carbon::parse('2026-06-27'),
            'created_at' => Carbon::parse('2026-07-04'),
        ]);

        $row = new ChannelFeedExport()->map($vehicle->fresh());

        $this->assertEquals(10, (int) $row['days_in_stock']);
    }

    public function testDaysInStockFallsBackToCreatedAtWhenNoArrivalDate(): void
    {
        Carbon::setTestNow('2026-07-07 00:00:00');

        $vehicle = Vehicle::factory()->create([
            'stock_arrival_date' => null,
            'created_at' => Carbon::parse('2026-07-02'),
        ]);

        $row = new ChannelFeedExport()->map($vehicle->fresh());

        $this->assertEquals(5, (int) $row['days_in_stock']);
    }
}
