<?php

namespace Tests\Tenant;

use App\Modules\Valuation\ConditionPriceTier;
use Illuminate\Support\Facades\View;
use Mtc\MercuryDataModels\VehicleValuation;
use Tests\TenantTestCase;

class ValuationEmailPartialTest extends TenantTestCase
{
    private function makeValuation(array $attributes = []): VehicleValuation
    {
        $valuation = new VehicleValuation();
        $valuation->forceFill(array_merge([
            'id' => 1,
            'registration' => 'FOO11BAZ',
            'make' => 'BMW',
            'model' => '3 Series',
            'derivative' => 'sport',
            'mileage' => 60000,
            'retail_price' => 20000,
            'clean_price' => 15000,
            'average_price' => 12000,
            'below_price' => 9000,
        ], $attributes));

        return $valuation;
    }

    private function render(array $with): string
    {
        return View::make('emails.enquiry.partials.valuation', array_merge([
            'enquiry' => (object) ['reason' => $with['valuation']],
            'layerAccentColour' => '#ccc',
            'valuation_email_price_to_show' => 'all',
            'valuation_customer_only_tier' => null,
            'valuation_highlight_tier' => null,
            'valuation_condition_label' => null,
        ], $with))->render();
    }

    public function testRendersAllTiersByDefault(): void
    {
        $output = $this->render(['valuation' => $this->makeValuation()]);

        $this->assertStringContainsString('Retail value', $output);
        $this->assertStringContainsString('Clean value', $output);
        $this->assertStringContainsString('Average value', $output);
        $this->assertStringContainsString('Below value', $output);
    }

    public function testCustomerOnlyTierShowsOnlyThatTierAndHidesRetail(): void
    {
        $output = $this->render([
            'valuation' => $this->makeValuation(['cosmetic_condition' => 'Good']),
            'valuation_customer_only_tier' => ConditionPriceTier::TIER_CLEAN,
            'valuation_condition_label' => 'Good',
        ]);

        $this->assertStringContainsString('Clean value', $output);
        $this->assertStringNotContainsString('Retail value', $output);
        $this->assertStringNotContainsString('Average value', $output);
        $this->assertStringNotContainsString('Below value', $output);
        $this->assertStringContainsString('Condition', $output);
        $this->assertStringContainsString('Good', $output);
    }

    public function testHighlightTierWrapsMatchedTierInStrong(): void
    {
        $output = $this->render([
            'valuation' => $this->makeValuation(['cosmetic_condition' => 'poor']),
            'valuation_highlight_tier' => ConditionPriceTier::TIER_BELOW,
            'valuation_condition_label' => 'poor',
        ]);

        $this->assertStringContainsString('Retail value', $output);
        $this->assertStringContainsString('Clean value', $output);
        $this->assertStringContainsString('Average value', $output);
        $this->assertStringContainsString('Below value', $output);
        $this->assertMatchesRegularExpression('/<strong>\s*9000\s*<\/strong>/', $output);
    }

    public function testConditionRowHiddenWhenLabelMissing(): void
    {
        $output = $this->render(['valuation' => $this->makeValuation()]);

        $this->assertStringNotContainsString('>Condition<', $output);
    }
}
