<?php

namespace Tests\Feature\Seeders;

use App\Models\LeadExport;
use App\Models\LeadExportTemplate;
use Database\Seeders\Global\DefaultLeadExportTemplateSeed;
use Tests\DatabaseTestCase;

class DefaultLeadExportTemplateSeedTest extends DatabaseTestCase
{
    public function test_it_seeds_a_published_one_to_one_lead_template(): void
    {
        (new DefaultLeadExportTemplateSeed())->run();

        $template = LeadExportTemplate::query()->where('title', 'Default lead (1:1)')->first();

        $this->assertNotNull($template);
        $this->assertTrue($template->is_published);
        $this->assertSame('lead', $template->type);
        $this->assertSame('json', $template->format);
        $this->assertSame(LeadExport::MODE_MAPPED, $template->mode);

        $destinations = collect($template->field_map)->pluck('destination');
        $this->assertContains('email', $destinations);
        $this->assertContains('vehicle.make', $destinations);
        $this->assertContains('vehicle.year', $destinations);
    }

    public function test_it_is_idempotent(): void
    {
        (new DefaultLeadExportTemplateSeed())->run();
        (new DefaultLeadExportTemplateSeed())->run();

        $this->assertSame(
            1,
            LeadExportTemplate::query()->where('title', 'Default lead (1:1)')->count()
        );
    }
}
