<?php

namespace Tests\Unit\B2B;

use Mtc\Modules\B2B\Services\TradeRegistrationService;
use Tests\TestCase;

class TradeRegistrationServiceTest extends TestCase
{
    private TradeRegistrationService $service;

    protected function setUp(): void
    {
        parent::setUp();
        $this->service = new TradeRegistrationService();
    }

    private function validData(array $overrides = []): array
    {
        return array_merge([
            'company_name' => 'Test Trading Ltd',
            'billing_firstname' => 'John',
            'billing_lastname' => 'Smith',
            'email' => 'trade-test-' . uniqid() . '@example.com',
            'phone' => '07700900000',
            'address_1' => '123 Trade Street',
            'address_2' => 'Suite 100',
            'city' => 'London',
            'county' => 'Greater London',
            'postcode' => 'SW1A 1AA',
            'message' => 'Test application',
        ], $overrides);
    }

    public function test_required_field_validation()
    {
        $result = $this->service->register([]);

        $this->assertNull($result);
        $errors = $this->service->getErrors();
        $this->assertArrayHasKey('company_name', $errors);
        $this->assertArrayHasKey('billing_firstname', $errors);
        $this->assertArrayHasKey('email', $errors);
        $this->assertArrayHasKey('phone', $errors);
        $this->assertArrayHasKey('address_1', $errors);
        $this->assertArrayHasKey('city', $errors);
        $this->assertArrayHasKey('postcode', $errors);
    }

    public function test_partial_required_field_validation()
    {
        $result = $this->service->register([
            'company_name' => 'Test Co',
            'email' => 'test@example.com',
        ]);

        $this->assertNull($result);
        $errors = $this->service->getErrors();
        $this->assertArrayHasKey('billing_firstname', $errors);
        $this->assertArrayHasKey('phone', $errors);
        $this->assertArrayHasKey('address_1', $errors);
        $this->assertArrayHasKey('city', $errors);
        $this->assertArrayHasKey('postcode', $errors);
        $this->assertArrayNotHasKey('company_name', $errors);
        $this->assertArrayNotHasKey('email', $errors);
    }

    public function test_duplicate_email_validation()
    {
        // This test requires a fully synced DB schema (members.type column).
        // Skip if schema is incomplete (e.g. local dev without db-sync).
        $columns = \Illuminate\Database\Capsule\Manager::select('SHOW COLUMNS FROM members WHERE Field = "type"');
        if (empty($columns)) {
            $this->markTestSkipped('members.type column missing — run db-sync to get full schema');
        }

        $email = 'dupe-test-' . uniqid() . '@example.com';
        $member = new \Mtc\Modules\Members\Models\Member();
        $member->email = $email;
        $member->save();

        $result = $this->service->register($this->validData(['email' => $email]));

        $this->assertNull($result);
        $this->assertArrayHasKey('email', $this->service->getErrors());
    }

    public function test_whitespace_only_fields_fail_validation()
    {
        $result = $this->service->register([
            'company_name' => '   ',
            'billing_firstname' => '  ',
            'billing_lastname' => '  ',
            'email' => '  ',
            'phone' => '  ',
            'address_1' => '  ',
            'city' => '  ',
            'postcode' => '  ',
        ]);

        $this->assertNull($result);
        $errors = $this->service->getErrors();
        $this->assertCount(8, $errors);
    }
}
