"use server";

import { getServerSession } from "next-auth";
import { revalidatePath } from "next/cache";
import { authOptions } from "@/lib/auth";
import { isAdminEmail } from "@/lib/admin";
import { payoutService } from "@/server/services/payout.service";
import { payoutRepo } from "@/server/repositories/payout.repo";
import { auditLog } from "@/server/services/audit-log.service";
import { emailService } from "@/lib/email/send";
import { payoutMarkPaidSchema } from "./schemas";

async function requireAdmin() {
  const session = await getServerSession(authOptions);
  if (!session?.user || !isAdminEmail(session.user.email)) {
    return null;
  }
  return session.user;
}

export async function markPayoutPaidAction(formData: FormData) {
  const admin = await requireAdmin();
  if (!admin) return { error: "Tidak memiliki akses admin" };

  const parsed = payoutMarkPaidSchema.safeParse({
    payoutId: formData.get("payoutId") as string,
    adminNote: (formData.get("adminNote") as string) ?? "",
  });
  if (!parsed.success) {
    return { error: parsed.error.issues[0].message };
  }

  try {
    await payoutService.markPaid({
      payoutId: parsed.data.payoutId,
      adminId: admin.id,
      adminNote: parsed.data.adminNote,
    });
    void notifyPayoutPaid(parsed.data.payoutId, parsed.data.adminNote);
    await auditLog.record({
      admin: { id: admin.id, email: admin.email },
      action: "PAYOUT_MARK_PAID",
      entityType: "Payout",
      entityId: parsed.data.payoutId,
      payload: { adminNote: parsed.data.adminNote },
    });
    revalidatePath("/admin/payouts");
    revalidatePath("/admin");
    revalidatePath("/profile");
    return { success: true };
  } catch (err) {
    return { error: (err as Error).message };
  }
}

/** E3.7 — kabari organizer kalau payout-nya sudah ditransfer admin. */
async function notifyPayoutPaid(payoutId: string, adminNote: string) {
  const payout = await payoutRepo.findById(payoutId);
  if (!payout) return;
  await emailService.send({
    to: payout.organizer.email,
    idempotencyKey: `payout_paid-${payout.id}`,
    template: {
      template: "payout_paid",
      data: {
        organizerName: payout.organizer.name,
        tripTitle: payout.trip.title,
        amount: payout.amount,
        adminNote,
      },
    },
  });
}
