<?php

namespace Tests\Tenant;

use App\Exports\InventoryExport;
use App\Facades\Settings;
use App\Jobs\InventoryExportJob;
use Illuminate\Support\Facades\Storage;
use Mtc\MercuryDataModels\Dealership;
use Mtc\MercuryDataModels\SalesChannelHistory;
use Mtc\MercuryDataModels\SalesChannelRule;
use Mtc\MercuryDataModels\Vehicle;
use Tests\TenantTestCase;

class InventoryExportTest extends TenantTestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function testExportJob()
    {
        Storage::fake('inventory-export');
        Vehicle::factory(10)->create();
        Settings::update('sales-channels-inventory-file-name', 'foo.csv');

        new InventoryExportJob()->handle();

        Storage::disk('inventory-export')->assertExists('foo.csv');
        $logged = SalesChannelHistory::query()
            ->where('channel', 'inventory')
            ->where('was_successful', true)
            ->exists();
        $this->assertTrue($logged);
    }

    public function testNewVehicleYearFallsBackToZeroOnlyWhenMissing()
    {
        // New with a real year keeps it; a new vehicle missing the year sends 0;
        // used vehicles are unaffected.
        Vehicle::factory()->create([
            'is_published' => true,
            'is_new' => true,
            'manufacture_year' => 2025,
            'uuid' => 'stock-new-year',
        ]);
        Vehicle::factory()->create([
            'is_published' => true,
            'is_new' => true,
            'manufacture_year' => null,
            'uuid' => 'stock-new-noyear',
        ]);
        Vehicle::factory()->create([
            'is_published' => true,
            'is_new' => false,
            'manufacture_year' => 2019,
            'uuid' => 'stock-used',
        ]);

        $rows = new InventoryExport()->collection()->keyBy('Stock #');

        $this->assertEquals(2025, $rows['stock-new-year']['Year']);
        $this->assertSame(0, $rows['stock-new-noyear']['Year']);
        $this->assertEquals(2019, $rows['stock-used']['Year']);
    }

    public function testFiltersByDealershipIdInList()
    {
        $included = Dealership::factory()->create();
        $excluded = Dealership::factory()->create();

        Vehicle::factory()->create([
            'is_published' => true,
            'dealership_id' => $included->id,
            'uuid' => 'stock-in',
        ]);
        Vehicle::factory()->create([
            'is_published' => true,
            'dealership_id' => $excluded->id,
            'uuid' => 'stock-out',
        ]);

        SalesChannelRule::query()->create([
            'channel' => 'inventory',
            'conditions' => [[
                'value' => [
                    'type' => 'and',
                    'column' => 'dealership_id',
                    'operator' => 'in',
                    'value' => [$included->id],
                ],
            ]],
        ]);

        $stockNumbers = new InventoryExport()->collection()->pluck('Stock #');

        $this->assertTrue($stockNumbers->contains('stock-in'));
        $this->assertFalse($stockNumbers->contains('stock-out'));
    }
}
