{
  "$schema": "mantara.schema.v1",
  "system_name": "Simple Loan Calculator",
  "schema_name": "ai_sch_20260514_032743",
  "description": "A system to calculate loan payments, total interest, and amortization schedules.",
  "menus": [
    {
      "menu_id": 1,
      "menu_name": "Loan Calculations",
      "sequence_number": 1,
      "description": "Handles all loan calculation operations.",
      "submenus": [
        {
          "submenu_id": 101,
          "submenu_name": "Perform Calculation",
          "sequence_number": 1,
          "description": "Allows users to input loan parameters and perform calculations.",
          "tables": [
            {
              "table_name": "calculations",
              "comment": "Stores loan calculation instances with input parameters and results.",
              "columns": [
                {
                  "name": "id",
                  "type": "UUID",
                  "constraints": "PRIMARY KEY DEFAULT gen_random_uuid()"
                },
                {
                  "name": "submenu_id",
                  "type": "INT",
                  "constraints": "DEFAULT 101 NOT NULL REFERENCES slc.submenu(submenu_id)"
                },
                {
                  "name": "session_id",
                  "type": "UUID",
                  "constraints": "NOT NULL REFERENCES slc.calculation_sessions(session_id) ON DELETE CASCADE",
                  "comment": "References the session during which the calculation was made."
                },
                {
                  "name": "principal",
                  "type": "DECIMAL(15,2)",
                  "constraints": "NOT NULL CHECK (principal > 0)",
                  "comment": "Loan amount borrowed."
                },
                {
                  "name": "annual_interest_rate",
                  "type": "DECIMAL(6,3)",
                  "constraints": "NOT NULL CHECK (annual_interest_rate >= 0)",
                  "comment": "Annual interest rate as percentage."
                },
                {
                  "name": "loan_term_months",
                  "type": "INTEGER",
                  "constraints": "NOT NULL CHECK (loan_term_months > 0)",
                  "comment": "Duration of loan in months."
                },
                {
                  "name": "monthly_payment",
                  "type": "DECIMAL(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Calculated monthly payment amount."
                },
                {
                  "name": "total_interest",
                  "type": "DECIMAL(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Total interest paid over loan term."
                },
                {
                  "name": "total_amount",
                  "type": "DECIMAL(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Total amount paid (principal + interest)."
                },
                {
                  "name": "created_at",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL DEFAULT CURRENT_TIMESTAMP"
                },
                {
                  "name": "updated_at",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL DEFAULT CURRENT_TIMESTAMP"
                }
              ],
              "foreign_keys": [
                {
                  "column": "session_id",
                  "references": "slc.calculation_sessions(session_id)",
                  "on_delete": "CASCADE"
                }
              ]
            }
          ]
        },
        {
          "submenu_id": 102,
          "submenu_name": "Amortization Schedule",
          "sequence_number": 2,
          "description": "Displays the amortization schedule for a loan calculation.",
          "tables": [
            {
              "table_name": "amortization_entries",
              "comment": "Stores each payment period in the loan amortization schedule.",
              "columns": [
                {
                  "name": "id",
                  "type": "UUID",
                  "constraints": "PRIMARY KEY DEFAULT gen_random_uuid()"
                },
                {
                  "name": "submenu_id",
                  "type": "INT",
                  "constraints": "DEFAULT 102 NOT NULL REFERENCES slc.submenu(submenu_id)"
                },
                {
                  "name": "calculation_id",
                  "type": "UUID",
                  "constraints": "NOT NULL REFERENCES slc.calculations(id) ON DELETE CASCADE",
                  "comment": "References the calculation this entry belongs to."
                },
                {
                  "name": "payment_number",
                  "type": "INTEGER",
                  "constraints": "NOT NULL CHECK (payment_number > 0)",
                  "comment": "Sequential payment number (1 to N)."
                },
                {
                  "name": "payment_date",
                  "type": "DATE",
                  "constraints": "NOT NULL",
                  "comment": "Scheduled payment date."
                },
                {
                  "name": "beginning_balance",
                  "type": "DECIMAL(15,2)",
                  "constraints": "NOT NULL CHECK (beginning_balance >= 0)",
                  "comment": "Loan balance at start of period."
                },
                {
                  "name": "payment_amount",
                  "type": "DECIMAL(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Total payment for the period."
                },
                {
                  "name": "principal_portion",
                  "type": "DECIMAL(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Amount applied to principal."
                },
                {
                  "name": "interest_portion",
                  "type": "DECIMAL(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Amount applied to interest."
                },
                {
                  "name": "ending_balance",
                  "type": "DECIMAL(15,2)",
                  "constraints": "NOT NULL CHECK (ending_balance >= 0)",
                  "comment": "Loan balance at end of period."
                },
                {
                  "name": "cumulative_interest",
                  "type": "DECIMAL(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Total interest paid to date."
                },
                {
                  "name": "cumulative_principal",
                  "type": "DECIMAL(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Total principal paid to date."
                },
                {
                  "name": "created_at",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL DEFAULT CURRENT_TIMESTAMP"
                },
                {
                  "name": "updated_at",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL DEFAULT CURRENT_TIMESTAMP"
                }
              ],
              "foreign_keys": [
                {
                  "column": "calculation_id",
                  "references": "slc.calculations(id)",
                  "on_delete": "CASCADE"
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "menu_id": 2,
      "menu_name": "Session Management",
      "sequence_number": 2,
      "description": "Tracks user sessions for analytics and usage patterns.",
      "submenus": [
        {
          "submenu_id": 201,
          "submenu_name": "Session Tracking",
          "sequence_number": 1,
          "description": "Stores session information for anonymous users.",
          "tables": [
            {
              "table_name": "calculation_sessions",
              "comment": "Tracks user sessions for analytics and usage patterns.",
              "columns": [
                {
                  "name": "id",
                  "type": "UUID",
                  "constraints": "PRIMARY KEY DEFAULT gen_random_uuid()"
                },
                {
                  "name": "submenu_id",
                  "type": "INT",
                  "constraints": "DEFAULT 201 NOT NULL REFERENCES slc.submenu(submenu_id)"
                },
                {
                  "name": "session_id",
                  "type": "UUID",
                  "constraints": "NOT NULL UNIQUE",
                  "comment": "Unique session identifier."
                },
                {
                  "name": "first_access_at",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL DEFAULT CURRENT_TIMESTAMP",
                  "comment": "When session started."
                },
                {
                  "name": "last_access_at",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL DEFAULT CURRENT_TIMESTAMP",
                  "comment": "Most recent activity."
                },
                {
                  "name": "calculation_count",
                  "type": "INTEGER",
                  "constraints": "NOT NULL DEFAULT 0",
                  "comment": "Number of calculations performed."
                },
                {
                  "name": "user_agent",
                  "type": "VARCHAR(500)",
                  "comment": "Browser/device information."
                },
                {
                  "name": "ip_address",
                  "type": "VARCHAR(45)",
                  "comment": "User IP address (anonymized)."
                },
                {
                  "name": "created_at",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL DEFAULT CURRENT_TIMESTAMP"
                },
                {
                  "name": "updated_at",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL DEFAULT CURRENT_TIMESTAMP"
                }
              ]
            }
          ]
        }
      ]
    }
  ],
  "assumptions": [
    "The system does not require user authentication.",
    "Calculations are stored for analytics purposes only and are associated with anonymous session IDs.",
    "The system handles edge cases such as zero interest rate and very long loan terms."
  ],
  "open_questions": [
    "Should the system support multi-currency calculations?",
    "Is there a need for additional analytics beyond session tracking?"
  ]
}
