#!/usr/bin/env python3
"""Structured vote submission. Validates against VoteOutput schema and writes JSON."""
import argparse, json, sys, os

# Allow importing multi_agent from the ralph package
sys.path.insert(0, '/opt/ralph')

from multi_agent.parsing import VoteOutput  # noqa: E402


def main():
    p = argparse.ArgumentParser(description='Cast a structured vote')
    p.add_argument('--winner', required=True, help='Proposal letter (A, B, C...)')
    p.add_argument('--decisive-argument', required=True, help='The argument that convinced you')
    p.add_argument('--concerns', required=True, help='Biggest concern about the winner')
    p.add_argument('--unrefuted-arguments', default='', help='Arguments nobody countered')
    p.add_argument('--merge-suggestion', default='', help='Hybrid proposal idea')
    args = p.parse_args()

    raw = {
        'winner': args.winner.strip(),
        'decisive_argument': args.decisive_argument.strip(),
        'concerns_about_the_winner': args.concerns.strip(),
        'unrefuted_arguments': args.unrefuted_arguments.strip(),
        'merge_suggestion': args.merge_suggestion.strip(),
    }

    try:
        vo = VoteOutput.model_validate(raw)
    except Exception as exc:
        print(f'ERROR: vote validation failed: {exc}', file=sys.stderr)
        sys.exit(1)

    vote = vo.model_dump()

    out = os.environ.get('VOTE_OUTPUT_PATH')
    if out:
        with open(out, 'w') as f:
            json.dump(vote, f, indent=2)
        print(f'Vote recorded for Proposal {vo.winner} -> {out}')
    else:
        print(json.dumps(vote, indent=2))
        print(f'\nVote recorded for Proposal {vo.winner}.')

if __name__ == '__main__':
    main()
