<?php

namespace Tests\Tenant;

use App\Crm\SendCopyViaMail;
use App\Facades\Settings;
use App\Mail\NewEnquiryCustomerCopyNotification;
use App\Mail\NewEnquiryMail;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Mail;
use Mtc\Crm\Facades\Enquiries;
use Mtc\Crm\Models\EnquiryAction;
use Mtc\Crm\Models\EnquiryObject;
use Mtc\Crm\Models\EnquiryType;
use Mtc\Crm\Models\FormAction;
use Mtc\Crm\Models\FormQuestion;
use Mtc\Crm\Models\FormSection;
use Mtc\MercuryDataModels\Dealership;
use Mtc\MercuryDataModels\Enquiry;
use Mtc\MercuryDataModels\Form;
use Mtc\MercuryDataModels\Property;
use Mtc\MercuryDataModels\PropertyCategory;
use Mtc\MercuryDataModels\Vehicle;
use Mtc\MercuryDataModels\VehicleValuation;
use Tests\TenantTestCase;
use Tests\UserForTenant;

class EnquiryRepositoryTest extends TenantTestCase
{
    use UserForTenant;

    public function testShowList()
    {
        Enquiry::factory(10)->create();
        $list = Enquiries::showList(\request());
        $this->assertInstanceOf(LengthAwarePaginator::class, $list);
        $this->assertEquals(10, $list->total());
    }


    public function testShowEditUrl()
    {
        $enquiry = \Mtc\Crm\Models\Enquiry::factory()->create([
            'title' => 'foo',
            'reason_type' => 'vehicle',
            'reason_id' => 123
        ]);

        $response = $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->get(route('enquiries.details', $enquiry->id));
        $this->assertStringContainsString('vehicles', $response->json('reason_url'));

        $enquiry = \Mtc\Crm\Models\Enquiry::factory()->create([
            'title' => 'foo',
            'reason_type' => 'new-car',
            'reason_id' => 123
        ]);

        $response = $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->get(route('enquiries.details', $enquiry->id));
        $this->assertStringContainsString('new-vehicles', $response->json('reason_url'));

        $enquiry = \Mtc\Crm\Models\Enquiry::factory()->create([
            'title' => 'foo',
            'reason_type' => 'offer',
            'reason_id' => 123
        ]);

        $response = $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->get(route('enquiries.details', $enquiry->id));
        $this->assertStringContainsString('offers', $response->json('reason_url'));

        $enquiry = \Mtc\Crm\Models\Enquiry::factory()->create([
            'title' => 'foo',
            'reason_type' => 'valuation',
            'reason_id' => 123
        ]);

        $response = $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->get(route('enquiries.details', $enquiry->id));
        $this->assertStringContainsString('valuations', $response->json('reason_url'));

        $category = PropertyCategory::factory()->create(['slug' => 'foo']);
        $property = Property::factory()->create(['slug' => 'baz', 'category_id' => $category->id]);

        $enquiry = \Mtc\Crm\Models\Enquiry::factory()->create([
            'title' => 'foo',
            'reason_type' => 'property',
            'reason_id' => $property->id
        ]);

        $response = $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->get(route('enquiries.details', $enquiry->id));
        $this->assertStringContainsString('properties/foo/edit/' .$property->id, $response->json('reason_url'));
    }

