//! Price-chain settlement helper for `AdminInstantSettlement`.
//!
//! Reads the open reference from `Treasury::last_close_*`, reads the Pyth close price,
//! resolves UP/DOWN, emits `PeriodSettled`, and writes the close price back to Treasury.
//! All market pool math and USDC distribution happen off-chain.

use steel::*;

use crate::consts::PYTH_BTC_USD_FEED_ID;
use crate::event::PeriodSettled;
use crate::pyth::{load_fresh_price, outcome_from_open_close, Price};
use crate::state::Treasury;

pub struct SettlementResult {
    pub outcome: u8,
    pub close_px: Price,
    pub open_px: Price,
}

/// Read open from Treasury, read close from Pyth, return outcome + both prices.
///
/// On the very first call (`last_close_price == 0`, genesis not yet set), the
/// Pyth close price is used as both open and close — outcome is UP (0) and the
/// price chain is seeded for subsequent periods.
pub fn resolve_settlement(
    treasury: &Treasury,
    pyth_price_feed_info: &AccountInfo<'_>,
    unix_timestamp: i64,
) -> Result<SettlementResult, ProgramError> {
    let close_px = load_fresh_price(pyth_price_feed_info, &PYTH_BTC_USD_FEED_ID, unix_timestamp)?;

    if treasury.last_close_price == 0 {
        // Genesis: seed the price chain; emit outcome UP as a neutral sentinel.
        return Ok(SettlementResult {
            outcome: crate::settlement::SIDE_UP,
            open_px: close_px,
            close_px,
        });
    }

    let open_px = Price {
        price: treasury.last_close_price,
        conf: 0,
        expo: treasury.last_close_expo,
        publish_time: treasury.last_close_publish_time,
    };

    let outcome = outcome_from_open_close(open_px, close_px)?;

    Ok(SettlementResult { outcome, close_px, open_px })
}

/// Write close price back to Treasury and emit the `PeriodSettled` event.
pub fn commit_settlement(
    treasury: &mut Treasury,
    result: SettlementResult,
    series_id: u16,
    period: u64,
) {
    PeriodSettled {
        series_id,
        outcome: result.outcome,
        _pad: [0; 5],
        period,
        close_price: result.close_px.price,
        close_expo: result.close_px.expo,
        _pad2: [0; 4],
        close_publish_time: result.close_px.publish_time,
        open_price: result.open_px.price,
        open_expo: result.open_px.expo,
        _pad3: [0; 4],
    }
    .log();

    treasury.last_close_price = result.close_px.price;
    treasury.last_close_expo = result.close_px.expo;
    treasury.last_close_publish_time = result.close_px.publish_time;
}

/// UP outcome constant (close > open).
pub const SIDE_UP: u8 = 0;
/// DOWN outcome constant (close <= open, including ties).
pub const SIDE_DOWN: u8 = 1;
