File size: 5,626 Bytes
9fbb3f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import React from "react";
import {
  AreaChart,
  Area,
  XAxis,
  YAxis,
  Tooltip,
  ResponsiveContainer,
  Legend,
  ReferenceLine,
} from "recharts";

function dollars(v) {
  if (v >= 1_000_000_000) return "$" + (v / 1_000_000_000).toFixed(1) + "B";
  if (v >= 1_000_000) return "$" + (v / 1_000_000).toFixed(1) + "M";
  if (v >= 1_000) return "$" + (v / 1_000).toFixed(0) + "K";
  return "$" + v.toFixed(0);
}

function pct(v) {
  return (v * 100).toFixed(1) + "%";
}

export default function GoalProjection({ goalProjection, financialGoal }) {
  const {
    probability_of_success,
    median_outcome,
    surplus_or_shortfall,
    required_cagr,
    method,
    yearly_projections,
  } = goalProjection;

  const target = financialGoal?.target_amount || 1_000_000;
  const years = financialGoal?.time_horizon_years || 20;
  const goalType = financialGoal?.goal_type || "Retirement";

  const isOnTrack = surplus_or_shortfall >= 0;

  const tooltipFormatter = (value, name) => {
    const labels = {
      p90: "90th Percentile",
      p50: "Median Path",
      p10: "10th Percentile",
    };
    return [dollars(value), labels[name] || name];
  };

  return (
    <section className="section">
      <h2>Goal-Based Planning</h2>

      {/* Status Banner */}
      <div className={`status-banner ${isOnTrack ? "success" : "error"}`}>
        <span>🎯</span>
        <strong>{goalType}:</strong> {pct(probability_of_success)} probability of reaching {dollars(target)} in {years} years
      </div>

      <div className="metrics-row">
        <div className="metric-card">
          <span className="metric-label">Probability of Success</span>
          <span
            className="metric-value"
            style={{ color: probability_of_success >= 0.7 ? "#22c55e" : probability_of_success >= 0.4 ? "#f59e0b" : "#ef4444" }}
          >
            {pct(probability_of_success)}
          </span>
        </div>
        <div className="metric-card">
          <span className="metric-label">Median Outcome</span>
          <span className="metric-value">{dollars(median_outcome)}</span>
        </div>
        <div className="metric-card">
          <span className="metric-label">Surplus / Shortfall</span>
          <span
            className="metric-value"
            style={{ color: isOnTrack ? "#22c55e" : "#ef4444" }}
          >
            {isOnTrack ? "+" : ""}
            {dollars(surplus_or_shortfall)}
          </span>
          <span className={`status-badge ${isOnTrack ? "on-track" : "off-track"}`}>
            {isOnTrack ? "↑ On track" : "↓ Off track"}
          </span>
        </div>
        <div className="metric-card">
          <span className="metric-label">Required CAGR</span>
          <span className="metric-value">{pct(required_cagr)}</span>
        </div>
      </div>

      <h3 style={{ textAlign: "center", marginTop: "2rem", color: "#64748b", fontSize: "1.5rem" }}>
        Projection: {goalType}
      </h3>

      <ResponsiveContainer width="100%" height={450}>
        <AreaChart
          data={yearly_projections}
          margin={{ top: 20, right: 30, bottom: 20, left: 40 }}
        >
          <defs>
            <linearGradient id="band90" x1="0" y1="0" x2="0" y2="1">
              <stop offset="5%" stopColor="#22c55e" stopOpacity={0.2} />
              <stop offset="95%" stopColor="#22c55e" stopOpacity={0.05} />
            </linearGradient>
          </defs>
          <XAxis
            dataKey="year"
            label={{ value: "Years", position: "bottom", offset: 0, fill: "#94a3b8" }}
            tick={{ fill: "#94a3b8" }}
          />
          <YAxis
            tickFormatter={dollars}
            label={{
              value: "Portfolio Value ($)",
              angle: -90,
              position: "insideLeft",
              offset: -10,
              fill: "#94a3b8"
            }}
            tick={{ fill: "#94a3b8" }}
          />
          <Tooltip
            contentStyle={{ backgroundColor: "#1e293b", border: "1px solid #475569", borderRadius: "8px" }}
            formatter={tooltipFormatter}
            labelFormatter={(y) => `Year ${y}`}
          />
          <Legend verticalAlign="top" height={36} />

          {/* 10th-90th percentile band */}
          <Area
            type="monotone"
            dataKey="p90"
            stroke="#22c55e"
            strokeOpacity={0.1}
            fill="url(#band90)"
            name="10th-90th Percentile"
          />
          <Area
            type="monotone"
            dataKey="p10"
            stroke="none"
            fill="#0f172a"
            fillOpacity={1}
            name="p10-base"
            legendType="none"
          />

          {/* Median path */}
          <Area
            type="monotone"
            dataKey="p50"
            stroke="#3b82f6"
            strokeWidth={3}
            fill="none"
            name="Median Path"
          />

          {/* Target line */}
          <ReferenceLine
            y={target}
            stroke="#ef4444"
            strokeDasharray="5 5"
            strokeWidth={3}
            label={{
              value: `Target: ${dollars(target)}`,
              position: "insideBottomRight",
              fill: "#ef4444",
              fontSize: 14,
              fontWeight: 600,
              offset: 20
            }}
          />
        </AreaChart>
      </ResponsiveContainer>

      <details style={{ marginTop: "1rem" }}>
        <summary>Simulation Methodology</summary>
        <div className="alert info" style={{ marginTop: "0.5rem" }}>
          {method}
        </div>
      </details>
    </section>
  );
}