<?php

namespace Tests\Tenant;

use App\Facades\Settings;
use Illuminate\Support\Facades\Queue;
use Mtc\MercuryDataModels\Dealership;
use Mtc\MercuryDataModels\Vehicle;
use Mtc\MercuryDataModels\VehicleFinance;
use Tests\TenantTestCase;

class HideSoldVehiclePricesTest extends TenantTestCase
{
    protected function setUp(): void
    {
        parent::setUp();
        Queue::fake();
    }

    private function enableHideSoldPrices(): void
    {
        Settings::make([
            'tab' => 'Automotive',
            'section' => 'General',
            'group' => 'Vehicles',
            'name' => 'Hide prices for sold vehicles',
            'config_key' => 'vehicle-hide-sold-prices',
            'type' => 'boolean',
            'value' => true,
        ]);
    }

    private function createVehicle(array $overrides = []): Vehicle
    {
        $dealership = Dealership::factory()->create();

        $vehicle = Vehicle::factory()->create(array_merge([
            'is_published' => true,
            'dealership_id' => $dealership->id,
            'price' => 25000,
            'original_price' => 27000,
            'previous_price' => 26000,
            'rrp_price' => 28000,
            'monthly_price' => 350,
        ], $overrides));

        VehicleFinance::factory()->create([
            'vehicle_id' => $vehicle->id,
            'finance_type' => 'HP',
            'monthly_price' => 350,
            'cash_price' => 25000,
            'total_deposit' => 3000,
            'customer_deposit' => 3000,
        ]);

        return $vehicle;
    }

    public function testSoldVehiclePricesHiddenOnList(): void
    {
        $this->enableHideSoldPrices();

        $this->createVehicle(['is_sold' => true]);

        $response = $this->postJson(route('vehicles.index'));

        $response->assertStatus(200);

        $vehicle = $response->json('results.data.0');
        $this->assertNull($vehicle['price']);
        $this->assertNull($vehicle['original_price']);
        $this->assertNull($vehicle['previous_price']);
        $this->assertNull($vehicle['rrp_price']);
        $this->assertNull($vehicle['deposit']);
        $this->assertNull($vehicle['monthly_price']);
        $this->assertNull($vehicle['finance_example']);
    }

    public function testNonSoldVehiclePricesVisibleOnList(): void
    {
        $this->enableHideSoldPrices();

        $this->createVehicle(['is_sold' => false]);

        $response = $this->postJson(route('vehicles.index'));

        $response->assertStatus(200);

        $vehicle = $response->json('results.data.0');
        $this->assertNotNull($vehicle['price']);
        $this->assertNotNull($vehicle['original_price']);
        $this->assertNotNull($vehicle['rrp_price']);
    }

    public function testSoldVehiclePricesVisibleOnListWhenSettingDisabled(): void
    {
        $this->createVehicle(['is_sold' => true]);

        $response = $this->postJson(route('vehicles.index'));

        $response->assertStatus(200);

        $vehicle = $response->json('results.data.0');
        $this->assertNotNull($vehicle['price']);
        $this->assertNotNull($vehicle['original_price']);
        $this->assertNotNull($vehicle['rrp_price']);
    }

    public function testSoldVehiclePricesHiddenOnView(): void
    {
        $this->enableHideSoldPrices();

        $this->createVehicle([
            'is_sold' => true,
            'slug' => 'sold-vehicle',
        ]);

        $response = $this->getJson(route('vehicles.show', 'sold-vehicle'));

        $response->assertStatus(200);

        $data = $response->json('data');
        $this->assertNull($data['price']);
        $this->assertNull($data['original_price']);
        $this->assertNull($data['previous_price']);
        $this->assertNull($data['rrp_price']);
        $this->assertNull($data['deposit']);
        $this->assertNull($data['monthly_price']);
        $this->assertNull($data['finance']);
        $this->assertNull($data['financeExamples']);
    }

    public function testNonSoldVehiclePricesVisibleOnViewWhenSettingEnabled(): void
    {
        $this->enableHideSoldPrices();

        $this->createVehicle([
            'is_sold' => false,
            'slug' => 'available-vehicle',
        ]);

        $response = $this->getJson(route('vehicles.show', 'available-vehicle'));

        $response->assertStatus(200);

        $data = $response->json('data');
        $this->assertNotNull($data['price']);
        $this->assertNotNull($data['original_price']);
        $this->assertNotNull($data['rrp_price']);
        $this->assertNotNull($data['financeExamples']);
    }

    public function testSoldVehiclePricesVisibleOnViewWhenSettingDisabled(): void
    {
        $this->createVehicle([
            'is_sold' => true,
            'slug' => 'sold-vehicle-visible',
        ]);

        $response = $this->getJson(route('vehicles.show', 'sold-vehicle-visible'));

        $response->assertStatus(200);

        $data = $response->json('data');
        $this->assertNotNull($data['price']);
        $this->assertNotNull($data['original_price']);
        $this->assertNotNull($data['rrp_price']);
        $this->assertNotNull($data['financeExamples']);
    }
}
