// Update member's contribution tier
import {
  getHelcimPlanId,
  requiresPayment,
} from "../../config/contributions.js";
import { connectDB } from "../../utils/mongoose.js";
import Member from "../../models/member.js";

const HELCIM_API_BASE = "https://api.helcim.com/v2";

export default defineEventHandler(async (event) => {
  try {
    const member = await requireAuth(event);
    await connectDB();
    const config = useRuntimeConfig(event);
    const body = await validateBody(event, updateContributionSchema);

    const oldTier = member.contributionTier;
    const newTier = body.contributionTier;

    // If same tier, nothing to do
    if (oldTier === newTier) {
      return {
        success: true,
        message: "Already on this tier",
      };
    }

    const helcimToken = config.helcimApiToken;
    const oldRequiresPayment = requiresPayment(oldTier);
    const newRequiresPayment = requiresPayment(newTier);

    // Case 1: Moving from free to paid tier
    if (!oldRequiresPayment && newRequiresPayment) {
      // Check if member has Helcim customer ID with saved payment method
      if (!member.helcimCustomerId) {
        throw createError({
          statusCode: 400,
          statusMessage:
            "Please use the subscription creation flow to upgrade to a paid tier",
          data: { requiresPaymentSetup: true },
        });
      }

      // Try to fetch customer info from Helcim to check for saved cards
      const helcimToken = config.helcimApiToken;

      try {
        const customerResponse = await fetch(
          `${HELCIM_API_BASE}/customers/${member.helcimCustomerId}`,
          {
            method: "GET",
            headers: {
              accept: "application/json",
              "api-token": helcimToken,
            },
          },
        );

        if (!customerResponse.ok) {
          throw new Error("Failed to fetch customer info");
        }

        const customerData = await customerResponse.json();
        const customerCode = customerData.customerCode;

        if (!customerCode) {
          throw new Error("No customer code found");
        }

        // Check if customer has saved cards
        const cardsResponse = await fetch(
          `${HELCIM_API_BASE}/card-terminals?customerId=${member.helcimCustomerId}`,
          {
            method: "GET",
            headers: {
              accept: "application/json",
              "api-token": helcimToken,
            },
          },
        );

        let hasCards = false;
        if (cardsResponse.ok) {
          const cardsData = await cardsResponse.json();
          hasCards = cardsData.cards && cardsData.cards.length > 0;
        }

        if (!hasCards) {
          throw new Error("No saved payment methods");
        }

        // Create new subscription with saved payment method
        const newPlanId = getHelcimPlanId(newTier);

        if (!newPlanId) {
          throw createError({
            statusCode: 400,
            statusMessage: `Plan not configured for tier ${newTier}`,
          });
        }

        // Generate idempotency key
        const chars =
          "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        let idempotencyKey = "";
        for (let i = 0; i < 25; i++) {
          idempotencyKey += chars.charAt(
            Math.floor(Math.random() * chars.length),
          );
        }

        // Get tier amount
        const { getContributionTierByValue } = await import(
          "../../config/contributions.js"
        );
        const tierInfo = getContributionTierByValue(newTier);

        const subscriptionResponse = await fetch(
          `${HELCIM_API_BASE}/subscriptions`,
          {
            method: "POST",
            headers: {
              accept: "application/json",
              "content-type": "application/json",
              "api-token": helcimToken,
              "idempotency-key": idempotencyKey,
            },
            body: JSON.stringify({
              subscriptions: [
                {
                  dateActivated: new Date().toISOString().split("T")[0],
                  paymentPlanId: parseInt(newPlanId),
                  customerCode: customerCode,
                  recurringAmount: parseFloat(tierInfo.amount),
                  paymentMethod: "card",
                },
              ],
            }),
          },
        );

        if (!subscriptionResponse.ok) {
          const errorText = await subscriptionResponse.text();
          console.error("Failed to create subscription:", errorText);
          throw new Error('Subscription creation failed');
        }

        const subscriptionData = await subscriptionResponse.json();
        const subscription = subscriptionData.data?.[0];

        if (!subscription) {
          throw new Error("No subscription returned in response");
        }

        // Update member record
        await Member.findByIdAndUpdate(
          member._id,
          { $set: { contributionTier: newTier, helcimSubscriptionId: subscription.id, paymentMethod: "card", status: "active" } },
          { runValidators: false }
        );

        return {
          success: true,
          message: "Successfully upgraded to paid tier",
          subscription: {
            subscriptionId: subscription.id,
            status: subscription.status,
            nextBillingDate: subscription.nextBillingDate,
          },
        };
      } catch (error) {
        console.error("Error creating subscription with saved payment:", error);
        // If we can't use saved payment, require new payment setup
        throw createError({
          statusCode: 400,
          statusMessage:
            "Payment information required. You'll be redirected to complete payment setup.",
          data: { requiresPaymentSetup: true },
        });
      }
    }

    // Case 2: Moving from paid to free tier (cancel subscription)
    if (oldRequiresPayment && !newRequiresPayment) {
      if (member.helcimSubscriptionId) {
        try {
          // Cancel Helcim subscription
          const response = await fetch(
            `${HELCIM_API_BASE}/subscriptions/${member.helcimSubscriptionId}`,
            {
              method: "DELETE",
              headers: {
                accept: "application/json",
                "api-token": helcimToken,
              },
            },
          );

          if (!response.ok) {
            console.error(
              "Failed to cancel Helcim subscription:",
              response.status,
            );
          }
        } catch (error) {
          console.error("Error canceling Helcim subscription:", error);
          // Continue anyway - we'll update the member record
        }
      }

      // Update member to free tier
      await Member.findByIdAndUpdate(
        member._id,
        { $set: { contributionTier: newTier, helcimSubscriptionId: null, paymentMethod: "none" } },
        { runValidators: false }
      );

      return {
        success: true,
        message: "Successfully downgraded to free tier",
      };
    }

    // Case 3: Moving between paid tiers
    if (oldRequiresPayment && newRequiresPayment) {
      const newPlanId = getHelcimPlanId(newTier);

      if (!newPlanId) {
        throw createError({
          statusCode: 400,
          statusMessage: `Plan not configured for tier ${newTier}`,
        });
      }

      if (!member.helcimSubscriptionId) {
        // No subscription exists - they need to go through payment flow
        throw createError({
          statusCode: 400,
          statusMessage:
            "Payment information required. You'll be redirected to complete payment setup.",
          data: { requiresPaymentSetup: true },
        });
      }

      try {
        // Update subscription plan in Helcim
        const response = await fetch(
          `${HELCIM_API_BASE}/subscriptions/${member.helcimSubscriptionId}`,
          {
            method: "PATCH",
            headers: {
              accept: "application/json",
              "content-type": "application/json",
              "api-token": helcimToken,
            },
            body: JSON.stringify({
              paymentPlanId: parseInt(newPlanId),
            }),
          },
        );

        if (!response.ok) {
          const errorText = await response.text();
          console.error(
            "Failed to update Helcim subscription:",
            response.status,
            errorText,
          );
          throw new Error('Subscription update failed');
        }

        const subscriptionData = await response.json();

        // Update member record
        await Member.findByIdAndUpdate(
          member._id,
          { $set: { contributionTier: newTier } },
          { runValidators: false }
        );

        return {
          success: true,
          message: "Successfully updated contribution level",
          subscription: subscriptionData,
        };
      } catch (error) {
        console.error("Error updating Helcim subscription:", error);
        throw createError({
          statusCode: 500,
          statusMessage: "Subscription update failed",
        });
      }
    }

    // Case 4: Moving between free tiers (shouldn't happen but handle it)
    await Member.findByIdAndUpdate(
      member._id,
      { $set: { contributionTier: newTier } },
      { runValidators: false }
    );

    return {
      success: true,
      message: "Successfully updated contribution level",
    };
  } catch (error) {
    if (error.statusCode) throw error;
    console.error("Error updating contribution tier:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "An unexpected error occurred",
    });
  }
});
