{
  "$schema": "mantara.schema.v1",
  "system_name": "Simple Loan Calculator",
  "schema_name": "ai_sch_20260514_021925",
  "description": "A web application for calculating loan payments, total interest, and amortization schedules.",
  "menus": [
    {
      "menu_id": 1,
      "menu_name": "Loan Calculations",
      "sequence_number": 1,
      "description": "Manage loan calculations and their results.",
      "submenus": [
        {
          "submenu_id": 101,
          "submenu_name": "Perform Calculation",
          "sequence_number": 1,
          "description": "Input loan parameters and perform calculations.",
          "tables": [
            {
              "table_name": "loan_calculations",
              "comment": "Stores individual loan calculation instances with input parameters and computed 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": "principal",
                  "type": "DECIMAL(15,2)",
                  "constraints": "NOT NULL CHECK (principal > 0)",
                  "comment": "The loan amount borrowed."
                },
                {
                  "name": "annual_interest_rate",
                  "type": "DECIMAL(6,3)",
                  "constraints": "NOT NULL CHECK (annual_interest_rate >= 0 AND annual_interest_rate < 100)",
                  "comment": "Annual interest rate as a percentage."
                },
                {
                  "name": "loan_term_months",
                  "type": "INTEGER",
                  "constraints": "NOT NULL CHECK (loan_term_months > 0 AND loan_term_months <= 600)",
                  "comment": "Duration of the loan in months."
                },
                {
                  "name": "monthly_payment",
                  "type": "DECIMAL(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "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": "calculation_method",
                  "type": "VARCHAR(100)",
                  "constraints": "NOT NULL DEFAULT 'standard-amortization'",
                  "comment": "Method used for calculation."
                },
                {
                  "name": "session_id",
                  "type": "UUID",
                  "comment": "Reference to the calculation session."
                },
                {
                  "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)"
                }
              ]
            }
          ]
        },
        {
          "submenu_id": 102,
          "submenu_name": "Amortization Schedule",
          "sequence_number": 2,
          "description": "View and manage amortization schedules for loan calculations.",
          "tables": [
            {
              "table_name": "amortization_schedule_entries",
              "comment": "Stores entries for 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": "loan_calculation_id",
                  "type": "UUID",
                  "constraints": "NOT NULL",
                  "comment": "Reference to the parent loan calculation."
                },
                {
                  "name": "payment_number",
                  "type": "INTEGER",
                  "constraints": "NOT NULL CHECK (payment_number > 0)",
                  "comment": "Sequential payment number."
                },
                {
                  "name": "payment_date",
                  "type": "DATE",
                  "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 CHECK (payment_amount >= 0)",
                  "comment": "Total payment for the period."
                },
                {
                  "name": "principal_portion",
                  "type": "DECIMAL(15,2)",
                  "constraints": "NOT NULL CHECK (principal_portion >= 0)",
                  "comment": "Amount applied to principal."
                },
                {
                  "name": "interest_portion",
                  "type": "DECIMAL(15,2)",
                  "constraints": "NOT NULL CHECK (interest_portion >= 0)",
                  "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 CHECK (cumulative_interest >= 0)",
                  "comment": "Total interest paid to date."
                },
                {
                  "name": "cumulative_principal",
                  "type": "DECIMAL(15,2)",
                  "constraints": "NOT NULL CHECK (cumulative_principal >= 0)",
                  "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": "loan_calculation_id",
                  "references": "slc.loan_calculations(id)",
                  "on_delete": "CASCADE"
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "menu_id": 2,
      "menu_name": "User Sessions",
      "sequence_number": 2,
      "description": "Manage user sessions and track calculation activities.",
      "submenus": [
        {
          "submenu_id": 201,
          "submenu_name": "Calculation Sessions",
          "sequence_number": 1,
          "description": "Track user sessions for analytics and temporary storage.",
          "tables": [
            {
              "table_name": "calculation_sessions",
              "comment": "Tracks user session data for analytics and temporary storage.",
              "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": "calculation_count",
                  "type": "INTEGER",
                  "constraints": "NOT NULL DEFAULT 0 CHECK (calculation_count >= 0)",
                  "comment": "Number of calculations performed in session."
                },
                {
                  "name": "user_agent",
                  "type": "TEXT",
                  "comment": "Browser/device information."
                },
                {
                  "name": "last_activity_at",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL DEFAULT CURRENT_TIMESTAMP",
                  "comment": "Last interaction time."
                },
                {
                  "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 temporarily for session continuity.",
    "No personally identifiable information (PII) is collected."
  ],
  "open_questions": [
    "Should the system support multiple currencies?",
    "Is there a need for more detailed session analytics?"
  ]
}
