//! Core types for rustledger
//!
//! This crate provides the fundamental types used throughout the rustledger project:
//!
//! - [`Amount`] - A decimal number with a currency
//! - [`Cost`] - Acquisition cost of a position (lot)
//! - [`CostSpec`] - Specification for matching or creating costs
//! - [`Position`] - Units held at a cost
//! - [`Inventory`] - A collection of positions with booking support
//! - [`BookingMethod`] - How to match lots when reducing positions
//! - [`Directive`] - All directive types (Transaction, Balance, Open, etc.)
//!
//! # Example
//!
//! ```
//! use rustledger_core::{Amount, Cost, Position, Inventory, BookingMethod};
//! use rust_decimal_macros::dec;
//!
//! // Create an inventory
//! let mut inv = Inventory::new();
//!
//! // Add a stock position with cost
//! let cost = Cost::new(dec!(150.00), "USD")
//!     .with_date(rustledger_core::naive_date(2024, 1, 15).unwrap());
//! inv.add(Position::with_cost(Amount::new(dec!(10), "AAPL"), cost));
//!
//! // Check holdings
//! assert_eq!(inv.units("AAPL"), dec!(10));
//!
//! // Sell some shares using FIFO
//! let result = inv.reduce(
//!     &Amount::new(dec!(-5), "AAPL"),
//!     None,
//!     BookingMethod::Fifo,
//! ).unwrap();
//!
//! assert_eq!(inv.units("AAPL"), dec!(5));
//! assert_eq!(result.cost_basis.unwrap().number, dec!(750.00)); // 5 * 150
//! ```

#![forbid(unsafe_code)]
#![warn(missing_docs)]

pub mod amount;
pub mod calendar;
pub mod cost;
pub mod decimal;
pub mod directive;
pub mod display_context;
pub mod extract;
pub mod format;
pub mod identifiers;
pub mod implicit_prices;
pub mod intern;
pub mod inventory;
pub mod meta_json;
pub mod position;
pub mod shift_spans_impls;
pub mod span;
pub mod synthetic;
pub mod visit;

// Kani formal verification proofs (only compiled with Kani)
#[cfg(kani)]
mod kani_proofs;

pub use amount::{Amount, AmountParseError, AmountParseErrorReason, IncompleteAmount};
pub use calendar::{CalendarPeriod, quarter_index0};
pub use cost::{BookedCost, BookedCostInvariantError, Cost, CostNumber, CostSpec};
pub use decimal::{
    add_python_scale, checked_add_python_scale, checked_div_python_scale, checked_sub_python_scale,
    negate_python, round_dp_python, sub_python_scale,
};
pub use directive::{
    Balance, Close, Commodity, Custom, Directive, DirectivePriority, Document, Event, MetaValue,
    Metadata, Note, Open, Pad, Posting, Price, PriceAnnotation, PriceKind, Query, Transaction,
    booking_sort_key, meta_value_as_bool, parse_bool_word, parse_precision_meta, sort_directives,
};
pub use display_context::{DEFAULT_CURRENCY, DisplayContext, OutputSurface, Precision};
pub use extract::{
    DEFAULT_CURRENCIES, extract_accounts, extract_accounts_iter, extract_currencies,
    extract_currencies_iter, extract_links, extract_links_iter, extract_payees,
    extract_payees_iter, extract_tags, extract_tags_iter,
};
pub use format::{
    Alignment, FormatConfig, FormatLine, format_directive_lines, format_directives,
    format_posting_line, posting_format_line, render_lines, resolve_alignment,
};
pub use identifiers::{
    ACCOUNT_TYPES, Account, AccountTypeKind, AccountTypes, Currency, Link, Tag, account_type,
    is_default_account_root, is_subaccount_or_equal,
};
pub use implicit_prices::extract_per_unit_price;
pub use intern::{InternedStr, StringInterner};
pub use inventory::{
    AccountedBookingError, BookingError, BookingMethod, BookingResult, Inventory, OverflowError,
    ReductionScope, sum_account_and_subaccounts,
};
pub use meta_json::{json_to_meta_value, meta_value_to_json, meta_value_type_tag};
pub use position::Position;
pub use span::{SYNTHESIZED_FILE_ID, ShiftSpans, Span, Spanned};
pub use visit::{visit_accounts, visit_currencies};

// Re-export commonly used external types
/// Calendar date without timezone. Alias for `jiff::civil::Date`.
pub type NaiveDate = jiff::civil::Date;
pub use rust_decimal::Decimal;

/// Construct a [`NaiveDate`] from `(year, month, day)` with i32/u32 arguments.
///
/// Wraps [`jiff::civil::date`] which takes `(i16, i8, i8)`.
/// Returns `None` if the date is invalid.
#[must_use]
pub fn naive_date(year: i32, month: u32, day: u32) -> Option<NaiveDate> {
    let y = i16::try_from(year).ok()?;
    let m = i8::try_from(month).ok()?;
    let d = i8::try_from(day).ok()?;
    NaiveDate::new(y, m, d).ok()
}

// Re-export rkyv wrappers when feature is enabled
#[cfg(feature = "rkyv")]
pub use intern::{AsDecimal, AsInternedStr, AsNaiveDate};
