defaults = {
    'apt': {
        'packages': {
            'libpod-parser-perl': {},
            'mosh': {},
            'weechat': {},
            'weechat-core': {},
            'weechat-curses': {},
            'weechat-perl': {},
            'weechat-plugins': {},
            'weechat-python': {},
            'weechat-ruby': {},
        },
        'repos': {
            'weechat': {
                'items': {
                    'deb https://weechat.org/{os} {os_release} main',
                },
            },
        },
    },
    'nftables': {
        'input': {
            '10-weechat': {
                'udp dport { 60000-61000 } accept',
            },
        },
    },
}


@metadata_reactor.provides(
    'backup-client/pre-hooks',
    'backups/paths',
    'users',
    'zfs/datasets',
)
def paths(metadata):
    user = metadata.get('weechat/user')

    return {
        'backup-client': {
            'pre-hooks': {
                'weechat': f"""
                    echo 'core.weechat */layout store' >> /home/{user}/.weechat/weechat_fifo
                    echo 'core.weechat */save' >> /home/{user}/.weechat/weechat_fifo
                """,
            },
        },
        'backups': {
            'paths': {
                f'/home/{user}/.weechat',
            },
        },
        'users': {
            user: {
                'enable_linger': True,
            },
        },
        'zfs': {
            'datasets': {
                f'tank/{user}': {},
                f'tank/{user}/weechat': {
                    'mountpoint': f'/home/{user}/.weechat',
                    'compression': 'on',
                },
            },
        },
    }


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

    relay_domain = metadata.get('weechat/relay_domain', None)
    if relay_domain is None:
        return {}

    return {
        'nginx': {
            'vhosts': {
                'weechat': {
                    'domain': relay_domain,
                    # This only does websockets connections, which stay
                    # open for a very long time. This only generates
                    # useless metrics.
                    'timing_log': False,
                    'locations': {
                        '/weechat': {
                            'proxy_read_timeout': '12h',
                            'target': 'http://[::1]:9000',
                            'websockets': True,
                        },
                    },
                },
            },
        },
    }
