"""
Command Line Interface for ggsipu_academics

This module provides a command line interface for calculating academic
parameters such as grade, grade point, semester grade point average
and cumulative grade point average.

"""

# Coded by Mayank Somvanshi

import argparse
from ggsipu_academics.Calculate import (
    Grade,
    GradePoint,
    SGPAFromMarks,
    SGPAFromGradePoint,
    CGPAFromSemesterMarks,
    CGPAFromSemesterSGPA,
)


# ---------- INPUT HELPERS ----------

def input_subjects_marks():
    subjects = []
    n = int(input("Enter number of subjects: "))
    for i in range(n):
        print(f"\nSubject {i+1}")
        credits = int(input("Credits: "))
        marks = int(input("Marks: "))
        subjects.append((credits, marks))
    return subjects


def input_subjects_gradepoints():
    subjects = []
    n = int(input("Enter number of subjects: "))
    for i in range(n):
        print(f"\nSubject {i+1}")
        credits = int(input("Credits: "))
        gp = float(input("Grade Point: "))
        subjects.append((credits, gp))
    return subjects


def input_semesters(mode):
    semesters = []
    sem_count = int(input("Enter number of semesters: "))

    for s in range(sem_count):
        print(f"\n--- Semester {s+1} ---")
        if mode == "marks":
            semesters.append(input_subjects_marks())
        else:
            semesters.append(input_subjects_gradepoints())

    return semesters


# ---------- CLI ----------

def main():
    parser = argparse.ArgumentParser(
        prog="ggsipu",
        description="GGSIPU Academic Calculator CLI"
    )

    subparsers = parser.add_subparsers(dest="command")

    # grade
    grade_parser = subparsers.add_parser(
        "grade", help="Calculate grade from marks"
    )
    grade_parser.add_argument("marks", type=int)

    # grade point
    gp_parser = subparsers.add_parser(
        "gradepoint", help="Calculate grade point from marks"
    )
    gp_parser.add_argument("marks", type=int)

    # sgpa
    sgpa_parser = subparsers.add_parser(
        "sgpa", help="Calculate SGPA"
    )
    sgpa_parser.add_argument(
        "--mode",
        choices=["marks", "gradepoint"],
        required=True,
        help="Input mode"
    )

    # cgpa
    cgpa_parser = subparsers.add_parser(
        "cgpa", help="Calculate CGPA"
    )
    cgpa_parser.add_argument(
        "--mode",
        choices=["marks", "sgpa"],
        required=True,
        help="Input mode"
    )

    args = parser.parse_args()

    # ---------- COMMAND HANDLERS ----------

    if args.command == "grade":
        print("Grade:", Grade(args.marks))

    elif args.command == "gradepoint":
        print("Grade Point:", GradePoint(args.marks))

    elif args.command == "sgpa":
        if args.mode == "marks":
            subjects = input_subjects_marks()
            print("SGPA:", SGPAFromMarks(subjects))
        else:
            subjects = input_subjects_gradepoints()
            print("SGPA:", SGPAFromGradePoint(subjects))

    elif args.command == "cgpa":
        if args.mode == "marks":
            semesters = input_semesters("marks")
            print("CGPA:", CGPAFromSemesterMarks(semesters))
        else:
            n = int(input("Enter number of semesters: "))
            sgpas = [float(input(f"SGPA of semester {i+1}: ")) for i in range(n)]
            print("CGPA:", CGPAFromSemesterSGPA(sgpas))

    else:
        parser.print_help()


if __name__ == "__main__":
    main()

