use crate::{CliError, Session};
use btctax_adapters::BundledTaxTables;
use btctax_core::whatif::{
self, HarvestReport, HarvestRequest, HarvestTarget, SellMethod, SellReport, SellRequest,
};
use btctax_core::{
Carryforward, EvaluateError, FilingStatus, LotMethod, TaxDate, TaxProfile, Usd, WalletId,
WhatIfError,
};
use btctax_store::Passphrase;
use std::path::Path;
use std::str::FromStr;
#[derive(Debug, Clone)]
pub struct AdhocProfile {
pub filing_status: FilingStatus,
pub income: Usd,
pub magi: Option<Usd>,
pub cf_long: Usd,
}
pub fn adhoc_profile(a: &AdhocProfile) -> TaxProfile {
TaxProfile {
filing_status: a.filing_status,
ordinary_taxable_income: a.income,
magi_excluding_crypto: a.magi.unwrap_or(a.income), qualified_dividends_and_other_pref_income: Usd::ZERO,
other_net_capital_gain: Usd::ZERO,
capital_loss_carryforward_in: Carryforward {
short: Usd::ZERO,
long: a.cf_long,
},
w2_ss_wages: Usd::ZERO,
w2_medicare_wages: Usd::ZERO,
schedule_c_expenses: Usd::ZERO,
}
}
#[derive(Debug)]
pub struct SellOutcome {
pub report: SellReport,
pub magi_caveat: bool,
}
#[allow(clippy::too_many_arguments)]
pub fn sell(
vault: &Path,
pp: &Passphrase,
sell_sat: i64,
wallet: WalletId,
at: TaxDate,
price: Option<Usd>,
method: Option<LotMethod>,
adhoc: Option<AdhocProfile>,
) -> Result<SellOutcome, CliError> {
let s = Session::open(vault, pp)?;
let (events, _state, cfg) = s.load_events_and_project()?;
let year = at.year();
let (profile, magi_caveat) = match &adhoc {
Some(a) => (Some(adhoc_profile(a)), a.magi.is_none()),
None => (s.tax_profile(year)?, false),
};
let prices = s.prices();
let tables = BundledTaxTables::load();
let req = SellRequest {
sell_sat,
wallet,
at,
price,
method: method.map(SellMethod::Method),
};
let report = whatif::sell(&events, prices, &cfg, profile.as_ref(), &tables, &req)
.map_err(map_whatif_err)?;
Ok(SellOutcome {
report,
magi_caveat,
})
}
#[derive(Debug)]
pub struct HarvestOutcome {
pub report: HarvestReport,
pub magi_caveat: bool,
}
pub fn parse_harvest_target(s: &str) -> Result<HarvestTarget, CliError> {
let lower = s.trim().to_ascii_lowercase();
match lower.as_str() {
"zero-ltcg" | "zero_ltcg" | "zeroltcg" => return Ok(HarvestTarget::ZeroLtcg),
"fifteen-ltcg" | "fifteen_ltcg" | "fifteenltcg" => return Ok(HarvestTarget::FifteenLtcg),
_ => {}
}
if let Some(v) = lower.strip_prefix("gain=") {
return Ok(HarvestTarget::Gain(parse_target_amount(v)?));
}
if let Some(v) = lower.strip_prefix("tax=") {
return Ok(HarvestTarget::Tax(parse_target_amount(v)?));
}
Err(CliError::Usage(format!(
"bad --target {s:?}: expected zero-ltcg | fifteen-ltcg | gain=$X | tax=$X"
)))
}
fn parse_target_amount(v: &str) -> Result<Usd, CliError> {
let cleaned = v.trim().replace(['$', ','], "");
Usd::from_str(&cleaned).map_err(|e| {
CliError::Usage(format!(
"bad --target amount {v:?}: expected a USD number: {e}"
))
})
}
#[allow(clippy::too_many_arguments)]
pub fn harvest(
vault: &Path,
pp: &Passphrase,
wallet: WalletId,
at: TaxDate,
price: Option<Usd>,
target: HarvestTarget,
adhoc: Option<AdhocProfile>,
) -> Result<HarvestOutcome, CliError> {
let s = Session::open(vault, pp)?;
let (events, _state, cfg) = s.load_events_and_project()?;
let year = at.year();
let (profile, magi_caveat) = match &adhoc {
Some(a) => (Some(adhoc_profile(a)), a.magi.is_none()),
None => (s.tax_profile(year)?, false),
};
let prices = s.prices();
let tables = BundledTaxTables::load();
let req = HarvestRequest {
wallet,
at,
price,
target,
};
let report = whatif::harvest(&events, prices, &cfg, profile.as_ref(), &tables, &req)
.map_err(map_whatif_err)?;
Ok(HarvestOutcome {
report,
magi_caveat,
})
}
pub fn map_whatif_err(e: WhatIfError) -> CliError {
match e {
WhatIfError::YearNotComputable(b) => CliError::Usage(format!(
"year not computable — resolve the blocker or set a tax profile first: [{:?}] {}",
b.kind, b.detail
)),
WhatIfError::PreTransitionYear(y) => CliError::Usage(format!(
"{y} is pre-2025: a pre-2025 sale restates a closed year — not a plan"
)),
WhatIfError::NoLots => {
CliError::Usage("no lots available to sell from that wallet as of that date".into())
}
WhatIfError::Evaluate(EvaluateError::ProceedsRequired) => CliError::Usage(
"--price <usd-per-btc> is required for a future/off-dataset date with no bundled price"
.into(),
),
WhatIfError::Evaluate(ev) => CliError::Usage(format!("evaluate error: {ev:?}")),
WhatIfError::InvalidTarget(msg) => CliError::Usage(msg),
}
}
#[cfg(test)]
mod tests {
use super::*;
use rust_decimal_macros::dec;
#[test]
fn adhoc_magi_defaults_to_income() {
let defaulted = adhoc_profile(&AdhocProfile {
filing_status: FilingStatus::Single,
income: dec!(100000),
magi: None,
cf_long: Usd::ZERO,
});
assert_eq!(
defaulted.magi_excluding_crypto,
dec!(100000),
"MAGI must FLOOR at income, never $0"
);
assert_ne!(defaulted.magi_excluding_crypto, Usd::ZERO);
let explicit = adhoc_profile(&AdhocProfile {
filing_status: FilingStatus::Single,
income: dec!(100000),
magi: Some(dec!(130000)),
cf_long: Usd::ZERO,
});
assert_eq!(explicit.magi_excluding_crypto, dec!(130000));
}
#[test]
fn parse_harvest_target_forms() {
assert_eq!(
parse_harvest_target("zero-ltcg").unwrap(),
HarvestTarget::ZeroLtcg
);
assert_eq!(
parse_harvest_target("FIFTEEN-LTCG").unwrap(),
HarvestTarget::FifteenLtcg
);
assert_eq!(
parse_harvest_target("gain=$25,000").unwrap(),
HarvestTarget::Gain(dec!(25000))
);
assert_eq!(
parse_harvest_target("tax=$0").unwrap(),
HarvestTarget::Tax(dec!(0))
);
assert_eq!(
parse_harvest_target("tax=1500.50").unwrap(),
HarvestTarget::Tax(dec!(1500.50))
);
assert!(parse_harvest_target("nonsense").is_err());
assert!(parse_harvest_target("gain=abc").is_err());
}
#[test]
fn adhoc_carryforward_in_is_long_term() {
let p = adhoc_profile(&AdhocProfile {
filing_status: FilingStatus::Mfj,
income: dec!(80000),
magi: Some(dec!(90000)),
cf_long: dec!(25000),
});
assert_eq!(p.capital_loss_carryforward_in.long, dec!(25000));
assert_eq!(p.capital_loss_carryforward_in.short, Usd::ZERO);
}
}