//! One stake per trader per **`(Market::series_id, Market::period)`** (**`PlaceBet`** creates / tops up). PDA: [`position_pda`](super::position_pda).

use steel::*;

use super::{position_pda, StreakAccount};

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct Position {
    pub authority: Pubkey,
    pub series_id: u16,
    pub _pad_series: [u8; 6],
    pub period: u64,
    pub stake: u64,
    /// [`Market::SIDE_UP`](super::Market::SIDE_UP) or [`Market::SIDE_DOWN`](super::Market::SIDE_DOWN); matches [`Market::outcome`] after settle.
    pub side: u8,
    /// [`STATE_PENDING`] / [`STATE_COMMITTED_PREOPEN`] / [`STATE_CLAIMED_WIN`] / [`STATE_FINALIZED_LOSS`] / [`STATE_COMMIT_REFUNDED`].
    pub state: u8,
    pub _pad: [u8; 6],
}

impl Position {
    /// Open (**`PlaceBet`** may add stake until settle).
    pub const STATE_PENDING: u8 = 0;

    /// Winner paid via **`ExecutorTreasury`** (**`distribute`**) (**executor**).
    pub const STATE_CLAIMED_WIN: u8 = 1;

    /// Loser bookkeeping: the next period’s **`BuyTickets`** or **`PlaceBet`** finalizes this after settlement.
    pub const STATE_FINALIZED_LOSS: u8 = 2;

    /// Locked **before** open-anchor / early window: tickets debited; merged into **`STATE_PENDING`** + **`total_*`** during live window via **`PlaceBet`** (**`ticket_units == 0`**, user), first live **`PlaceBet`** with stake, or **`ExecutorTreasury`** **`EXECUTOR_KIND_MERGE_COMMITTED_POSITION`** (executor).
    pub const STATE_COMMITTED_PREOPEN: u8 = 3;

    /// Reserved legacy state (**no instruction writes this today**); **`PlaceBet`** commit path may recycle **`stake == 0`** accounts.
    pub const STATE_COMMIT_REFUNDED: u8 = 4;

    pub fn pda(series_id: u16, period: u64, authority: Pubkey) -> (Pubkey, u8) {
        position_pda(series_id, period, authority)
    }
}

account!(StreakAccount, Position);
