'use server';

import {
  foldersService,
  CreateFolderData,
  UpdateFolderData,
} from '@/services/folders';
import { revalidatePath } from 'next/cache';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';

/**
 * Récupère tous les dossiers de l'utilisateur
 */
export async function getFolders() {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user?.id) {
      return { success: false, error: 'Non autorisé' };
    }

    const folders = await foldersService.getFolders(session.user.id);
    return { success: true, data: folders };
  } catch (error) {
    console.error('Error fetching folders:', error);
    return {
      success: false,
      error: error instanceof Error ? error.message : 'Erreur inconnue',
    };
  }
}

/**
 * Crée un nouveau dossier
 */
export async function createFolder(data: Omit<CreateFolderData, 'userId'>) {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user?.id) {
      return { success: false, error: 'Non autorisé' };
    }

    const folder = await foldersService.createFolder({
      ...data,
      userId: session.user.id,
    });

    revalidatePath('/notes');
    return { success: true, data: folder };
  } catch (error) {
    console.error('Error creating folder:', error);
    return {
      success: false,
      error: error instanceof Error ? error.message : 'Erreur inconnue',
    };
  }
}

/**
 * Met à jour un dossier
 */
export async function updateFolder(folderId: string, data: UpdateFolderData) {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user?.id) {
      return { success: false, error: 'Non autorisé' };
    }

    const folder = await foldersService.updateFolder(
      folderId,
      session.user.id,
      data
    );

    revalidatePath('/notes');
    return { success: true, data: folder };
  } catch (error) {
    console.error('Error updating folder:', error);
    return {
      success: false,
      error: error instanceof Error ? error.message : 'Erreur inconnue',
    };
  }
}

/**
 * Supprime un dossier
 */
export async function deleteFolder(folderId: string) {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user?.id) {
      return { success: false, error: 'Non autorisé' };
    }

    await foldersService.deleteFolder(folderId, session.user.id);

    revalidatePath('/notes');
    return { success: true };
  } catch (error) {
    console.error('Error deleting folder:', error);
    return {
      success: false,
      error: error instanceof Error ? error.message : 'Erreur inconnue',
    };
  }
}

/**
 * Réorganise l'ordre des dossiers
 */
export async function reorderFolders(
  folderOrders: Array<{ id: string; order: number }>
) {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user?.id) {
      return { success: false, error: 'Non autorisé' };
    }

    await foldersService.reorderFolders(session.user.id, folderOrders);

    revalidatePath('/notes');
    return { success: true };
  } catch (error) {
    console.error('Error reordering folders:', error);
    return {
      success: false,
      error: error instanceof Error ? error.message : 'Erreur inconnue',
    };
  }
}
