use serde::{Deserialize, Serialize};
use solana_sdk::pubkey::Pubkey;

/// Request body for the login endpoint.
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct AuthRequest {
    pub message: String,       // Arbitrary message signed by the client
    pub client_pubkey: String, // Client's public key as a string
    pub signature: String,     // Base64 URL encoded signature of the message string
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct AutomationRequest {
    pub authority: String,
    pub amount: u64,
    pub strategy: AutomationStrategy,
}

#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub enum AutomationStrategy {
    Preferred(Vec<i64>),
    Random(i64),
}

impl AutomationStrategy {
    pub fn new(i: i64, squares: Vec<i64>) -> Self {
        match i {
            0 => AutomationStrategy::Preferred(squares),
            1 => AutomationStrategy::Random(squares[0]),
            _ => panic!("Invalid automation strategy"),
        }
    }
}

impl From<AutomationStrategy> for i64 {
    fn from(strategy: AutomationStrategy) -> Self {
        match strategy {
            AutomationStrategy::Preferred(_squares) => 0,
            AutomationStrategy::Random(_num_squares) => 1,
        }
    }
}

impl From<AutomationStrategy> for Vec<i64> {
    fn from(strategy: AutomationStrategy) -> Self {
        match strategy {
            AutomationStrategy::Preferred(squares) => squares,
            AutomationStrategy::Random(num_squares) => vec![num_squares],
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Automation {
    pub authority: Pubkey,
    pub is_enabled: bool,
    pub balance: u64,
    pub amount: u64,
    pub strategy: AutomationStrategy,
}
