"""Alert webhook (Teams etc.): payload formats + send_alert fan-out."""
from unittest.mock import patch

from shopdb.utils.mailer import _webhook_payload, send_webhook, send_alert

CFG = {'url': 'https://teams.example/webhook', 'format': 'teams'}


def test_payload_formats():
    teams = _webhook_payload('teams', 'Title', 'Body')
    assert teams['@type'] == 'MessageCard' and teams['title'] == 'Title'
    card = _webhook_payload('adaptivecard', 'T', 'B')
    assert card['attachments'][0]['contentType'].endswith('card.adaptive')
    assert _webhook_payload('json', 'T', 'B') == {'title': 'T', 'text': 'B'}


def test_noop_when_unset(app):
    with app.app_context():
        with patch('shopdb.utils.mailer.get_webhook_config',
                   return_value={'url': '', 'format': 'teams'}):
            ok, err = send_webhook('t', 'b')
            assert ok is False and err is None


def test_posts_when_configured(app):
    with app.app_context():
        with patch('shopdb.utils.mailer.get_webhook_config', return_value=CFG), \
             patch('shopdb.utils.mailer.requests') as mock_requests:
            mock_requests.post.return_value.status_code = 200
            ok, err = send_webhook('Low stock', 'x down')
            assert ok is True and err is None
            assert mock_requests.post.call_args[0][0] == CFG['url']
            assert mock_requests.post.call_args[1]['json']['@type'] == 'MessageCard'


def test_send_alert_fans_out_to_webhook(app):
    with app.app_context():
        with patch('shopdb.utils.mailer.get_webhook_config', return_value=CFG), \
             patch('shopdb.utils.mailer.requests') as mock_requests:
            mock_requests.post.return_value.status_code = 200
            send_alert('Subject', '<p>Body</p>')
            assert mock_requests.post.called  # webhook fired even with no email recipients
