"""Cron entry point: run offer auto-expiration.

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

Cron suggestion: every 15 minutes — offers expire at hour granularity
so this is fine.
"""

import argparse
import sys

from pyramid.paster import bootstrap, get_appsettings, setup_logging

from .. import models
from ..lib.offer_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"offer_tick: expired={result['expired']}")


if __name__ == "__main__":
    main()
