#!/usr/bin/env python3
"""CurriVerse X particle-equilibrium lab / آزمایش تعادل ذره.
Run: python3 ee-equilibrium.py m H U
A particle at rest has cable directions uL=(-3/5,4/5), uR=(4/5,3/5),
weight (0,-10m), and actuator force (H,U), in SI and an inertial frame.
Candidate tensions can be negative: then this assumed cable equilibrium is infeasible.
Half-unit inputs: m=0.5..6 kg, H=-12..12 N, U=0..60 N.
Standard library only. g=10 is a teaching value; no stability or rigid-body torque model.
"""
import argparse
from fractions import Fraction as Q
import json

def number(text):
    try:v=Q(text)
    except (ValueError,ZeroDivisionError):raise argparse.ArgumentTypeError('Use a finite number or fraction, e.g. -1/2.')
    if (2*v).denominator!=1:raise argparse.ArgumentTypeError('Use half-unit steps.')
    return v

def analyze(m,H,U):
    m,H,U=map(Q,(m,H,U))
    if not (Q(1,2)<=m<=6 and -12<=H<=12 and 0<=U<=60):raise ValueError('Use m 0.5..6, H -12..12 and U 0..60.')
    W=10*m;L=W-U;TL=(3*H+4*L)/5;TR=(-4*H+3*L)/5
    left=[-Q(3,5)*TL,Q(4,5)*TL];right=[Q(4,5)*TR,Q(3,5)*TR]
    residual=[left[0]+right[0]+H,left[1]+right[1]+U-W]
    assert residual==[0,0]
    assert 4*TL+3*TR==5*L and -3*TL+4*TR==-5*H
    status='infeasible' if min(TL,TR)<0 else 'boundary' if min(TL,TR)==0 else 'both-pulling'
    return {'weight_N':str(W),'effective_downward_load_N':str(L),'candidate_left_tension_N':str(TL),'candidate_right_tension_N':str(TR),
            'candidate_left_components_N':list(map(str,left)),'candidate_right_components_N':list(map(str,right)),
            'force_residual_N':list(map(str,residual)),'status':status,'agreement':'exact'}

def main():
    p=argparse.ArgumentParser(usage='%(prog)s [--json] m H U',description='Solve candidate cable forces and check unilateral feasibility; never clip negative tension.')
    p.add_argument('--json',action='store_true');args,inputs=p.parse_known_args()
    if len(inputs)!=3:p.error('Provide exactly three values: m,H,U.')
    try:r=analyze(*[number(x) for x in inputs])
    except (argparse.ArgumentTypeError,ValueError) as e:p.error(str(e))
    if args.json:print(json.dumps(r));return
    print('Candidate equilibrium, not a motion simulation / تعادل پیشنهادی، نه شبیه‌سازی حرکت')
    print('Negative tension is infeasible; zero tension is a boundary, not a stability guarantee. / کشش منفی ناممکن و کشش صفر مرز است، نه تضمین پایداری.')
    for k,v in r.items():print(k+': '+json.dumps(v,ensure_ascii=False))
if __name__=='__main__':main()
