{
  "$schema": "mantara.schema.v1",
  "system_name": "Loan Calculator",
  "schema_name": "ai_sch_simple_loa_042319",
  "description": "A web application for calculating loan payment details based on user inputs.",
  "menus": [
    {
      "menu_id": 1,
      "menu_name": "Loan Calculations",
      "sequence_number": 1,
      "description": "Manage loan calculation sessions and results.",
      "submenus": [
        {
          "submenu_id": 101,
          "submenu_name": "Loan Calculation Sessions",
          "sequence_number": 1,
          "description": "Store and manage individual loan calculation sessions.",
          "tables": [
            {
              "table_name": "loan_calculations",
              "comment": "Stores loan calculation sessions 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 loan_calc.submenu(submenu_id)"
                },
                {
                  "name": "principal",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Initial loan amount borrowed"
                },
                {
                  "name": "annual_interest_rate",
                  "type": "NUMERIC(5,4)",
                  "constraints": "NOT NULL",
                  "comment": "Yearly interest rate as a percentage"
                },
                {
                  "name": "loan_term_months",
                  "type": "INTEGER",
                  "constraints": "NOT NULL",
                  "comment": "Duration of the loan in months"
                },
                {
                  "name": "monthly_payment",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Calculated monthly payment amount"
                },
                {
                  "name": "total_payment",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Calculated total amount paid over loan term"
                },
                {
                  "name": "total_interest",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Calculated total interest paid over loan term"
                },
                {
                  "name": "session_id",
                  "type": "VARCHAR(255)",
                  "constraints": "NOT NULL",
                  "comment": "Browser session identifier for tracking user calculations"
                },
                {
                  "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"
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "menu_id": 2,
      "menu_name": "Amortization Schedules",
      "sequence_number": 2,
      "description": "Manage detailed amortization schedules for loan calculations.",
      "submenus": [
        {
          "submenu_id": 201,
          "submenu_name": "Amortization Schedule Entries",
          "sequence_number": 1,
          "description": "Store detailed payment breakdowns for each loan calculation period.",
          "tables": [
            {
              "table_name": "amortization_schedule_entries",
              "comment": "Stores individual payment breakdowns for each period of a loan term.",
              "columns": [
                {
                  "name": "id",
                  "type": "UUID",
                  "constraints": "PRIMARY KEY DEFAULT gen_random_uuid()"
                },
                {
                  "name": "submenu_id",
                  "type": "INT",
                  "constraints": "DEFAULT 201 NOT NULL REFERENCES loan_calc.submenu(submenu_id)"
                },
                {
                  "name": "calculation_id",
                  "type": "UUID",
                  "constraints": "NOT NULL REFERENCES loan_calc.loan_calculations(id) ON DELETE CASCADE",
                  "comment": "Reference to parent LoanCalculation"
                },
                {
                  "name": "payment_number",
                  "type": "INTEGER",
                  "constraints": "NOT NULL",
                  "comment": "Sequential payment number (1 to N)"
                },
                {
                  "name": "payment_date",
                  "type": "DATE",
                  "constraints": "NOT NULL",
                  "comment": "Scheduled payment date"
                },
                {
                  "name": "beginning_balance",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Loan balance at start of period"
                },
                {
                  "name": "payment_amount",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Total payment for this period"
                },
                {
                  "name": "principal_portion",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Amount applied to principal"
                },
                {
                  "name": "interest_portion",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Amount applied to interest"
                },
                {
                  "name": "ending_balance",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Loan balance at end of period"
                },
                {
                  "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"
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "menu_id": 3,
      "menu_name": "Configuration",
      "sequence_number": 3,
      "description": "Manage system-wide default values and constraints for the calculator.",
      "submenus": [
        {
          "submenu_id": 301,
          "submenu_name": "Calculator Defaults",
          "sequence_number": 1,
          "description": "Store default values and constraints for loan calculations.",
          "tables": [
            {
              "table_name": "calculator_defaults",
              "comment": "Stores system-wide default values and constraints for the loan calculator.",
              "columns": [
                {
                  "name": "id",
                  "type": "UUID",
                  "constraints": "PRIMARY KEY DEFAULT gen_random_uuid()"
                },
                {
                  "name": "submenu_id",
                  "type": "INT",
                  "constraints": "DEFAULT 301 NOT NULL REFERENCES loan_calc.submenu(submenu_id)"
                },
                {
                  "name": "default_principal",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Default loan amount"
                },
                {
                  "name": "default_interest_rate",
                  "type": "NUMERIC(5,2)",
                  "constraints": "NOT NULL",
                  "comment": "Default annual interest rate"
                },
                {
                  "name": "default_term_months",
                  "type": "INTEGER",
                  "constraints": "NOT NULL",
                  "comment": "Default loan term in months"
                },
                {
                  "name": "min_principal",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Minimum allowed principal amount"
                },
                {
                  "name": "max_principal",
                  "type": "NUMERIC(15,2)",
                  "constraints": "NOT NULL",
                  "comment": "Maximum allowed principal amount"
                },
                {
                  "name": "min_interest_rate",
                  "type": "NUMERIC(5,4)",
                  "constraints": "NOT NULL",
                  "comment": "Minimum allowed interest rate"
                },
                {
                  "name": "max_interest_rate",
                  "type": "NUMERIC(5,4)",
                  "constraints": "NOT NULL",
                  "comment": "Maximum allowed interest rate"
                },
                {
                  "name": "min_term_months",
                  "type": "INTEGER",
                  "constraints": "NOT NULL",
                  "comment": "Minimum allowed term length"
                },
                {
                  "name": "max_term_months",
                  "type": "INTEGER",
                  "constraints": "NOT NULL",
                  "comment": "Maximum allowed term length"
                },
                {
                  "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 system does not require user authentication for loan calculations.",
    "Loan calculations are stored temporarily and do not require persistent storage beyond session duration."
  ],
  "open_questions": [
    "Should the system support multiple currencies for loan calculations?",
    "Is there a need to store historical calculation data for analytics purposes?"
  ],
  "run_id": "simple_loa_042319"
}