    public function testEnquiryDealershipRecipients()
    {
        // setup
        Mail::fake();

        Settings::make([
            'tab' => 'foo-baz',
            'section' => 'lorem',
            'group' => 'ipsum',
            'name' => 'dolor',
            'type' => 'image',
            'config_key' => 'site-logo'
        ]);

        Dealership::factory()->create([
            'id' => 4,
        ]);

        $form = Form::factory()->create();
        $section = FormSection::factory()->create(['form_id' => $form->id]);

        $form_question = FormQuestion::factory()->create([
            'form_id' => $form->id,
            'form_section_id' => $section->id,
            "name" => "Dealership",
            'data' => [
                "component" => false,
                "componentName" => false,
                "meta" => null,
                "name" => "Dealership",
                "type" => "dealership_id",
                'specifies-recipient' => true,
                "field-emails-specification" => "4:recipient1@test.com"
            ],
        ]);

        FormAction::factory()->create([
            'form_id' => $form->id,
            "action_name" => "send-copy-via-mail",
            "fields" => [
                "send_to_field_location" => null,
                "send_to_both" => false,
                "recipient" => "fallback@test.com",
                "enquiry_subject" => "Vehicle Enquiry",
                "enquiry_content" => "A new vehicle enquiry has been submitted on the website."
            ],
        ]);

        $form->refresh();

        // this works with 'answer' => 4,
        // also works with string i.e. 'answer' => '4',
        $details = [
            (string)$form_question->id => [
                'id' => $form_question->id,
                'answer' => 4,
                'question' => $form_question->name,
                "type" => "dealership_id",
                'value' => 'my dealership name',
            ],
        ];

        $enquiry = Enquiry::factory()->create([
            'form_id' => $form->id,
            'details' => $details,
            'data' => [
                'questions' => [
                    (string)$form_question->id => [
                        'component' => 'TextField',
                        'componentName' => 'TextField',
                        'meta' => null,
                    ],
                ],
            ],
        ]);

        $action = EnquiryAction::factory()->create([
            'enquiry_id' => $enquiry->id,
            'action_name' => 'send-copy-via-mail',
            'data' => [
                'send_to_field_location' => null,
                'send_to_both' => false,
                'recipient' => 'fallback@test.com',
                'enquiry_subject' => 'my enquiry subject',
                'enquiry content' => 'lorem ipsum',
            ],
            'processed_at' => null,
            'attempts' => 0,
            'failed_at' => null,
        ]);

        $enquiry->refresh();

        new SendCopyViaMail()->handle($action, $enquiry);

        Mail::assertSent(NewEnquiryMail::class, fn (NewEnquiryMail $mail) => $mail->assertTo('recipient1@test.com'));
    }

    public function testEnquiryRecipientField()
    {
        // setup
        Mail::fake();

        Settings::make([
            'tab' => 'foo-baz',
            'section' => 'lorem',
            'group' => 'ipsum',
            'name' => 'dolor',
            'type' => 'image',
            'config_key' => 'site-logo'
        ]);

        $form = Form::factory()->create();
        $section = FormSection::factory()->create(['form_id' => $form->id]);

        $form_question = FormQuestion::factory()->create([
            'form_id' => $form->id,
            'form_section_id' => $section->id,
            "name" => "Foo",
            'data' => [
                "component" => false,
                "componentName" => false,
                "meta" => null,
                "name" => "Foo",
                "type" => "foo",
            ],
        ]);

        FormAction::factory()->create([
            'form_id' => $form->id,
            "action_name" => "send-copy-via-mail",
            "fields" => [
                "send_to_field_location" => 'foo',
                "send_to_both" => false,
                "recipient" => "fallback@test.com",
                "enquiry_subject" => "Vehicle Enquiry",
                "enquiry_content" => "A new vehicle enquiry has been submitted on the website."
            ],
        ]);

        $form->refresh();

        $details = [
            (string)$form_question->id => [
                'id' => $form_question->id,
                'answer' => 'question.recipient@test.com',
                'question' => $form_question->name,
                "type" => "recipient",
                'value' => 'question.recipient@test.com',
            ],
        ];

        $enquiry = Enquiry::factory()->create([
            'form_id' => $form->id,
            'details' => $details,
            'data' => [
                'questions' => [
                    (string)$form_question->id => [
                        'component' => 'TextField',
                        'componentName' => 'TextField',
                        'meta' => null,
                    ],
                ],
            ],
        ]);

        $action = EnquiryAction::factory()->create([
            'enquiry_id' => $enquiry->id,
            'action_name' => 'send-copy-via-mail',
            'data' => [
                'send_to_field_location' => 'Foo',
                'send_to_both' => false,
                'recipient' => 'fallback@test.com',
                'enquiry_subject' => 'my enquiry subject',
                'enquiry content' => 'lorem ipsum',
            ],
            'processed_at' => null,
            'attempts' => 0,
            'failed_at' => null,
        ]);

        $enquiry->refresh();

        new SendCopyViaMail()->handle($action, $enquiry);

        Mail::assertSent(NewEnquiryMail::class, fn (NewEnquiryMail $mail) => $mail->assertTo('question.recipient@test.com'));
    }

