from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


def read(rel):
    return (ROOT / rel).read_text(encoding="utf-8")


def test_contact_question_asks_when_either_phone_or_email_missing():
    text = read("templates/cv-report.php")
    assert "Contact is not optional for a usable resume" in text
    assert "|| trim((string) ($cv->phone ?? '')) === ''" in text
    assert "What phone number should appear on the resume?" in text
    assert "What email address should appear on the resume?" in text


def test_direct_rewrite_request_blocks_before_credit_spend_when_required_facts_missing():
    text = read("includes/class-ssc-ch-shortcodes.php")
    guard_pos = text.index("Server-side guardrail: a direct rewrite URL must not spend")
    spend_pos = text.index("'activity_type' => 'spend'", guard_pos)
    assert guard_pos < spend_pos
    assert "add_query_arg('fix_required', '1', $report_url)" in text
    assert "!$has_phone || !$has_email || !$has_availability || !$fix_answers_seen" in text
    assert "email, phone, ai_rewrite_text" in text


def test_fix_wizard_contact_answer_updates_structured_email_and_phone_when_possible():
    text = read("includes/class-ssc-ch-shortcodes.php")
    assert "Treat contact as structured data when possible" in text
    assert "$update['email'] = sanitize_email($m_email[0]);" in text
    assert "$update['phone'] = SSC_CH_Sanitizer::text($m_phone[0], 50);" in text


def test_rewrite_prompt_forbids_placeholders_and_candidate_notes():
    text = read("includes/class-ssc-ch-openrouter.php")
    assert "Use candidate-supplied Fix Wizard answers as authoritative facts" in text
    assert "Do not output placeholders like [Phone number needed]" in text
    assert "Do not add a NOTES FOR CANDIDATE section" in text
