{
  "$schema": "mantara.schema.v1",
  "system_name": "Simple Calculator",
  "schema_name": "calc",
  "description": "A web-based calculator application for basic arithmetic operations.",
  "menus": [
    {
      "menu_id": 1,
      "menu_name": "Session Management",
      "sequence_number": 1,
      "description": "Handles session tracking and history of calculations.",
      "submenus": [
        {
          "submenu_id": 101,
          "submenu_name": "Session History",
          "sequence_number": 1,
          "description": "Stores session information and history of calculations.",
          "tables": [
            {
              "table_name": "session_history",
              "comment": "Stores session information and history of calculations.",
              "columns": [
                {
                  "name": "id",
                  "type": "UUID",
                  "constraints": "PRIMARY KEY DEFAULT gen_random_uuid()"
                },
                {
                  "name": "session_id",
                  "type": "VARCHAR(255)",
                  "constraints": "NOT NULL UNIQUE",
                  "comment": "Unique identifier for the browser session"
                },
                {
                  "name": "start_time",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL",
                  "comment": "When the session began"
                },
                {
                  "name": "last_activity_time",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL",
                  "comment": "Last interaction timestamp"
                },
                {
                  "name": "created_at",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL DEFAULT CURRENT_TIMESTAMP"
                },
                {
                  "name": "updated_at",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL DEFAULT CURRENT_TIMESTAMP"
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "menu_id": 2,
      "menu_name": "Calculation Management",
      "sequence_number": 2,
      "description": "Handles individual calculations and their attributes.",
      "submenus": [
        {
          "submenu_id": 201,
          "submenu_name": "Calculations",
          "sequence_number": 1,
          "description": "Stores individual calculations performed by the user.",
          "tables": [
            {
              "table_name": "calculation",
              "comment": "Represents a single arithmetic operation performed by the user.",
              "columns": [
                {
                  "name": "id",
                  "type": "UUID",
                  "constraints": "PRIMARY KEY DEFAULT gen_random_uuid()"
                },
                {
                  "name": "submenu_id",
                  "type": "INT",
                  "constraints": "DEFAULT 201 NOT NULL REFERENCES calc.submenu(submenu_id)"
                },
                {
                  "name": "session_history_id",
                  "type": "UUID",
                  "constraints": "NOT NULL",
                  "comment": "References the session in which the calculation was performed"
                },
                {
                  "name": "operand1",
                  "type": "NUMERIC",
                  "constraints": "NOT NULL",
                  "comment": "First number in the operation"
                },
                {
                  "name": "operator",
                  "type": "VARCHAR(10)",
                  "constraints": "NOT NULL CHECK (operator IN ('+', '-', '×', '÷', '*', '/'))",
                  "comment": "Arithmetic operator used in the calculation"
                },
                {
                  "name": "operand2",
                  "type": "NUMERIC",
                  "constraints": "NOT NULL",
                  "comment": "Second number in the operation"
                },
                {
                  "name": "result",
                  "type": "NUMERIC",
                  "constraints": "NOT NULL",
                  "comment": "Calculated result of the operation"
                },
                {
                  "name": "expression",
                  "type": "VARCHAR(500)",
                  "constraints": "NOT NULL",
                  "comment": "Full expression as displayed (e.g., '5 + 3 = 8')"
                },
                {
                  "name": "timestamp",
                  "type": "TIMESTAMP",
                  "constraints": "NOT NULL",
                  "comment": "When the calculation was performed"
                },
                {
                  "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_history_id",
                  "references": "calc.session_history(id)",
                  "on_delete": "CASCADE"
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "menu_id": 3,
      "menu_name": "Calculator State Management",
      "sequence_number": 3,
      "description": "Manages the current state of the calculator interface.",
      "submenus": [
        {
          "submenu_id": 301,
          "submenu_name": "Calculator State",
          "sequence_number": 1,
          "description": "Stores the current state of the calculator interface.",
          "tables": [
            {
              "table_name": "calculator_state",
              "comment": "Represents the current state of the calculator interface.",
              "columns": [
                {
                  "name": "id",
                  "type": "UUID",
                  "constraints": "PRIMARY KEY DEFAULT gen_random_uuid()"
                },
                {
                  "name": "submenu_id",
                  "type": "INT",
                  "constraints": "DEFAULT 301 NOT NULL REFERENCES calc.submenu(submenu_id)"
                },
                {
                  "name": "session_history_id",
                  "type": "UUID",
                  "constraints": "NOT NULL UNIQUE",
                  "comment": "References the session associated with the calculator state"
                },
                {
                  "name": "current_display",
                  "type": "VARCHAR(255)",
                  "constraints": "NOT NULL DEFAULT '0'",
                  "comment": "Current value shown on the display"
                },
                {
                  "name": "previous_value",
                  "type": "NUMERIC",
                  "comment": "Previously entered value"
                },
                {
                  "name": "current_operator",
                  "type": "VARCHAR(10)",
                  "constraints": "CHECK (current_operator IN ('+', '-', '×', '÷', '*', '/', NULL))",
                  "comment": "Currently selected operator"
                },
                {
                  "name": "awaiting_operand",
                  "type": "BOOLEAN",
                  "constraints": "NOT NULL DEFAULT false",
                  "comment": "Whether calculator is waiting for next operand input"
                },
                {
                  "name": "is_result_displayed",
                  "type": "BOOLEAN",
                  "constraints": "NOT NULL DEFAULT false",
                  "comment": "Whether the display shows a result"
                },
                {
                  "name": "memory",
                  "type": "NUMERIC",
                  "constraints": "NOT NULL DEFAULT 0",
                  "comment": "Value stored in memory for M+, M-, MR, MC functions"
                },
                {
                  "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_history_id",
                  "references": "calc.session_history(id)",
                  "on_delete": "CASCADE"
                }
              ]
            }
          ]
        }
      ]
    }
  ],
  "assumptions": [
    "Session history is maintained only for the duration of the browser session.",
    "Calculations are stored with a UUID for unique identification.",
    "Calculator state is reset upon page refresh."
  ],
  "open_questions": [
    "Should the calculator support scientific functions in the future?",
    "Is there a need for persistent storage of session history beyond the session?"
  ]
}
