use brk_cohort::{Loss, Profit, ProfitabilityRange};
use brk_error::Result;
use brk_indexer::Lengths;
use brk_traversable::Traversable;
use brk_types::{Bitcoin, Cents, Dollars, Height, PartsPerMillionSigned32, Sats, Version};
use vecdb::{AnyStoredVec, AnyVec, CachedBoxedVec, Database, Exit, Rw, StorageMode, WritableVec};

use crate::{
    indexes,
    internal::{
        CachedWindowStartVec, PerBlock, RatioPerBlock, SpotValuePerBlock,
        SpotValuePerBlockWithDeltas, Windows,
    },
    price,
};

#[derive(Traversable)]
pub struct WithSth<All, Sth = All> {
    pub all: All,
    pub sth: Sth,
}

#[derive(Traversable)]
pub struct ProfitabilityBucket<M: StorageMode = Rw> {
    pub supply: WithSth<SpotValuePerBlockWithDeltas<M>, SpotValuePerBlock<M>>,
    pub realized_cap: WithSth<PerBlock<Dollars, M>>,
    pub unrealized_pnl: WithSth<PerBlock<Dollars, M>>,
    pub nupl: RatioPerBlock<PartsPerMillionSigned32, M>,
}

impl<M: StorageMode> ProfitabilityBucket<M> {
    fn min_len(&self) -> usize {
        self.supply
            .all
            .sats
            .height
            .len()
            .min(self.realized_cap.all.height.len())
    }
}

impl ProfitabilityBucket {
    fn forced_import(
        db: &Database,
        name: &str,
        version: Version,
        indexes: &indexes::Vecs,
        cached_starts: &Windows<&CachedWindowStartVec>,
        spot_price: &CachedBoxedVec<Height, Cents>,
    ) -> Result<Self> {
        Ok(Self {
            supply: WithSth {
                all: SpotValuePerBlockWithDeltas::forced_import(
                    db,
                    &format!("{name}_supply"),
                    version,
                    indexes,
                    cached_starts,
                    spot_price,
                )?,
                sth: SpotValuePerBlock::forced_import(
                    db,
                    &format!("{name}_sth_supply"),
                    version,
                    indexes,
                    spot_price,
                )?,
            },
            realized_cap: WithSth {
                all: PerBlock::forced_import(
                    db,
                    &format!("{name}_realized_cap"),
                    version,
                    indexes,
                )?,
                sth: PerBlock::forced_import(
                    db,
                    &format!("{name}_sth_realized_cap"),
                    version,
                    indexes,
                )?,
            },
            unrealized_pnl: WithSth {
                all: PerBlock::forced_import(
                    db,
                    &format!("{name}_unrealized_pnl"),
                    version,
                    indexes,
                )?,
                sth: PerBlock::forced_import(
                    db,
                    &format!("{name}_sth_unrealized_pnl"),
                    version,
                    indexes,
                )?,
            },
            nupl: RatioPerBlock::forced_import_ppm(
                db,
                &format!("{name}_nupl"),
                version + Version::ONE,
                indexes,
            )?,
        })
    }

    #[inline(always)]
    pub(crate) fn push(
        &mut self,
        supply: Sats,
        sth_supply: Sats,
        realized_cap: Dollars,
        sth_realized_cap: Dollars,
    ) {
        self.supply.all.sats.height.push(supply);
        self.supply.sth.sats.height.push(sth_supply);
        self.realized_cap.all.height.push(realized_cap);
        self.realized_cap.sth.height.push(sth_realized_cap);
    }

    pub(crate) fn compute(
        &mut self,
        prices: &price::Vecs,
        starting_lengths: &Lengths,
        is_profit: bool,
        exit: &Exit,
    ) -> Result<()> {
        let max_from = starting_lengths.height;

        self.unrealized_pnl.all.height.compute_transform3(
            max_from,
            &prices.spot.cents.height,
            &self.realized_cap.all.height,
            &self.supply.all.sats.height,
            |(i, spot, cap, supply, ..)| {
                let mv = f64::from(Dollars::from(spot)) * f64::from(Bitcoin::from(supply));
                let rc = f64::from(cap);
                let pnl = if is_profit { mv - rc } else { rc - mv }.max(0.0);
                (i, Dollars::from(pnl))
            },
            exit,
        )?;
        self.unrealized_pnl.sth.height.compute_transform3(
            max_from,
            &prices.spot.cents.height,
            &self.realized_cap.sth.height,
            &self.supply.sth.sats.height,
            |(i, spot, cap, supply, ..)| {
                let mv = f64::from(Dollars::from(spot)) * f64::from(Bitcoin::from(supply));
                let rc = f64::from(cap);
                let pnl = if is_profit { mv - rc } else { rc - mv }.max(0.0);
                (i, Dollars::from(pnl))
            },
            exit,
        )?;

        self.nupl.ppm.height.compute_transform3(
            max_from,
            &prices.spot.cents.height,
            &self.realized_cap.all.height,
            &self.supply.all.sats.height,
            |(i, spot, cap_dollars, supply_sats, ..)| {
                let p = spot.as_u128();
                let supply = supply_sats.as_u128();
                if p == 0 || supply == 0 {
                    (i, PartsPerMillionSigned32::ZERO)
                } else {
                    let rp = Cents::from(cap_dollars).as_u128() * Sats::ONE_BTC_U128 / supply;
                    let ratio = (p as f64 - rp as f64) / p as f64;
                    (i, PartsPerMillionSigned32::from(ratio))
                }
            },
            exit,
        )?;

        Ok(())
    }

