import axios from 'axios';
import { logger } from '../utils/logger';

// Get API base URL from runtime config (window.env) or build-time config
// IMPORTANT: Call this function at REQUEST TIME, not at module load time,
// to ensure window.env is populated by env-config.js
const getApiBaseUrl = (): string => {
  // Check window.env first (runtime config from env.sh)
  if (typeof window !== 'undefined' && window.env?.API_BASE_URL) {
    return window.env.API_BASE_URL;
  }

  // Fall back to build-time env or production default
  // Next.js requires direct static reference to NEXT_PUBLIC_ variables
  return process.env.NEXT_PUBLIC_API_BASE_URL || 'https://puffinoffset.com/api';
};

export interface CreateCheckoutSessionParams {
  tons: number;
  portfolioId: number;
  pricePerTon: number; // Price per ton from Wren API
  customerEmail?: string;
}

export interface CheckoutSessionResponse {
  sessionId: string;
  url: string;
  orderId: string;
}

export interface OrderDetails {
  order: {
    id: string;
    tons: number;
    portfolioId: number;
    baseAmount: number;
    processingFee: number;
    totalAmount: number;
    currency: string;
    status: string;
    wrenOrderId: string | null;
    stripeSessionId: string;
    createdAt: string;
  };
  session: {
    paymentStatus: string;
    customerEmail?: string;
  };
}

/**
 * Create a Stripe checkout session
 * @param params Checkout session parameters
 * @returns Checkout session response with redirect URL
 */
export async function createCheckoutSession(
  params: CreateCheckoutSessionParams
): Promise<CheckoutSessionResponse> {
  try {
    const apiBaseUrl = getApiBaseUrl(); // Lazy evaluate at request time
    logger.info('Creating checkout session:', params);
    logger.info('Using API base URL:', apiBaseUrl);

    const response = await axios.post<CheckoutSessionResponse>(
      `${apiBaseUrl}/api/checkout/create-session`,
      params
    );

    logger.info('Checkout session created:', response.data.sessionId);
    return response.data;
  } catch (error) {
    if (axios.isAxiosError(error)) {
      logger.error('Checkout session creation failed:', error.response?.data || error.message);
      throw new Error(error.response?.data?.message || 'Failed to create checkout session');
    }
    throw error;
  }
}

/**
 * Get order details by session ID
 * @param sessionId Stripe session ID
 * @returns Order details
 */
export async function getOrderDetails(sessionId: string): Promise<OrderDetails> {
  try {
    const apiBaseUrl = getApiBaseUrl(); // Lazy evaluate at request time
    logger.info('Fetching order details for session:', sessionId);

    const response = await axios.get<OrderDetails>(
      `${apiBaseUrl}/api/checkout/session/${sessionId}`
    );

    logger.info('Order details retrieved:', response.data.order.id);
    return response.data;
  } catch (error) {
    if (axios.isAxiosError(error)) {
      logger.error('Order details retrieval failed:', error.response?.data || error.message);
      throw new Error(error.response?.data?.message || 'Failed to retrieve order details');
    }
    throw error;
  }
}

/**
 * Health check for backend API
 * @returns true if backend is healthy
 */
export async function checkBackendHealth(): Promise<boolean> {
  try {
    const apiBaseUrl = getApiBaseUrl(); // Lazy evaluate at request time
    const response = await axios.get(`${apiBaseUrl}/health`);
    return response.data.status === 'ok';
  } catch (error) {
    logger.error('Backend health check failed:', error);
    return false;
  }
}