    public function testSendEnquiryToVehicleDealershipDisabled()
    {
        // setup
        Mail::fake();

        Settings::make([
            'tab' => 'foo-baz',
            'section' => 'lorem',
            'group' => 'ipsum',
            'name' => 'dolor',
            'type' => 'image',
            'config_key' => 'site-logo'
        ]);

        $form = Form::factory()->create();
        $section = FormSection::factory()->create(['form_id' => $form->id]);

        $form_question = FormQuestion::factory()->create([
            'form_id' => $form->id,
            'form_section_id' => $section->id,
            "name" => "Foo",
            'data' => [
                "component" => false,
                "componentName" => false,
                "meta" => null,
                "name" => "Foo",
                "type" => "foo",
            ],
        ]);

        FormAction::factory()->create([
            'form_id' => $form->id,
            "action_name" => "send-copy-via-mail",
            "fields" => [
                "send_to_field_location" => 'foo',
                "send_to_both" => true,
                "recipient" => "fallback@test.com",
                "enquiry_subject" => "Vehicle Enquiry",
                "enquiry_content" => "A new vehicle enquiry has been submitted on the website."
            ],
        ]);

        $form->refresh();

        $dealership = Dealership::factory()->create([
            'email' => 'dealer@test.com'
        ]);

        $vehicle = Vehicle::factory()->create([
            'dealership_id' => $dealership->id,
        ]);

        $details = [
            (string)$form_question->id => [
                'id' => $form_question->id,
                'answer' => 'question.recipient@test.com',
                'question' => $form_question->name,
                "type" => "recipient",
                'value' => 'question.recipient@test.com',
            ],
        ];

        $enquiry = \Mtc\Crm\Models\Enquiry::factory()->create([
            'title' => 'foo',
            'reason_type' => 'vehicle',
            'reason_id' => $vehicle->id,
            'form_id' => $form->id,
            'details' => $details,
            'data' => [
                'questions' => [
                    (string)$form_question->id => [
                        'component' => 'TextField',
                        'componentName' => 'TextField',
                        'meta' => null,
                    ],
                ],
            ],
        ]);

        $action = EnquiryAction::factory()->create([
            'enquiry_id' => $enquiry->id,
            'action_name' => 'send-copy-via-mail',
            'data' => [
                'send_to_field_location' => 'Foo',
                'send_to_both' => false,
                'recipient' => 'recipient@test.com',
                'enquiry_subject' => 'my enquiry subject',
                'enquiry content' => 'lorem ipsum',
            ],
            'processed_at' => null,
            'attempts' => 0,
            'failed_at' => null,
        ]);

        $enquiry->refresh();

        new SendCopyViaMail()->handle($action, $enquiry);

        $sent_to = [];

        Mail::assertSent(NewEnquiryMail::class, function (NewEnquiryMail $mail) use (&$sent_to) {
            $sent_to[] = $mail->to[0]['address'];
            return $mail->assertTo('question.recipient@test.com');
        });

        $this->assertNotContains('dealer@test.com', $sent_to);
    }

