'use server';

import { redirect } from 'next/navigation';

import { createClient } from '@/utils/supabase/server';
import { medusaLogout } from '@lib/data/customer';

export const signOutAction = async () => {
  const client = await createClient();

  try {
    try {
      await medusaLogout();
    } catch (medusaError) {
      console.warn('Medusa logout failed or not available:', medusaError);
    }

    const { error } = await client.auth.signOut();
    if (error) {
      throw error;
    }
  } catch (error) {
    console.error('Logout error:', error);
    throw error;
  }

  return redirect('/');
};
