use serde::{Deserialize, Serialize};
use solana_program::pubkey::Pubkey;
use steel::*;

use super::OilAccount;

/// Referral account tracks a referrer's stats and pending rewards.
/// 
/// Anyone can create a Referral account to become a referrer.
/// When a new driller signs up with their referral link, 0.5% of that
/// driller's claimed rewards (both SOL and OIL) are credited to the
/// referrer's pending balance.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
pub struct Referral {
    /// The authority (wallet) of this referrer.
    pub authority: Pubkey,

    /// Total number of drillers referred by this referrer.
    pub total_referred: u64,

    /// Total SOL earned from referrals (lifetime, for stats).
    pub total_sol_earned: u64,

    /// Total OIL earned from referrals (lifetime, for stats).
    pub total_oil_earned: u64,

    /// Pending SOL rewards to claim.
    pub pending_sol: u64,

    /// Pending OIL rewards to claim.
    pub pending_oil: u64,
}

impl Referral {
    /// Claims pending SOL rewards.
    /// Returns the amount of SOL that can be claimed and resets pending_sol to 0.
    pub fn claim_sol(&mut self) -> u64 {
        let amount = self.pending_sol;
        self.pending_sol = 0;
        amount
    }

    /// Claims pending OIL rewards.
    /// Returns the amount of OIL that can be claimed and resets pending_oil to 0.
    pub fn claim_oil(&mut self) -> u64 {
        let amount = self.pending_oil;
        self.pending_oil = 0;
        amount
    }

    /// Calculates and credits SOL referral bonus (1.0% of total amount).
    /// Returns the referral amount that should be transferred to this referral account.
    /// 
    /// This function handles:
    /// - Calculating 1.0% referral bonus
    /// - Crediting pending_sol
    /// - Updating total_sol_earned
    pub fn credit_sol_referral(&mut self, total_amount: u64) -> u64 {
        // Calculate referral bonus (1.0% of total claim)
        let referral_amount = if total_amount > 0 {
            total_amount / 100 // 1.0% = 1/100
        } else {
            0
        };

        // Credit referral account
        if referral_amount > 0 {
            self.pending_sol += referral_amount;
            self.total_sol_earned += referral_amount;
        }

        referral_amount
    }

    /// Calculates and credits OIL referral bonus (1.0% of total amount).
    /// Returns the referral amount that should be transferred to this referral account.
    /// 
    /// This function handles:
    /// - Calculating 1.0% referral bonus
    /// - Crediting pending_oil
    /// - Updating total_oil_earned
    pub fn credit_oil_referral(&mut self, total_amount: u64) -> u64 {
        // Calculate referral bonus (1.0% of total claim)
        let referral_amount = if total_amount > 0 {
            total_amount / 100 // 1.0% = 1/100
        } else {
            0
        };

        // Credit referral account
        if referral_amount > 0 {
            self.pending_oil += referral_amount;
            self.total_oil_earned += referral_amount;
        }

        referral_amount
    }
}

account!(OilAccount, Referral);
