{
  "$schema": "mantara.schema.v1",
  "system_name": "Simple Loan Calculator",
  "schema_name": "ai_sch_20260514_042319",
  "description": "A web application for calculating loan payment schedules, monthly payments, and total interest costs based on loan parameters.",
  "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": "calculation_inputs",
              "comment": "Stores input parameters for loan calculations.",
              "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_amount",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL CHECK (principal_amount > 0)",
                  "comment": "Loan amount in dollars."
                },
                {
                  "name": "interest_rate",
                  "type": "NUMERIC(5,3)",
                  "constraints": "NOT NULL CHECK (interest_rate >= 0 AND interest_rate <= 30)",
                  "comment": "Annual interest rate as a percentage."
                },
                {
                  "name": "term_months",
                  "type": "INTEGER",
                  "constraints": "NOT NULL CHECK (term_months > 0 AND term_months <= 360)",
                  "comment": "Loan duration in months."
                },
                {
                  "name": "start_date",
                  "type": "DATE",
                  "comment": "Optional first payment date."
                }
              ]
            }
          ]
        },
        {
          "submenu_id": 102,
          "submenu_name": "Calculation Results",
          "sequence_number": 2,
          "description": "View results of loan calculations including payment schedules.",
          "tables": [
            {
              "table_name": "calculations",
              "comment": "Stores results of loan calculations including monthly payments and total interest.",
              "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": "principal",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL CHECK (principal > 0)",
                  "comment": "Loan amount borrowed."
                },
                {
                  "name": "annual_interest_rate",
                  "type": "NUMERIC(5,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 <= 360)",
                  "comment": "Duration of loan in months."
                },
                {
                  "name": "monthly_payment",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Calculated monthly payment amount."
                },
                {
                  "name": "total_interest",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Total interest paid over loan lifetime."
                },
                {
                  "name": "total_amount_paid",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Principal plus total interest."
                },
                {
                  "name": "calculation_method",
                  "type": "VARCHAR(100)",
                  "constraints": "NOT NULL",
                  "comment": "Fixed-rate amortization method identifier."
                },
                {
                  "name": "created_at",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL DEFAULT CURRENT_TIMESTAMP"
                },
                {
                  "name": "updated_at",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL DEFAULT CURRENT_TIMESTAMP",
                  "comment": "Set by application or trigger on update."
                }
              ]
            },
            {
              "table_name": "amortization_schedule_entries",
              "comment": "Stores each payment period's details in the loan's 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": "Reference to parent calculation."
                },
                {
                  "name": "payment_number",
                  "type": "INTEGER",
                  "constraints": "NOT NULL CHECK (payment_number > 0)",
                  "comment": "Sequential payment number."
                },
                {
                  "name": "payment_date",
                  "type": "DATE",
                  "constraints": "NOT NULL",
                  "comment": "Scheduled payment date."
                },
                {
                  "name": "beginning_balance",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL CHECK (beginning_balance >= 0)",
                  "comment": "Principal balance at start of period."
                },
                {
                  "name": "payment_amount",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL CHECK (payment_amount >= 0)",
                  "comment": "Total payment for this period."
                },
                {
                  "name": "principal_payment",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL CHECK (principal_payment >= 0)",
                  "comment": "Portion applied to principal."
                },
                {
                  "name": "interest_payment",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL CHECK (interest_payment >= 0)",
                  "comment": "Portion applied to interest."
                },
                {
                  "name": "ending_balance",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL CHECK (ending_balance >= 0)",
                  "comment": "Principal balance after payment."
                },
                {
                  "name": "cumulative_interest",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL CHECK (cumulative_interest >= 0)",
                  "comment": "Total interest 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",
                  "comment": "Set by application or trigger on update."
                }
              ]
            }
          ]
        }
      ]
    }
  ],
  "assumptions": [
    "The application does not require user authentication or role-based access control.",
    "All calculations are performed client-side without backend processing."
  ],
  "open_questions": [
    "Should the application support exporting amortization schedules to CSV?",
    "Is there a need for storing calculation history beyond the current session?"
  ]
}
