import json
from http import HTTPMethod, HTTPStatus

from ..conftest import HttpApiProxy, LambdaContext


def test_billing(
    mock_app,
    dynamodb_seeds,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    org_id = 'edp8njvgQuzNkLx2ySNfAD'
    # This data was added from seeds
    r = mock_app.lambda_handler(
        http_api_proxy(
            raw_path=f'/billing/{org_id}',
            method=HTTPMethod.GET,
            queryStringParameters={
                'start_date': '2025-07-01',
                'end_date': '2025-07-31',
            },
        ),
        lambda_context,
    )

    assert r['statusCode'] == HTTPStatus.OK
    data = json.loads(r['body'])
    expected = [
        {
            'sk': 'START#2025-07-01#END#2025-07-31#SCHEDULE#AUTO_CLOSE',
            'created_at': '2025-07-24T15:46:43.312549-03:00',
            'id': 'BILLING#ORG#edp8njvgQuzNkLx2ySNfAD',
            'ttl': 1754017200,
        },
        {
            'enrolled_at': '2025-07-25T03:31:11.736247-03:00',
            'author': {'name': 'Carolina Brand', 'id': 'SMEXYk5MQkKCzknJpxqr8n'},
            'sk': 'START#2025-07-01#END#2025-07-31#ENROLLMENT#d2124d5a-caaf-4e27-9edb-8380faf15f35',
            'course': {
                'name': 'NR-11 – Transporte, movimentação, armazenagem e manuseio de materiais',
                'id': 'a810dd22-56c0-4d9b-8cd2-7e2ee9c45839',
            },
            'created_at': '2025-07-25T03:31:17.306858-03:00',
            'id': 'BILLING#ORG#edp8njvgQuzNkLx2ySNfAD',
            'unit_price': 87.2,
            'user': {'name': 'ERICK ALVES DOS SANTOS', 'id': '02157895558'},
        },
        {
            'enrolled_at': '2025-07-24T15:46:37.162960-03:00',
            'author': {'name': 'Carolina Brand', 'id': 'SMEXYk5MQkKCzknJpxqr8n'},
            'sk': 'START#2025-07-01#END#2025-07-31#ENROLLMENT#556e99cf-18b2-459c-a46d-f71a807ba551',
            'course': {
                'name': 'NR-06 - Equipamento de Proteção Individual - EPI',
                'id': '5c119d4b-573c-4d8d-a99d-63756af2f4c5',
            },
            'created_at': '2025-07-24T16:42:41.673797-03:00',
            'id': 'BILLING#ORG#edp8njvgQuzNkLx2ySNfAD',
            'unit_price': 79.2,
            'user': {'name': 'ERICK ALVES DOS SANTOS', 'id': '02157895558'},
        },
        {
            'sk': 'START#2025-07-01#END#2025-07-31',
            'created_at': '2025-07-24T15:46:43.312549-03:00',
            'id': 'BILLING#ORG#edp8njvgQuzNkLx2ySNfAD',
            'status': 'PENDING',
        },
    ]
    assert data['items'] == expected


def test_billing_terms(
    mock_app,
    dynamodb_seeds,
    http_api_proxy: HttpApiProxy,
    lambda_context: LambdaContext,
):
    org_id = 'edp8njvgQuzNkLx2ySNfAD'
    # This data was added from seeds
    r = mock_app.lambda_handler(
        http_api_proxy(
            raw_path=f'/billing/{org_id}/terms',
            method=HTTPMethod.GET,
        ),
        lambda_context,
    )

    data = json.loads(r['body'])

    assert 'billing_day' in data
