<?php

declare(strict_types=1);

use App\Rules\SignalIdentifier\DomainIdentifier;
use App\Rules\SignalIdentifier\PhoneIdentifier;
use App\Rules\SignalIdentifier\SmsSenderRule;
use Illuminate\Support\Facades\Validator;

dataset('domains_valid', [
    ['example.com'],
    ['vashinvestor.ru'],
    ['sub.example.co.uk'],
    ['site-with-dash.ru'],
]);

dataset('domains_invalid', [
    ['EXAMPLE.COM'],            // верхний регистр
    ['no-tld'],                 // без точки
    ['has spaces.com'],
    ['http://example.com'],     // с протоколом
    ['example.com/path'],       // с путём
    ['example..com'],           // двойная точка
]);

test('DomainIdentifier accepts valid', function (string $value) {
    $v = Validator::make(['x' => $value], ['x' => [new DomainIdentifier]]);
    expect($v->fails())->toBeFalse("rejected valid {$value}");
})->with('domains_valid');

test('DomainIdentifier rejects invalid', function (string $value) {
    $v = Validator::make(['x' => $value], ['x' => [new DomainIdentifier]]);
    expect($v->fails())->toBeTrue("accepted invalid {$value}");
})->with('domains_invalid');

dataset('phones_valid', [
    ['79991234567'],
    ['74955551212'],
    ['78001234567'],
]);

dataset('phones_invalid', [
    ['89991234567'],            // не 7 в начале
    ['7999123456'],             // 10 цифр
    ['799912345678'],           // 12 цифр
    ['+79991234567'],           // плюс
    ['7 999 123 45 67'],        // пробелы
    ['79991234abc'],            // буквы
]);

test('PhoneIdentifier accepts valid', function (string $v) {
    $val = Validator::make(['x' => $v], ['x' => [new PhoneIdentifier]]);
    expect($val->fails())->toBeFalse();
})->with('phones_valid');

test('PhoneIdentifier rejects invalid', function (string $v) {
    $val = Validator::make(['x' => $v], ['x' => [new PhoneIdentifier]]);
    expect($val->fails())->toBeTrue();
})->with('phones_invalid');

test('SmsSenderRule accepts alpha and short numeric', function () {
    foreach (['TINKOFF', 'SBER', '900', '1234', 'BANK-1', 'alfa_bank'] as $v) {
        $r = Validator::make(['x' => $v], ['x' => [new SmsSenderRule]]);
        expect($r->fails())->toBeFalse("rejected valid {$v}");
    }
});

test('SmsSenderRule rejects 11-digit phone (supplier blocks it)', function () {
    foreach (['79991234567', '12345678901'] as $v) {
        $r = Validator::make(['x' => $v], ['x' => [new SmsSenderRule]]);
        expect($r->fails())->toBeTrue("accepted invalid {$v}");
    }
});

test('SmsSenderRule rejects too long', function () {
    $longString = str_repeat('A', 31);
    $r = Validator::make(['x' => $longString], ['x' => [new SmsSenderRule]]);
    expect($r->fails())->toBeTrue();
});

test('SmsSenderRule combined with required rejects empty string', function () {
    // Laravel skips non-implicit rules для пустых строк; в реальном FormRequest
    // signal-поля будут обязательными — проверяем связку 'required' + наша rule.
    $r = Validator::make(['x' => ''], ['x' => ['required', new SmsSenderRule]]);
    expect($r->fails())->toBeTrue();
});

test('SmsSenderRule rejects special chars', function () {
    foreach (['SBER!', 'a@b', 'with space', 'punto.ru'] as $v) {
        $r = Validator::make(['x' => $v], ['x' => [new SmsSenderRule]]);
        expect($r->fails())->toBeTrue("accepted invalid {$v}");
    }
});
