public class TaxCalculator {
    // Official 2026 Standard Deduction Constants
    public static final double SINGLE_DEDUCTION = 16100.0;
    public static final double MARRIED_DEDUCTION = 32200.0;

    /**
     * Helper method to subtract the standard deduction from gross income.
     * Ensures that taxable income never drops below 0.0.
     */
    public static double applyStandardDeduction(double grossIncome, double deductionAmount) {
        double taxableIncome = grossIncome - deductionAmount;
        if (taxableIncome < 0) {
            taxableIncome = 0.0;
        }
        return taxableIncome;
    }

    /**
     * Calculates progressive federal income tax for a Single Filer.
     * Uses the 2026 top-down subtractive threshold boundaries.
     */
    public static double calculateSingleTax(double grossIncome) {
        double taxableIncome = applyStandardDeduction(grossIncome, SINGLE_DEDUCTION);
        
        if (taxableIncome <= 0) return 0.0;

        double tax = 0.0;
        double currentIncome = taxableIncome;

        // 37% Bracket: Income over $640,600
        if (currentIncome > 640600.0) {
            double marginalAmount = currentIncome - 640600.0;
            tax += marginalAmount * 0.37;
            currentIncome = 640600.0;
        }
        // 35% Bracket: Income over $256,225
        if (currentIncome > 256225.0) {
            double marginalAmount = currentIncome - 256225.0;
            tax += marginalAmount * 0.35;
            currentIncome = 256225.0;
        }
        // 32% Bracket: Income over $201,775
        if (currentIncome > 201775.0) {
            double marginalAmount = currentIncome - 201775.0;
            tax += marginalAmount * 0.32;
            currentIncome = 201775.0;
        }
        // 24% Bracket: Income over $105,700
        if (currentIncome > 105700.0) {
            double marginalAmount = currentIncome - 105700.0;
            tax += marginalAmount * 0.24;
            currentIncome = 105700.0;
        }
        // 22% Bracket: Income over $50,400
        if (currentIncome > 50400.0) {
            double marginalAmount = currentIncome - 50400.0;
            tax += marginalAmount * 0.22;
            currentIncome = 50400.0;
        }
        // 12% Bracket: Income over $12,400
        if (currentIncome > 12400.0) {
            double marginalAmount = currentIncome - 12400.0;
            tax += marginalAmount * 0.12;
            currentIncome = 12400.0;
        }
        // 10% Bracket: The remaining baseline income
        tax += currentIncome * 0.10;

        return tax;
    }

    /**
     * Calculates progressive federal income tax for Married Filing Jointly.
     * Uses the 2026 top-down subtractive threshold boundaries.
     */
    public static double calculateMarriedTax(double grossIncome) {
        double taxableIncome = applyStandardDeduction(grossIncome, MARRIED_DEDUCTION);
        
        if (taxableIncome <= 0) return 0.0;

        double tax = 0.0;
        double currentIncome = taxableIncome;

        // 37% Bracket: Income over $768,700
        if (currentIncome > 768700.0) {
            double marginalAmount = currentIncome - 768700.0;
            tax += marginalAmount * 0.37;
            currentIncome = 768700.0;
        }
        // 35% Bracket: Income over $512,450
        if (currentIncome > 512450.0) {
            double marginalAmount = currentIncome - 512450.0;
            tax += marginalAmount * 0.35;
            currentIncome = 512450.0;
        }
        // 32% Bracket: Income over $403,550
        if (currentIncome > 403550.0) {
            double marginalAmount = currentIncome - 403550.0;
            tax += marginalAmount * 0.32;
            currentIncome = 403550.0;
        }
        // 24% Bracket: Income over $211,400
        if (currentIncome > 211400.0) {
            double marginalAmount = currentIncome - 211400.0;
            tax += marginalAmount * 0.24;
            currentIncome = 211400.0;
        }
        // 22% Bracket: Income over $100,800
        if (currentIncome > 100800.0) {
            double marginalAmount = currentIncome - 100800.0;
            tax += marginalAmount * 0.22;
            currentIncome = 100800.0;
        }
        // 12% Bracket: Income over $24,800
        if (currentIncome > 24800.0) {
            double marginalAmount = currentIncome - 24800.0;
            tax += marginalAmount * 0.12;
            currentIncome = 24800.0;
        }
        // 10% Bracket: The remaining baseline income
        tax += currentIncome * 0.10;

        return tax;
    }
}