    public function testSendEnquiryToVehicleDealershipEnabled()
    {
        // setup
        Mail::fake();

        Settings::make([
            'tab' => 'foo-baz',
            'section' => 'lorem',
            'group' => 'ipsum',
            'name' => 'dolor',
            'type' => 'image',
            'config_key' => 'site-logo'
        ]);

        Settings::make([
            'tab' => 'Site',
            'section' => 'General',
            'group' => 'Enquiries',
            'name' => 'Send vehicle enquiries to dealership',
            'config_key' => 'form-enquiry-send-to-vehicle-dealership',
            'type' => 'boolean',
            'value' => true,
        ]);

        $form = Form::factory()->create();
        $section = FormSection::factory()->create(['form_id' => $form->id]);

        $form_question = FormQuestion::factory()->create([
            'form_id' => $form->id,
            'form_section_id' => $section->id,
            "name" => "Foo",
            'data' => [
                "component" => false,
                "componentName" => false,
                "meta" => null,
                "name" => "Foo",
                "type" => "foo",
            ],
        ]);

        FormAction::factory()->create([
            'form_id' => $form->id,
            "action_name" => "send-copy-via-mail",
            "fields" => [
                "send_to_field_location" => 'foo',
                "send_to_both" => true,
                "recipient" => "fallback@test.com",
                "enquiry_subject" => "Vehicle Enquiry",
                "enquiry_content" => "A new vehicle enquiry has been submitted on the website."
            ],
        ]);

        $form->refresh();

        $dealership = Dealership::factory()->create([
            'email' => 'dealer@test.com'
        ]);

        $vehicle = Vehicle::factory()->create([
            'dealership_id' => $dealership->id,
        ]);

        $details = [
            (string)$form_question->id => [
                'id' => $form_question->id,
                'answer' => 'question.recipient@test.com',
                'question' => $form_question->name,
                "type" => "recipient",
                'value' => 'question.recipient@test.com',
            ],
        ];

        $enquiry = \Mtc\Crm\Models\Enquiry::factory()->create([
            'title' => 'foo',
            'reason_type' => 'vehicle',
            'reason_id' => $vehicle->id,
            'form_id' => $form->id,
            'details' => $details,
            'data' => [
                'questions' => [
                    (string)$form_question->id => [
                        'component' => 'TextField',
                        'componentName' => 'TextField',
                        'meta' => null,
                    ],
                ],
            ],
        ]);

        EnquiryObject::factory()->create([
            'enquiry_id' => $enquiry->id,
            'object_id' => $vehicle->id,
            'object_type' => 'foo',
        ]);

        EnquiryObject::factory()->create([
            'enquiry_id' => $enquiry->id,
            'object_id' => $vehicle->id,
            'object_type' => 'vehicle',
        ]);

        EnquiryObject::factory()->create([
            'enquiry_id' => $enquiry->id,
            'object_id' => $vehicle->id,
            'object_type' => 'bar',
        ]);

        $action = EnquiryAction::factory()->create([
            'enquiry_id' => $enquiry->id,
            'action_name' => 'send-copy-via-mail',
            'data' => [
                'send_to_field_location' => 'Foo',
                'send_to_both' => false,
                'recipient' => 'recipient@test.com',
                'enquiry_subject' => 'my enquiry subject',
                'enquiry content' => 'lorem ipsum',
            ],
            'processed_at' => null,
            'attempts' => 0,
            'failed_at' => null,
        ]);

        $enquiry->refresh();

        new SendCopyViaMail()->handle($action, $enquiry);

        $sent_to = [];

        Mail::assertSent(NewEnquiryMail::class, function (NewEnquiryMail $mail) use (&$sent_to) {
            $sent_to[] = $mail->to[0]['address'];
            return true;
        });

        $this->assertContains('question.recipient@test.com', $sent_to);
        $this->assertContains('dealer@test.com', $sent_to);
    }

