"use server"; import { DATABASE_ID, ID, TABLES, tablesDB } from "@/lib/appwrite-rest"; export type ContactFormState = { ok: boolean; message: string; errors?: Record; }; const initial: ContactFormState = { ok: false, message: "" }; export async function submitContact( _prev: ContactFormState = initial, formData: FormData, ): Promise { const name = String(formData.get("name") ?? "").trim(); const email = String(formData.get("email") ?? "").trim(); const phone = String(formData.get("phone") ?? "").trim(); const subject = String(formData.get("subject") ?? "").trim(); const message = String(formData.get("message") ?? "").trim(); const source = String(formData.get("source") ?? "").trim(); const errors: Record = {}; if (!name) errors.name = "Ad zorunlu"; // E-posta YA DA telefon biri yeterli (quick lead form için) if (!email && !phone) { errors.email = "E-posta veya telefon zorunlu"; } else if (email && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) { errors.email = "Geçerli bir e-posta girin"; } // Tam form değilse message zorunlu değil const isQuickLead = source.startsWith("quick"); if (!isQuickLead && (!message || message.length < 10)) { errors.message = "Mesaj en az 10 karakter olmalı"; } if (Object.keys(errors).length > 0) { return { ok: false, message: "Lütfen form alanlarını kontrol edin", errors }; } try { await tablesDB.createRow(DATABASE_ID, TABLES.contactMessages, ID.unique(), { name, email: email || `${phone}@telefon.tr`, // email zorunlu — telefon-only ise placeholder phone: phone || null, subject: subject || (isQuickLead ? `Hızlı talep — ${source}` : null), message: message || (isQuickLead ? `Hızlı lead: ${name} — ${phone || email}. Geri arama bekleniyor.` : ""), status: "new", }); return { ok: true, message: isQuickLead ? "Talebiniz alındı. En kısa sürede sizi arayacağız." : "Mesajınız iletildi. En kısa sürede dönüş yapacağız.", }; } catch (err) { const detail = err instanceof Error ? err.message : "Bilinmeyen hata"; return { ok: false, message: `Kayıt başarısız: ${detail}` }; } }