<?php

namespace Tests\Unit;

use App\LeadExports\LeadExportOpenApiGenerator;
use App\Models\LeadExport;
use Tests\TestCase;

class LeadExportOpenApiGeneratorTest extends TestCase
{
    private function makeMappedExport(): LeadExport
    {
        $leadExport = new LeadExport();
        $leadExport->name = 'Partner CRM';
        $leadExport->slug = 'partner-crm';
        $leadExport->mode = LeadExport::MODE_MAPPED;
        $leadExport->url = 'https://crm.example.com/api/leads';
        $leadExport->field_map = [
            ['destination' => 'email', 'label' => 'Email', 'type' => 'string'],
            ['destination' => 'vehicle.year', 'label' => 'Year', 'type' => 'integer'],
            ['destination' => 'vehicle.price', 'label' => 'Price', 'type' => 'float'],
        ];

        return $leadExport;
    }

    public function test_it_uses_the_configured_url_as_server_and_path(): void
    {
        $doc = (new LeadExportOpenApiGenerator())->generate($this->makeMappedExport());

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

    public function test_mapped_schema_reflects_types_and_nesting(): void
    {
        $doc = (new LeadExportOpenApiGenerator())->generate($this->makeMappedExport());
        $schema = $doc['paths']['/api/leads']['post']['requestBody']['content']['application/json']['schema'];

        $this->assertSame('string', $schema['properties']['email']['type']);
        $this->assertSame('object', $schema['properties']['vehicle']['type']);
        $this->assertSame('integer', $schema['properties']['vehicle']['properties']['year']['type']);
        $this->assertSame('number', $schema['properties']['vehicle']['properties']['price']['type']);
    }

    public function test_passthrough_schema_is_a_free_form_object(): void
    {
        $leadExport = new LeadExport();
        $leadExport->name = 'Free Form';
        $leadExport->slug = 'free-form';
        $leadExport->mode = LeadExport::MODE_PASSTHROUGH;
        $leadExport->url = 'https://crm.example.com/leads';

        $doc = (new LeadExportOpenApiGenerator())->generate($leadExport);
        $schema = $doc['paths']['/leads']['post']['requestBody']['content']['application/json']['schema'];

        $this->assertSame('object', $schema['type']);
        $this->assertSame('string', $schema['additionalProperties']['type']);
    }

    public function test_missing_url_falls_back_to_a_placeholder(): void
    {
        $leadExport = new LeadExport();
        $leadExport->name = 'No URL';
        $leadExport->slug = 'no-url';
        $leadExport->mode = LeadExport::MODE_MAPPED;

        $doc = (new LeadExportOpenApiGenerator())->generate($leadExport);

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