    public function testSendEnquiryToVehicleDealershipEnabledMultiple()
    {
        // setup
        Mail::fake();

        Settings::make([
            'tab' => 'foo-baz',
            'section' => 'lorem',
            'group' => 'ipsum',
            'name' => 'dolor',
            'type' => 'image',
            'config_key' => 'site-logo'
        ]);

        Settings::make([
            'tab' => 'Site',
            'section' => 'General',
            'group' => 'Enquiries',
            'name' => 'Send vehicle enquiries to dealership',
            'config_key' => 'form-enquiry-send-to-vehicle-dealership',
            'type' => 'boolean',
            'value' => true,
        ]);

        $form = Form::factory()->create();
        $section = FormSection::factory()->create(['form_id' => $form->id]);

        $form_question = FormQuestion::factory()->create([
            'form_id' => $form->id,
            'form_section_id' => $section->id,
            "name" => "Foo",
            'data' => [
                "component" => false,
                "componentName" => false,
                "meta" => null,
                "name" => "Foo",
                "type" => "foo",
            ],
        ]);

        FormAction::factory()->create([
            'form_id' => $form->id,
            "action_name" => "send-copy-via-mail",
            "fields" => [
                "send_to_field_location" => 'foo',
                "send_to_both" => true,
                "recipient" => "fallback@test.com",
                "enquiry_subject" => "Vehicle Enquiry",
                "enquiry_content" => "A new vehicle enquiry has been submitted on the website."
            ],
        ]);

        $form->refresh();

        $dealership = Dealership::factory()->create([
            'email' => 'dealer_one@test.com,dealer_two@test.com'
        ]);

        $vehicle = Vehicle::factory()->create([
            'dealership_id' => $dealership->id,
        ]);

        $details = [
            (string)$form_question->id => [
                'id' => $form_question->id,
                'answer' => 'question.recipient@test.com',
                'question' => $form_question->name,
                "type" => "recipient",
                'value' => 'question.recipient@test.com',
            ],
        ];

        $enquiry = \Mtc\Crm\Models\Enquiry::factory()->create([
            'title' => 'foo',
            'reason_type' => 'vehicle',
            'reason_id' => $vehicle->id,
            'form_id' => $form->id,
            'details' => $details,
            'data' => [
                'questions' => [
                    (string)$form_question->id => [
                        'component' => 'TextField',
                        'componentName' => 'TextField',
                        'meta' => null,
                    ],
                ],
            ],
        ]);

        EnquiryObject::factory()->create([
            'enquiry_id' => $enquiry->id,
            'object_id' => $vehicle->id,
            'object_type' => 'vehicle',
        ]);

        $action = EnquiryAction::factory()->create([
            'enquiry_id' => $enquiry->id,
            'action_name' => 'send-copy-via-mail',
            'data' => [
                'send_to_field_location' => 'Foo',
                'send_to_both' => false,
                'recipient' => 'recipient@test.com',
                'enquiry_subject' => 'my enquiry subject',
                'enquiry content' => 'lorem ipsum',
            ],
            'processed_at' => null,
            'attempts' => 0,
            'failed_at' => null,
        ]);

        $enquiry->refresh();

        new SendCopyViaMail()->handle($action, $enquiry);

        $sent_to = [];

        Mail::assertSent(NewEnquiryMail::class, function (NewEnquiryMail $mail) use (&$sent_to) {
            $sent_to[] = $mail->to[0]['address'];
            return true;
        });

        $this->assertContains('question.recipient@test.com', $sent_to);
        $this->assertContains('dealer_one@test.com', $sent_to);
        $this->assertContains('dealer_two@test.com', $sent_to);
    }

    public function testMailHasValuationsAll()
    {
        // setup
        Mail::fake();

        Settings::make([
            'tab' => 'foo-baz',
            'section' => 'lorem',
            'group' => 'ipsum',
            'name' => 'dolor',
            'type' => 'image',
            'config_key' => 'site-logo'
        ]);

        Settings::make([
            'tab' => 'Automotive',
            'section' => 'Valuation',
            'group' => 'General',
            'name' => 'Valuation price to show in confirmation email',
            'config_key' => 'valuation-email-price-to-show',
            'type' => 'string',
            'value' => 'all',
        ]);

        $form = Form::factory()->create();
        $section = FormSection::factory()->create(['form_id' => $form->id]);

        $form_question = FormQuestion::factory()->create([
            'form_id' => $form->id,
            'form_section_id' => $section->id,
            "name" => "Enquirer",
            'data' => [
                "component" => false,
                "componentName" => false,
                "meta" => null,
                "name" => "Enquirer",
                "type" => "enquirer",
            ],
        ]);

        FormAction::factory()->create([
            'form_id' => $form->id,
            "action_name" => "send-copy-via-mail",
            "fields" => [
                "send_to_field_location" => 'foo',
                "send_to_both" => false,
                "recipient" => "fallback@test.com",
                "enquiry_subject" => "Vehicle Enquiry",
                "enquiry_content" => "A new vehicle enquiry has been submitted on the website."
            ],
        ]);

        $form->refresh();

        $details = [
            (string)$form_question->id => [
                'id' => $form_question->id,
                'answer' => 'enquirer@test.com',
                'question' => $form_question->name,
                "type" => "recipient",
                'value' => 'enquirer@test.com',
            ],
        ];

        $valuation = VehicleValuation::factory()->create([
            'make' => 'foo',
            'model' => 'bar',
            'registration' => 'abc123',
            'mileage' => 12345,
            'retail_price' => 1122,
            'average_price' => 789,
            'clean_price' => 999,
            'below_price' => 456,
        ]);

        $enquiry = Enquiry::factory()->create([
            'form_id' => $form->id,
            'details' => $details,
            'reason_type' => 'valuation',
            'reason_id' => $valuation->id,
            'data' => [
                'questions' => [
                    (string)$form_question->id => [
                        'component' => 'TextField',
                        'componentName' => 'TextField',
                        'meta' => null,
                    ],
                ],
            ],
        ]);

        EnquiryObject::factory()->create([
            'enquiry_id' => $enquiry->id,
            'object_type' => 'valuation',
            'object_id' => $valuation->id,
        ]);

        $action = EnquiryAction::factory()->create([
            'enquiry_id' => $enquiry->id,
            'action_name' => 'send-copy-via-mail',
            'data' => [
                'send_to_both' => false,
                'recipient' => 'fallback@test.com',
                'enquiry_subject' => 'my enquiry subject',
                'enquiry content' => 'lorem ipsum',
            ],
            'processed_at' => null,
            'attempts' => 0,
            'failed_at' => null,
        ]);

        $enquiry->refresh();

        new SendCopyViaMail()->handle($action, $enquiry);

        Mail::assertSent(NewEnquiryMail::class, fn (NewEnquiryMail $mail) => $mail->assertSeeInOrderInHtml([
            'Retail value',
            '1122',
            'Clean value',
            '999',
            'Average value',
            '789',
            'Below value',
            '456',
        ]));
    }

