use ratatui::layout::{Constraint, Direction, Layout, Rect};

pub struct DashboardLayout {
    pub ticker_bar: Rect,
    pub orderbook: Rect,
    pub chart: Option<Rect>,
    pub funding: Option<Rect>,
    pub symbol_details: Option<Rect>,
    pub trades: Rect,
    pub orders: Rect,
    pub positions: Rect,
    pub balances: Rect,
    pub status_bar: Rect,
}

pub fn compute(area: Rect) -> DashboardLayout {
    let wide = area.width >= 120;

    if wide {
        let main = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(3), // ticker bar
                Constraint::Fill(1),   // top section (takes remaining space)
                Constraint::Max(14),   // bottom strip (compact — just enough for data)
                Constraint::Length(1), // status bar
            ])
            .split(area);

        // Top section: 3 columns — orderbook 22% | chart+trades 40% | funding 38%
        let top_cols = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([
                Constraint::Percentage(22),
                Constraint::Percentage(40),
                Constraint::Percentage(38),
            ])
            .split(main[1]);

        // Center column: chart 55% stacked over trades 45%
        let center_split = Layout::default()
            .direction(Direction::Vertical)
            .constraints([Constraint::Percentage(55), Constraint::Percentage(45)])
            .split(top_cols[1]);

        // Right column: funding rates (55%) stacked over symbol details (45%)
        let right_split = Layout::default()
            .direction(Direction::Vertical)
            .constraints([Constraint::Percentage(55), Constraint::Percentage(45)])
            .split(top_cols[2]);

        // Bottom strip: 3 columns — orders | positions | balances side by side
        let bot_cols = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([
                Constraint::Percentage(33),
                Constraint::Percentage(40),
                Constraint::Percentage(27),
            ])
            .split(main[2]);

        DashboardLayout {
            ticker_bar: main[0],
            orderbook: top_cols[0],
            chart: Some(center_split[0]),
            funding: Some(right_split[0]),
            symbol_details: Some(right_split[1]),
            trades: center_split[1],
            orders: bot_cols[0],
            positions: bot_cols[1],
            balances: bot_cols[2],
            status_bar: main[3],
        }
    } else {
        // Narrow: no chart or funding columns
        let main = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(3),      // ticker bar
                Constraint::Percentage(50), // top row (orderbook + trades)
                Constraint::Percentage(50), // bottom row (orders + account)
                Constraint::Length(1),      // status bar
            ])
            .split(area);

        let top_cols = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
            .split(main[1]);

        let bot_rows = Layout::default()
            .direction(Direction::Vertical)
            .constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
            .split(main[2]);

        // Split bottom-right into positions + balances
        let account_split = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
            .split(bot_rows[1]);

        DashboardLayout {
            ticker_bar: main[0],
            orderbook: top_cols[0],
            trades: top_cols[1],
            chart: None,
            funding: None,
            symbol_details: None,
            orders: bot_rows[0],
            positions: account_split[0],
            balances: account_split[1],
            status_bar: main[3],
        }
    }
}
