{
  "$schema": "mantara.schema.v1",
  "system_name": "Simple Loan Calculator",
  "schema_name": "ai_sch_20260514_022907",
  "description": "A web application for calculating loan payments, total interest, and amortization schedules.",
  "menus": [
    {
      "menu_id": 1,
      "menu_name": "Calculations",
      "sequence_number": 1,
      "description": "Manage loan calculations and their results.",
      "submenus": [
        {
          "submenu_id": 101,
          "submenu_name": "Loan Calculations",
          "sequence_number": 1,
          "description": "Perform and store loan calculations.",
          "tables": [
            {
              "table_name": "calculations",
              "comment": "Stores 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": "session_id",
                  "type": "UUID",
                  "constraints": "NOT NULL REFERENCES slc.calculation_sessions(session_id) ON DELETE CASCADE",
                  "comment": "References the user session during which the calculation was performed."
                },
                {
                  "name": "principal",
                  "type": "DECIMAL(15,2)",
                  "constraints": "NOT NULL CHECK (principal > 0)",
                  "comment": "Loan amount borrowed, must be positive."
                },
                {
                  "name": "annual_interest_rate",
                  "type": "DECIMAL(6,3)",
                  "constraints": "NOT NULL CHECK (annual_interest_rate >= 0)",
                  "comment": "Annual interest rate as a 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 the 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",
                  "comment": "Timestamp when the calculation was performed."
                },
                {
                  "name": "updated_at",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL DEFAULT CURRENT_TIMESTAMP",
                  "comment": "Timestamp when the calculation was last updated."
                }
              ],
              "foreign_keys": [
                {
                  "column": "session_id",
                  "references": "slc.calculation_sessions(session_id)",
                  "on_delete": "CASCADE"
                }
              ]
            }
          ]
        },
        {
          "submenu_id": 102,
          "submenu_name": "Amortization Schedules",
          "sequence_number": 2,
          "description": "Store and manage amortization schedule entries for each loan calculation.",
          "tables": [
            {
              "table_name": "amortization_entries",
              "comment": "Stores amortization schedule entries for each loan calculation.",
              "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 parent loan calculation."
                },
                {
                  "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",
                  "comment": "Loan balance at the start of the 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",
                  "comment": "Loan balance at the end of the 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",
                  "comment": "Timestamp when the entry was created."
                },
                {
                  "name": "updated_at",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL DEFAULT CURRENT_TIMESTAMP",
                  "comment": "Timestamp when the entry was last updated."
                }
              ],
              "foreign_keys": [
                {
                  "column": "calculation_id",
                  "references": "slc.calculations(id)",
                  "on_delete": "CASCADE"
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "menu_id": 2,
      "menu_name": "Sessions",
      "sequence_number": 2,
      "description": "Track user sessions for analytics and usage patterns.",
      "submenus": [
        {
          "submenu_id": 201,
          "submenu_name": "Calculation Sessions",
          "sequence_number": 1,
          "description": "Manage user sessions and track calculation activities.",
          "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": "Timestamp when the session started."
                },
                {
                  "name": "last_access_at",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL DEFAULT CURRENT_TIMESTAMP",
                  "comment": "Most recent activity timestamp."
                },
                {
                  "name": "calculation_count",
                  "type": "INTEGER",
                  "constraints": "NOT NULL DEFAULT 0",
                  "comment": "Number of calculations performed in this session."
                },
                {
                  "name": "user_agent",
                  "type": "VARCHAR(500)",
                  "comment": "Browser/device information."
                },
                {
                  "name": "ip_address",
                  "type": "VARCHAR(45)",
                  "comment": "Anonymized user IP address."
                },
                {
                  "name": "created_at",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL DEFAULT CURRENT_TIMESTAMP",
                  "comment": "Timestamp when the session record was created."
                },
                {
                  "name": "updated_at",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL DEFAULT CURRENT_TIMESTAMP",
                  "comment": "Timestamp when the session record was last updated."
                }
              ]
            }
          ]
        }
      ]
    }
  ],
  "assumptions": [
    "Loan calculations are performed anonymously without user authentication.",
    "Each calculation is associated with a unique browser session for tracking purposes."
  ],
  "open_questions": [
    "Should the application support multi-currency calculations in the future?",
    "Is there a need to store historical calculation data beyond the 90-day retention period?"
  ]
}
