"""Exact seconds -> offset; optional read-only timeline anchoring. No float/default FPS."""
import argparse
import json
from resolve_api_timecode import Clock, seconds_to_frames, seconds_at_timeline


def main():
    p = argparse.ArgumentParser(description=__doc__)
    p.add_argument("seconds", help="Exact text: 12.5 or 1001/24000, not a timecode label")
    p.add_argument("--rate", required=True)
    p.add_argument("--rounding", choices=("exact", "floor", "ceil", "nearest"), required=True)
    p.add_argument("--timeline", action="store_true", help="Read the open timeline to anchor the offset")
    p.add_argument("--mode", choices=("ndf", "df"), help="Required only with --timeline")
    a = p.parse_args()
    if a.timeline and not a.mode:
        p.error("--timeline also requires --mode")
    if not a.timeline and a.mode:
        p.error("DF/NDF labels are irrelevant to pure seconds conversion; omit --mode")
    if a.timeline:
        from resolve_api_connection import connect
        result = seconds_at_timeline(connect(), a.seconds, a.rate, a.mode, a.rounding)
    else:
        # NDF here only constructs the rate carrier; seconds math never uses label mode.
        result = seconds_to_frames(a.seconds, Clock(a.rate, "ndf"), a.rounding)
    print(json.dumps(result, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
