from datetime import datetime, timedelta

defaults = {
    'infobeamer-cms': {
        'config': {
            'MAX_UPLOADS': 5,
            'PREFERRED_URL_SCHEME': 'https',
            'SESSION_COOKIE_NAME': '__Host-sess',
            'URL_KEY': repo.vault.password_for(f'{node.name} infobeamer-cms url key'),
            'VERSION': 1,
        },
        'workers': 4,
    },
}

@metadata_reactor.provides(
    'nginx/vhosts',
)
def nginx(metadata):
    if not node.has_bundle('nginx'):
        raise DoNotRunAgain

    return {
        'nginx': {
            'vhosts': {
                'infobeamer-cms': {
                    'domain': node.metadata.get('infobeamer-cms/domain'),
                    'locations': {
                        '/': {
                            'target': 'http://127.0.0.1:8000',
                        },
                        '/sync': {
                            'return': 403,
                        },
                        '/static': {
                            'alias': '/opt/infobeamer-cms/src/static',
                        },
                    },
                    'website_check_path': '/',
                    'website_check_string': 'Share your projects',
                },
            },
        },
    }


@metadata_reactor.provides(
    'infobeamer-cms/config/TIME_MAX',
    'infobeamer-cms/config/TIME_MIN',
)
def event_times(metadata):
    event_start = datetime.strptime(metadata.get('infobeamer-cms/event_start_date'), '%Y-%m-%d')
    event_duration = metadata.get('infobeamer-cms/event_duration_days', 4)

    event_end = event_start + timedelta(days=event_duration)

    return {
        'infobeamer-cms': {
            'config': {
                'TIME_MAX': int(event_end.timestamp()),
                'TIME_MIN': int(event_start.timestamp()),
            },
        },
    }
