import argparse
import os

import psycopg
import psycopg.conninfo

USER_POOL_ID = os.getenv("USER_POOL_ID")
POSTGRES_HOST = os.getenv("POSTGRES_HOST")
POSTGRES_PORT = os.getenv("POSTGRES_PORT")
POSTGRES_USER = os.getenv("POSTGRES_USER")
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD")
POSTGRES_DB = os.getenv("POSTGRES_DB")


def main() -> None:
    postgres_url = psycopg.conninfo.make_conninfo(
        host=POSTGRES_HOST,
        port=POSTGRES_PORT,
        user=POSTGRES_USER,
        password=POSTGRES_PASSWORD,
        dbname=POSTGRES_DB,
    )
    with psycopg.connect(postgres_url) as conn, conn.cursor() as cur:
        cur.execute(
            """--sql
            UPDATE quota_allocations SET status ='expired'
            WHERE status IN ('active', 'exceeded') AND valid_to_date < TIMEZONE('Asia/Tokyo', NOW())::DATE;
            UPDATE quota_allocations SET status ='active'
            WHERE status='scheduled' AND TIMEZONE('Asia/Tokyo', NOW())::DATE >= valid_from_date;
            """,
        )
        conn.commit()


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Quota Allocation Status Updater Script")
    main()
