"use server";

import { getWorkshopDetailById } from "../services/workshopDetailService";
import { extractDomain } from "../utils/domain";
import type { WorkshopDetailDataWithTranslations } from "../services/workshopDetailService";

interface GetWorkshopDetailActionResponse {
  success: boolean;
  data?: WorkshopDetailDataWithTranslations;
  error?: string;
}

/**
 * Server action to fetch workshop detail by ID
 */
export async function getWorkshopDetailAction(
  workshopId: string,
  language?: string,
  franchiseeId?: string | null
): Promise<GetWorkshopDetailActionResponse> {
  try {
    const domain = await extractDomain();

    const workshopDetail = await getWorkshopDetailById(workshopId, domain, language, franchiseeId);

    if (!workshopDetail || !workshopDetail.default) {
      return {
        success: false,
        error: "Workshop not found",
      };
    }

    return {
      success: true,
      data: workshopDetail,
    };
  } catch (error) {
    console.error("[getWorkshopDetailAction] Error:", error);
    return {
      success: false,
      error: "Failed to fetch workshop details",
    };
  }
}
