"""Cron entry point: run auction state transitions.

Usage:
    env/bin/python -m make_post_sell.scripts.auction_tick development.ini

Cron suggestion: every minute (* * * * *). Soft-close gives 60s default
windows so 1-minute granularity is plenty.
"""

import argparse
import sys

from pyramid.paster import bootstrap, get_appsettings, setup_logging

from .. import models
from ..lib.auction_tick import tick


def parse_args(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "config_uri",
        help="Configuration file, e.g., development.ini",
    )
    return parser.parse_args(argv[1:])


def main(argv=sys.argv):
    args = parse_args(argv)
    setup_logging(args.config_uri)
    env = bootstrap(args.config_uri)

    settings = get_appsettings(args.config_uri)  # noqa: F841
    with env["request"].tm:
        result = tick(env["request"].dbsession)
    print(
        f"auction_tick: activated={result['activated']} ended={result['ended']}"
    )


if __name__ == "__main__":
    main()
