<?php

it('hardens transfer queue with type validation retry caps and stale lock recovery', function (): void {
    $content = file_get_contents(app_path('classes/economic_transfer_queue.php'));

    expect($content)->not->toBeFalse();
    expect($content)->toContain('private const STALE_PROCESSING_LOCK_SECONDS = 900');
    expect($content)->toContain('$this->validateTransferType($transfer_type)');
    expect($content)->toContain('$this->normalizePayloadForTransferType($transfer_type, $payload, $created_by)');
    expect($content)->toContain('$this->findActiveJobByTarget($transfer_type, $payload, $created_by)');
    expect($content)->toContain('$max_attempts = max(1, min(10, $max_attempts));');
    expect($content)->toContain('private function normalizePayloadForTransferType(string $transfer_type, array $payload, int $created_by): array');
    expect($content)->toContain('private function normalizeBooleanPayloadValue(mixed $value, string $field_name, int $created_by): bool');
    expect($content)->toContain('private function findActiveJobByJsonNumericTarget(string $transfer_type, string $json_path, int $target_value, int $created_by): ?array');
    expect($content)->toContain('ECONOMIC_TRANSFER_JOB_DEDUPED');
    expect($content)->toContain('ECONOMIC_TRANSFER_JOB_VALIDATION_REJECTED');
    expect($content)->toContain('Queue job reached max retry attempts');
    expect($content)->toContain('AND attempts < max_attempts');
    expect($content)->toContain('public function listJobs(array $statuses = [], int $limit = 50, int $offset = 0, ?string $transfer_type = null): array');
    expect($content)->toContain('public function getJobByIdForUser(int $job_id, int $created_by): ?array');
    expect($content)->toContain('public function listJobsForCreatedBy(array $statuses = [], int $limit = 50, int $offset = 0, ?string $transfer_type = null, int $created_by = 0): array');
    expect($content)->toContain('public function countJobsForCreatedBy(array $statuses = [], ?string $transfer_type = null, int $created_by = 0): int');
    expect($content)->toContain('public function retryJobForUser(int $job_id, int $created_by): array');
    expect($content)->toContain('public function countJobs(array $statuses = [], ?string $transfer_type = null): int');
    expect($content)->toContain('public function listMonitorJobsForUser(int $user_id, int $limit = 50, ?string $transfer_type = null): array');
    expect($content)->toContain('public function dismissTerminalJobForUser(int $job_id, int $user_id): array');
    expect($content)->toContain('public function dismissTerminalJobsForUser(int $user_id, ?string $transfer_type = null): int');
    expect($content)->toContain('economic_transfer_queue_job_dismissals');
    expect($content)->toContain("Only completed or failed queue jobs can be dismissed");
    expect($content)->toContain('$this->clearDismissalsForJob($job_id);');
    expect($content)->toContain('private function clearDismissalsForJob(int $job_id): void');
    expect($content)->toContain('public function processPendingByTransferType(string $transfer_type, int $limit = 10): array');
    expect($content)->toContain('private function processPendingInternal(int $limit = 5, ?string $transfer_type = null): array');
    expect($content)->toContain('private function claimNextJob(?string $transfer_type = null): ?array');
    expect($content)->toContain("transfer_type = '");
    expect($content)->toContain('private function sanitizeStatuses(array $statuses): array');
    expect($content)->toContain('private function buildListJobsWhereClause(array $statuses = [], ?string $transfer_type = null): string');
    expect($content)->toContain('private function releaseStaleProcessingLocks(): void');
});
