<?php

namespace Tests\Tenant;

use App\Facades\Settings;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;
use Mtc\MercuryDataModels\Dealership;
use Mtc\MercuryDataModels\Vehicle;
use Tests\TenantTestCase;

class GoogleFeedTest extends TenantTestCase
{
    protected $route;

    #[\Override]
    public function setUp(): void
    {
        parent::setUp();
        Storage::fake('public');

        $this->route = route('tenant.feeds.google', tenant('id')) . '?api_token=' . config('auth.api_auth_token');
    }

    public function testUnauthorizedAccessReturns401()
    {
        $response = $this->get(route('tenant.feeds.facebook', tenant('id')) . '?api_token=invalid');

        $response->assertStatus(401);
    }

    public function testDisabledFeedReturns404()
    {
        Settings::shouldReceive('get')
            ->with('sales-channels-google-enabled', false)
            ->andReturn(false);

        $response = $this->get($this->route);

        $response->assertStatus(404);
    }

    public function testCanViewFeed()
    {
        Settings::shouldReceive('get')
            ->with('sales-channels-google-enabled', false)
            ->andReturn(true);

        Settings::shouldReceive('get')
            ->with('sales-channels-google-store-code', null)
            ->andReturn(fake()->randomDigitNotZero())
            ->byDefault();

        $response = $this->get($this->route);

        $response->assertStatus(200);
        $response->assertSee("store_code", "title");
        $response->assertSee("description");
        $response->assertSee("availability");
        $response->assertSee("custom_label_0");
        $response->assertSee("custom_label_1");
    }

    public function testCanDownloadFeed()
    {
        Settings::shouldReceive('get')
            ->with('sales-channels-google-enabled', false)
            ->andReturn(true);

        Settings::shouldReceive('get')
            ->with('sales-channels-google-store-code', null)
            ->andReturn(fake()->randomDigitNotZero())
            ->byDefault();

        $response = $this->get($this->route . '&download=true');

        $response->assertStatus(200);
        $response->assertHeader('Content-Type', 'text/csv; charset=UTF-8');
        $response->assertHeader('Content-Disposition', 'attachment; filename=google.csv');
    }

    public function testStoreCodeFallsBackToSettingWhenDealershipHasNoOverride()
    {
        $dealership = Dealership::factory()->create();
        Vehicle::factory()->create([
            'dealership_id' => $dealership->id,
            'is_published' => true,
        ]);

        Settings::shouldReceive('get')
            ->with('sales-channels-google-enabled', false)
            ->andReturn(true);

        Settings::shouldReceive('get')
            ->with('sales-channels-google-store-code', null)
            ->andReturn('GLOBAL_456');

        Settings::shouldReceive('get')->byDefault();

        $response = $this->get($this->route);

        $response->assertStatus(200);
        $response->assertSee('GLOBAL_456');
    }

    public function testFeedIsStoredWithTenantSpecificPath()
    {
        Excel::fake();

        Settings::shouldReceive('get')
            ->with('sales-channels-google-enabled', false)
            ->andReturn(true);

        Settings::shouldReceive('get')
            ->with('sales-channels-google-store-code', null)
            ->andReturn(fake()->randomDigitNotZero())
            ->byDefault();

        $this->get($this->route);

        Excel::assertStored('feeds/google-feed/' . tenant('id') . '-google.csv', 'file-storage');
    }

    public function testVehiclesWithNoPriceAreExcludedFromFeed()
    {
        $dealership = Dealership::factory()->create();

        Vehicle::factory()->create([
            'dealership_id' => $dealership->id,
            'is_published' => true,
            'price' => 19995,
            'vin' => 'PRICEDVIN12345678',
        ]);

        Vehicle::factory()->create([
            'dealership_id' => $dealership->id,
            'is_published' => true,
            'price' => null,
            'vin' => 'NULLPRICEVIN12345',
        ]);

        Vehicle::factory()->create([
            'dealership_id' => $dealership->id,
            'is_published' => true,
            'price' => 0,
            'vin' => 'ZEROPRICEVIN12345',
        ]);

        Settings::shouldReceive('get')
            ->with('sales-channels-google-enabled', false)
            ->andReturn(true);

        Settings::shouldReceive('get')->byDefault();

        $response = $this->get($this->route);

        $response->assertStatus(200);
        $response->assertSee('PRICEDVIN12345678');
        $response->assertDontSee('NULLPRICEVIN12345');
        $response->assertDontSee('ZEROPRICEVIN12345');
    }

    public function testMileageFallsBackToZeroWhenOdometerIsNull()
    {
        $dealership = Dealership::factory()->create();
        Vehicle::factory()->create([
            'dealership_id' => $dealership->id,
            'is_published' => true,
            'odometer_mi' => null,
            'odometer_km' => null,
        ]);

        Settings::shouldReceive('get')
            ->with('sales-channels-google-enabled', false)
            ->andReturn(true);

        Settings::shouldReceive('get')
            ->with('automotive-distance_measurement')
            ->andReturn('mi');

        Settings::shouldReceive('get')->byDefault();

        $response = $this->get($this->route);

        $response->assertStatus(200);
        $response->assertSee('0 miles');
        $response->assertDontSee(',&quot; miles&quot;', false);
        $response->assertDontSee('," miles"', false);
    }

    public function testStoreCodeUsesDealershipValueWhenSet()
    {
        $dealership = Dealership::factory()->create([
            'data' => ['google-store-code' => 'DEALER_123'],
        ]);
        Vehicle::factory()->create([
            'dealership_id' => $dealership->id,
            'is_published' => true,
        ]);

        Settings::shouldReceive('get')
            ->with('sales-channels-google-enabled', false)
            ->andReturn(true);

        Settings::shouldReceive('get')
            ->with('sales-channels-google-store-code', null)
            ->andReturn('GLOBAL_456');

        Settings::shouldReceive('get')->byDefault();

        $response = $this->get($this->route);

        $response->assertStatus(200);
        $response->assertSee('DEALER_123');
        $response->assertDontSee('GLOBAL_456');
    }
}