    public function testMailHasValuationsAverage()
    {
        // setup
        Mail::fake();

        Settings::make([
            'tab' => 'foo-baz',
            'section' => 'lorem',
            'group' => 'ipsum',
            'name' => 'dolor',
            'type' => 'image',
            'config_key' => 'site-logo'
        ]);

        Settings::make([
            'tab' => 'Automotive',
            'section' => 'Valuation',
            'group' => 'General',
            'name' => 'Valuation price to show in confirmation email',
            'config_key' => 'valuation-email-price-to-show',
            'type' => 'string',
            'value' => 'average_price',
        ]);

        $form = Form::factory()->create();
        $section = FormSection::factory()->create(['form_id' => $form->id]);

        $form_question = FormQuestion::factory()->create([
            'form_id' => $form->id,
            'form_section_id' => $section->id,
            "name" => "Enquirer",
            'data' => [
                "component" => false,
                "componentName" => false,
                "meta" => null,
                "name" => "Enquirer",
                "type" => "enquirer",
            ],
        ]);

        FormAction::factory()->create([
            'form_id' => $form->id,
            "action_name" => "send-copy-via-mail",
            "fields" => [
                "send_to_field_location" => 'foo',
                "send_to_both" => false,
                "recipient" => "fallback@test.com",
                "enquiry_subject" => "Vehicle Enquiry",
                "enquiry_content" => "A new vehicle enquiry has been submitted on the website."
            ],
        ]);

        $form->refresh();

        $details = [
            (string)$form_question->id => [
                'id' => $form_question->id,
                'answer' => 'enquirer@test.com',
                'question' => $form_question->name,
                "type" => "recipient",
                'value' => 'enquirer@test.com',
            ],
        ];

        $valuation = VehicleValuation::factory()->create([
            'make' => 'foo',
            'model' => 'bar',
            'registration' => 'abc123',
            'mileage' => 12345,
            'average_price' => 789,
            'clean_price' => 999,
            'below_price' => 456,
        ]);

        $enquiry = Enquiry::factory()->create([
            'form_id' => $form->id,
            'details' => $details,
            'reason_type' => 'valuation',
            'reason_id' => $valuation->id,
            'data' => [
                'questions' => [
                    (string)$form_question->id => [
                        'component' => 'TextField',
                        'componentName' => 'TextField',
                        'meta' => null,
                    ],
                ],
            ],
        ]);

        EnquiryObject::factory()->create([
            'enquiry_id' => $enquiry->id,
            'object_type' => 'valuation',
            'object_id' => $valuation->id,
        ]);

        $action = EnquiryAction::factory()->create([
            'enquiry_id' => $enquiry->id,
            'action_name' => 'send-copy-via-mail',
            'data' => [
                'send_to_both' => false,
                'recipient' => 'fallback@test.com',
                'enquiry_subject' => 'my enquiry subject',
                'enquiry content' => 'lorem ipsum',
            ],
            'processed_at' => null,
            'attempts' => 0,
            'failed_at' => null,
        ]);

        $enquiry->refresh();

        new SendCopyViaMail()->handle($action, $enquiry);

        Mail::assertSent(NewEnquiryMail::class, fn (NewEnquiryMail $mail) => $mail->assertSeeInOrderInHtml([
                'Average value',
                '789',
            ])
            && $mail->assertDontSeeInHtml('Clean value')
            && $mail->assertDontSeeInHtml('Below value')
            && $mail->assertDontSeeInHtml('Retail value'));
    }

