#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import logging
from os import getenv

from rs.funcs import sendall
from rs.models import Comprobante

__desc = "Send receipts in batches"
__error_desc = """You launched the script for testing but
environment variable `DEBUG` was not set on your system.

Try again with --production flag if you want to launch for production.

Bye!"""

__error_desc2 = """You need to remove DEBUG from the environment variables before using --production.
"""

parser = argparse.ArgumentParser(description=__desc)
parser.add_argument(
    "--production",
    action="store_true",
    default=False,
    help="Ensure the user *really* wants to use this script with production values",
)
parser.add_argument(
    "--count",
    action="store_true",
    default=False,
    help="Count how many vouchers are left for processing",
)

if __name__ == "__main__":
    args = parser.parse_args()

    if args.count:
        print("Comprobantes por procesar: {}".format(Comprobante.count()))
        exit(0)

    if not args.production and getenv("DEBUG") is None:
        logging.error(__error_desc)
        exit(1)
    elif args.production and getenv("DEBUG"):
        logging.error(__error_desc2)
        exit(1)
    sendall()

# Local Variables:
# major-mode: python-mode
# End:
