<?php

namespace Tests\Tenant;

use App\Http\Controllers\LeadExportController;
use App\Models\LeadExport;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Tests\TenantTestCase;

class LeadExportDocsTest extends TenantTestCase
{
    private function controller(): LeadExportController
    {
        return new LeadExportController();
    }

    public function testDocsReturnsOpenApiForMappedExport(): void
    {
        LeadExport::create([
            'name' => 'Partner CRM',
            'type' => 'lead',
            'active' => true,
            'mode' => LeadExport::MODE_MAPPED,
            'url' => 'https://crm.example.com/leads',
            'field_map' => [['destination' => 'contact.email', 'label' => 'Email', 'type' => 'string']],
        ]);

        $response = $this->controller()->docs('partner-crm');
        $doc = $response->getData(true);

        $this->assertSame('3.0.0', $doc['openapi']);
        $this->assertSame('https://crm.example.com', $doc['servers'][0]['url']);
        $this->assertArrayHasKey('/leads', $doc['paths']);
    }

    public function testDocsAvailableForInactiveExport(): void
    {
        LeadExport::create(['name' => 'Dormant', 'type' => 'lead', 'active' => false]);

        $response = $this->controller()->docs('dormant');

        $this->assertSame(200, $response->getStatusCode());
    }

    public function testDocsUnknownSlugReturns404(): void
    {
        $this->expectException(ModelNotFoundException::class);

        $this->controller()->docs('does-not-exist');
    }
}
