path = "/home/crogers2287/cfrmanager/src/hooks/usePropertyLedger.ts"
with open(path, 'r') as f:
    content = f.read()

# Fix summary init
old = """    const result: LedgerSummary = {
      totalSpent: 0,
      totalIncome: 0,
      taxDeductibleTotal: 0,"""

new = """    const result: LedgerSummary = {
      totalSpent: 0,
      totalIncome: 0,
      depositsHeld: 0,
      taxDeductibleTotal: 0,"""

content = content.replace(old, new)

# Fix the calculation loop — exclude security_deposit from income/expense
old = """      if (entry.isIncome) {
        result.totalIncome += entry.amount;
      } else {
        result.totalSpent += Math.abs(entry.amount);
      }"""

new = """      // Security deposits are a liability held in trust — never count as income or expense
      if (entry.category === "security_deposit") {
        if (entry.isIncome) result.depositsHeld += entry.amount;
      } else if (entry.isIncome) {
        result.totalIncome += entry.amount;
      } else {
        result.totalSpent += Math.abs(entry.amount);
      }"""

content = content.replace(old, new)

# Fix byMonth to also exclude deposits
old = """      if (entry.isIncome) {
        result.byMonth[monthKey].income += entry.amount;
      } else {
        result.byMonth[monthKey].expense += Math.abs(entry.amount);
      }"""

new = """      if (entry.category !== "security_deposit") {
        if (entry.isIncome) {
          result.byMonth[monthKey].income += entry.amount;
        } else {
          result.byMonth[monthKey].expense += Math.abs(entry.amount);
        }
      }"""

content = content.replace(old, new)

with open(path, 'w') as f:
    f.write(content)
print("usePropertyLedger.ts: Fixed")
