from datetime import date
from types import SimpleNamespace

from app.services.certificate_templates import (
    CLASSIC_TEMPLATE_CODE,
    PRACTICE_CAMP_TEMPLATE_CODE,
    get_certificate_template,
    list_certificate_templates,
)
from app.services.pdf import font, render_certificate_image


def test_builtin_certificate_templates_have_assets():
    templates = list_certificate_templates()

    assert {item.code for item in templates} == {CLASSIC_TEMPLATE_CODE, PRACTICE_CAMP_TEMPLATE_CODE}
    assert all(item.asset_path.exists() for item in templates)


def test_practice_camp_template_renders_four_dynamic_values():
    certificate = SimpleNamespace(
        template_code=PRACTICE_CAMP_TEMPLATE_CODE,
        course_start_date=date(2026, 3, 1),
        course_end_date=date(2026, 8, 31),
        issue_date=date(2026, 9, 5),
    )
    learner = SimpleNamespace(current_name="张晓慧")

    rendered = render_certificate_image(certificate, learner, None)
    template = get_certificate_template(PRACTICE_CAMP_TEMPLATE_CODE)

    assert rendered.size == (3437, 2551)
    assert template.dynamic_fields == ("姓名", "课程开始日期", "课程结束日期", "发证日期")


def test_certificate_bold_fonts_use_real_bold_faces():
    for kind in ("kai", "song"):
        regular = font(32, kind, False)
        bold = font(32, kind, True)

        assert regular.getname()[1] != bold.getname()[1]
        assert "Bold" in bold.getname()[1]
