<?php

declare(strict_types=1);

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

test('pricing_tiers table exists with required columns', function () {
    expect(Schema::hasTable('pricing_tiers'))->toBeTrue();

    foreach ([
        'id',
        'tier_no',
        'leads_in_tier',
        'price_per_lead_kopecks',
        'is_active',
        'effective_from',
        'created_at',
        'updated_at',
    ] as $col) {
        expect(Schema::hasColumn('pricing_tiers', $col))->toBeTrue("column {$col} missing");
    }
});

test('pricing_tiers tier_no constrained to 1..7', function () {
    $check = DB::selectOne(
        "SELECT pg_get_constraintdef(c.oid) AS def
         FROM pg_constraint c
         JOIN pg_class t ON c.conrelid = t.oid
         WHERE t.relname = 'pricing_tiers' AND c.conname = 'chk_pricing_tiers_tier_no'"
    );

    expect($check)->not->toBeNull();
    expect($check->def)
        ->toContain('1')
        ->toContain('7');
});

test('pricing_tiers has unique on (tier_no, effective_from)', function () {
    $idx = DB::selectOne(
        "SELECT indexdef
         FROM pg_indexes
         WHERE tablename = 'pricing_tiers'
           AND indexname = 'pricing_tiers_tier_effective_unique'"
    );

    expect($idx)->not->toBeNull();
    expect($idx->indexdef)
        ->toContain('UNIQUE')
        ->toContain('tier_no')
        ->toContain('effective_from');
});

test('pricing_tiers price stored in kopecks (integer, not float)', function () {
    $col = DB::selectOne(
        "SELECT data_type
         FROM information_schema.columns
         WHERE table_name = 'pricing_tiers' AND column_name = 'price_per_lead_kopecks'"
    );

    expect($col)->not->toBeNull();
    expect($col->data_type)->toBe('integer');
});