    pub(crate) fn compute_from_ranges(
        &mut self,
        prices: &price::Vecs,
        starting_lengths: &Lengths,
        is_profit: bool,
        sources: &[&ProfitabilityBucket],
        exit: &Exit,
    ) -> Result<()> {
        let max_from = starting_lengths.height;

        self.supply.all.sats.height.compute_sum_of_others(
            max_from,
            &sources
                .iter()
                .map(|s| &s.supply.all.sats.height)
                .collect::<Vec<_>>(),
            exit,
        )?;
        self.supply.sth.sats.height.compute_sum_of_others(
            max_from,
            &sources
                .iter()
                .map(|s| &s.supply.sth.sats.height)
                .collect::<Vec<_>>(),
            exit,
        )?;
        self.realized_cap.all.height.compute_sum_of_others(
            max_from,
            &sources
                .iter()
                .map(|s| &s.realized_cap.all.height)
                .collect::<Vec<_>>(),
            exit,
        )?;
        self.realized_cap.sth.height.compute_sum_of_others(
            max_from,
            &sources
                .iter()
                .map(|s| &s.realized_cap.sth.height)
                .collect::<Vec<_>>(),
            exit,
        )?;

        self.compute(prices, starting_lengths, is_profit, exit)
    }

    pub(crate) fn collect_all_vecs_mut(&mut self) -> Vec<&mut dyn AnyStoredVec> {
        vec![
            &mut self.supply.all.inner.sats.height as &mut dyn AnyStoredVec,
            &mut self.supply.sth.sats.height,
            &mut self.realized_cap.all.height,
            &mut self.realized_cap.sth.height,
            &mut self.unrealized_pnl.all.height,
            &mut self.unrealized_pnl.sth.height,
            &mut self.nupl.ppm.height,
        ]
    }
}

/// All profitability metrics: 25 ranges + 14 profit thresholds + 9 loss thresholds.
#[derive(Traversable)]
pub struct ProfitabilityMetrics<M: StorageMode = Rw> {
    pub range: ProfitabilityRange<ProfitabilityBucket<M>>,
    pub profit: Profit<ProfitabilityBucket<M>>,
    pub loss: Loss<ProfitabilityBucket<M>>,
}

impl<M: StorageMode> ProfitabilityMetrics<M> {
    pub fn iter(&self) -> impl Iterator<Item = &ProfitabilityBucket<M>> {
        self.range
            .iter()
            .chain(self.profit.iter())
            .chain(self.loss.iter())
    }

    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut ProfitabilityBucket<M>> {
        self.range
            .iter_mut()
            .chain(self.profit.iter_mut())
            .chain(self.loss.iter_mut())
    }

    pub(crate) fn min_stateful_len(&self) -> usize {
        self.range.iter().map(|b| b.min_len()).min().unwrap_or(0)
    }
}

impl ProfitabilityMetrics {
    pub(crate) fn forced_import(
        db: &Database,
        version: Version,
        indexes: &indexes::Vecs,
        cached_starts: &Windows<&CachedWindowStartVec>,
        spot_price: &CachedBoxedVec<Height, Cents>,
    ) -> Result<Self> {
        let range = ProfitabilityRange::try_new(|name| {
            ProfitabilityBucket::forced_import(
                db,
                name,
                version,
                indexes,
                cached_starts,
                spot_price,
            )
        })?;

        let aggregate_version = version + Version::TWO;

        let profit = Profit::try_new(|name| {
            ProfitabilityBucket::forced_import(
                db,
                name,
                aggregate_version,
                indexes,
                cached_starts,
                spot_price,
            )
        })?;

        let loss = Loss::try_new(|name| {
            ProfitabilityBucket::forced_import(
                db,
                name,
                aggregate_version,
                indexes,
                cached_starts,
                spot_price,
            )
        })?;

        Ok(Self {
            range,
            profit,
            loss,
        })
    }

    pub(crate) fn compute(
        &mut self,
        prices: &price::Vecs,
        starting_lengths: &Lengths,
        exit: &Exit,
    ) -> Result<()> {
        for (is_profit, bucket) in self.range.iter_mut_with_is_profit() {
            bucket.compute(prices, starting_lengths, is_profit, exit)?;
        }

        let range_arr = self.range.as_array();

        for (threshold, sources) in self.profit.iter_mut_with_growing_prefix(&range_arr) {
            threshold.compute_from_ranges(prices, starting_lengths, true, sources, exit)?;
        }
        for (threshold, sources) in self.loss.iter_mut_with_growing_suffix(&range_arr) {
            threshold.compute_from_ranges(prices, starting_lengths, false, sources, exit)?;
        }

        Ok(())
    }

    pub(crate) fn collect_all_vecs_mut(&mut self) -> Vec<&mut dyn AnyStoredVec> {
        let mut vecs = Vec::new();
        for bucket in self.iter_mut() {
            vecs.extend(bucket.collect_all_vecs_mut());
        }
        vecs
    }
}
