<?php

declare(strict_types=1);

use App\Models\SupplierProject;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\Concerns\SharesSupplierPdo;

uses(DatabaseTransactions::class, SharesSupplierPdo::class);

it('allows same (platform, unique_key) with different subject_code', function (): void {
    SupplierProject::query()->create([
        'platform' => 'B1', 'signal_type' => 'site', 'unique_key' => 'okna.ru',
        'subject_code' => 82, 'current_limit' => 0, 'sync_status' => 'ok',
    ]);
    SupplierProject::query()->create([
        'platform' => 'B1', 'signal_type' => 'site', 'unique_key' => 'okna.ru',
        'subject_code' => 83, 'current_limit' => 0, 'sync_status' => 'ok',
    ]);

    expect(SupplierProject::query()->where('unique_key', 'okna.ru')->count())->toBe(2);
});

it('rejects duplicate (platform, unique_key, subject_code) including NULL pool', function (): void {
    SupplierProject::query()->create([
        'platform' => 'B1', 'signal_type' => 'site', 'unique_key' => 'pool.ru',
        'subject_code' => null, 'current_limit' => 0, 'sync_status' => 'ok',
    ]);

    expect(fn () => SupplierProject::query()->create([
        'platform' => 'B1', 'signal_type' => 'site', 'unique_key' => 'pool.ru',
        'subject_code' => null, 'current_limit' => 0, 'sync_status' => 'ok',
    ]))->toThrow(QueryException::class);
});

it('rejects subject_code out of 1..89 range', function (): void {
    expect(fn () => SupplierProject::query()->create([
        'platform' => 'B1', 'signal_type' => 'site', 'unique_key' => 'bad.ru',
        'subject_code' => 90, 'current_limit' => 0, 'sync_status' => 'ok',
    ]))->toThrow(QueryException::class);
});
