<?php

it('defines idempotent normalized product restriction schema and one-time exact legacy seeding', function (): void {
    $schema = file_get_contents(app_path('classes/customer_rule_product_restriction_schema_bootstrap.php'));

    expect($schema)
        ->toContain('CREATE TABLE IF NOT EXISTS customer_rule_product_restrictions')
        ->toContain('CREATE TABLE IF NOT EXISTS customer_rule_product_collections')
        ->toContain('CREATE TABLE IF NOT EXISTS customer_rule_product_collection_products')
        ->toContain('legacy_exact_product_sets_v1')
        ->toContain('customer_rule_product_migrations')
        ->toContain('po.option_id = p.id')
        ->toContain('Unable to record customer-rule product migration');
});

it('uses configured exact product ids in runtime order and invoice enforcement', function (): void {
    $productRules = file_get_contents(app_path('classes/customer_product_rule_service.php'));
    $policy = file_get_contents(app_path('classes/customer_order_product_policy.php'));
    $invoice = file_get_contents(app_path('classes/invoice_period_flag_service.php'));

    expect($productRules)->toContain('violationForCustomerProduct')
        ->and($policy)->toContain('violationForCustomerProduct')
        ->and($invoice)->toContain("['__disabled_products']")
        ->and($invoice)->not->toContain('customer_product_rule_service::isStandaloneAdditionalServiceRow($row)')
        ->and($invoice)->not->toContain('$this->rowIsTankCleaningProduct($row)');
});

it('wires versioned management permissions and structured customer attributes', function (): void {
    $route = file_get_contents(app_path('routes/superuserCustomerRuleProductRestrictionsRoute.php'));
    $attributes = file_get_contents(app_path('routes/customerAttributes.php'));

    expect($route)
        ->toContain('/superuser/customer-rules/product-restrictions')
        ->toContain('requireClassicSuperuserAnyPermission([')
        ->toContain("'superuser_customer_rules_view'")
        ->toContain("requireClassicSuperuserPermission('superuser_customer_rules_manage')")
        ->and($attributes)->toContain('enrichAttributes(')
        ->and($attributes)->toContain('SUPPORTED_ATTRIBUTES');
});

it('fails closed when configured restriction reads cannot be completed', function (): void {
    $service = file_get_contents(app_path('classes/customer_rule_product_restriction_service.php'));

    expect($service)
        ->toContain('Unable to load customer-rule restriction version')
        ->toContain('Unable to load customer-rule restriction collections')
        ->toContain('Unable to load active customer-rule product restrictions')
        ->toContain('foreach (self::PRODUCT_IMPACT_ATTRIBUTES as $attribute)')
        ->toContain('Unable to validate collection products');
});

it('uses strict datetime-safe deleted row predicates', function (): void {
    $schema = file_get_contents(app_path('classes/customer_rule_product_restriction_schema_bootstrap.php'));
    $service = file_get_contents(app_path('classes/customer_rule_product_restriction_service.php'));
    $orderItems = file_get_contents(app_path('objects/order_items_o.php'));

    expect($schema)->toContain('p.deleted_at IS NULL')
        ->not->toContain("p.deleted_at = ''")
        ->and($service)->toContain('p.deleted_at IS NULL')
        ->not->toContain("p.deleted_at = ''")
        ->and($orderItems)->toContain('AND deleted_at IS NULL')
        ->not->toContain("deleted_at = ''");
});