    public function testAllElectricCustomerCopyShowsAllRowsWhenSettingIsAll()
    {
        Mail::fake();

        $this->seedAllElectricThemeSettings();
        Settings::make([
            'tab' => 'Automotive',
            'section' => 'Valuation',
            'group' => 'General',
            'name' => 'Valuation price to show in confirmation email',
            'config_key' => 'valuation-email-price-to-show',
            'type' => 'string',
            'value' => 'all',
        ]);

        $enquiry = $this->buildValuationEnquiryForAllElectric([
            'average_price' => 789,
            'clean_price' => 999,
            'below_price' => 456,
        ]);

        Mail::to('customer@test.com')->send(new NewEnquiryCustomerCopyNotification($enquiry));

        Mail::assertSent(
            NewEnquiryCustomerCopyNotification::class,
            fn (NewEnquiryCustomerCopyNotification $mail) =>
                $mail->assertSeeInHtml('Clean Price')
                && $mail->assertSeeInHtml('999')
                && $mail->assertSeeInHtml('Average Price')
                && $mail->assertSeeInHtml('789')
                && $mail->assertSeeInHtml('Below Average Price')
                && $mail->assertSeeInHtml('456')
        );
    }

    public function testAllElectricCustomerCopyShowsOnlyChosenRowWhenSettingIsCleanPrice()
    {
        Mail::fake();

        $this->seedAllElectricThemeSettings();
        Settings::make([
            'tab' => 'Automotive',
            'section' => 'Valuation',
            'group' => 'General',
            'name' => 'Valuation price to show in confirmation email',
            'config_key' => 'valuation-email-price-to-show',
            'type' => 'string',
            'value' => 'clean_price',
        ]);

        $enquiry = $this->buildValuationEnquiryForAllElectric([
            'average_price' => 789,
            'clean_price' => 999,
            'below_price' => 456,
        ]);

        Mail::to('customer@test.com')->send(new NewEnquiryCustomerCopyNotification($enquiry));

        Mail::assertSent(
            NewEnquiryCustomerCopyNotification::class,
            fn (NewEnquiryCustomerCopyNotification $mail) =>
                $mail->assertSeeInHtml('Clean Price')
                && $mail->assertSeeInHtml('999')
                && $mail->assertDontSeeInHtml('Average Price')
                && $mail->assertDontSeeInHtml('Below Average Price')
        );
    }

    private function seedAllElectricThemeSettings(): void
    {
        Settings::make([
            'tab' => 'foo-baz',
            'section' => 'lorem',
            'group' => 'ipsum',
            'name' => 'dolor',
            'type' => 'image',
            'config_key' => 'site-logo',
        ]);

        Settings::make([
            'tab' => 'Site',
            'section' => 'Mail',
            'group' => 'General',
            'name' => 'Customer mail theme',
            'config_key' => 'mail-customer-theme',
            'type' => 'string',
            'value' => 'all-electric',
        ]);
    }

    private function buildValuationEnquiryForAllElectric(array $prices)
    {
        $type = EnquiryType::factory()->create([
            'name' => 'Valuation',
            'template_name' => 'valuation',
        ]);

        $form = Form::factory()->create(['type_id' => $type->id]);

        $valuation = VehicleValuation::factory()->create(array_merge([
            'make' => 'foo',
            'model' => 'bar',
            'registration' => 'abc123',
            'mileage' => 12345,
        ], $prices));

        $created = Enquiry::factory()->create([
            'form_id' => $form->id,
            'valuation_id' => $valuation->id,
            'reason_type' => 'valuation',
            'reason_id' => $valuation->id,
            'details' => [],
            'data' => ['questions' => []],
        ]);

        return Enquiry::query()
            ->with(['form.type', 'valuation', 'actions'])
            ->findOrFail($created->id);
    }
}
