import { NextRequest, NextResponse } from 'next/server'; import { query } from '@/lib/db'; export async function GET(req: NextRequest) { const { searchParams } = new URL(req.url); const slug = searchParams.get('slug'); if (!slug) { return NextResponse.json({ error: 'Missing tenant slug' }, { status: 400 }); } try { const res = await query(` SELECT t.id, t.name, t.slug, t.business_type, t.phone, t.email, t.address, t.city, t.state, t.zip_code, t.description, t.logo_url, t.cover_url, t.settings, t.subscription_status, t.is_active, COALESCE(p.name, 'Starter') as plan_name FROM tenants t LEFT JOIN plans p ON t.plan_id = p.id WHERE t.slug = $1 `, [slug]); if (res.rows.length === 0) { return NextResponse.json({ error: 'Tenant not found' }, { status: 404 }); } const tenant = res.rows[0]; const settings = tenant.settings || {}; return NextResponse.json({ id: tenant.id, name: tenant.name, slug: tenant.slug, businessType: tenant.business_type, phone: tenant.phone || '', email: tenant.email || '', address: tenant.address || '', city: tenant.city || '', state: tenant.state || '', zipCode: tenant.zip_code || '', description: tenant.description || '', logoUrl: tenant.logo_url || '', coverUrl: tenant.cover_url || '', planName: tenant.plan_name, subscriptionStatus: tenant.subscription_status, // Settings JSONB themeColor: settings.theme_color || '#6C63FF', workingDays: settings.working_days || [1, 2, 3, 4, 5, 6], startTime: settings.start_time || '09:00', endTime: settings.end_time || '19:00', interval: settings.interval || 30, allowOnlineBooking: settings.allow_online_booking !== false, requireConfirmation: settings.require_confirmation || false, sendReminders: settings.send_reminders !== false, reminderMinutesBefore: settings.reminder_minutes_before || 60, allowCancellation: settings.allow_cancellation !== false, bookingAdvanceMin: settings.booking_advance_min || 60, bookingAdvanceMaxDays: settings.booking_advance_max_days || 30, cancellationLimitMin: settings.cancellation_limit_min || 120, toleranceMinutes: settings.tolerance_minutes || 15, notifyNewAppointment: settings.notify_new_appointment !== false, notifyCancellation: settings.notify_cancellation !== false, notifyClientReminder: settings.notify_client_reminder !== false, dailySummary: settings.daily_summary || false, weeklyReport: settings.weekly_report !== false, dailySchedules: settings.daily_schedules || null, profSchedules: settings.prof_schedules || {}, }); } catch (error: any) { console.error('GET Settings error:', error); return NextResponse.json({ error: error.message }, { status: 500 }); } } export async function PUT(req: NextRequest) { try { const body = await req.json(); const { slug } = body; if (!slug) return NextResponse.json({ error: 'Missing slug' }, { status: 400 }); // Build the settings JSONB object const settingsJson = { theme_color: body.themeColor || '#6C63FF', working_days: body.workingDays || [1, 2, 3, 4, 5, 6], start_time: body.startTime || '09:00', end_time: body.endTime || '19:00', interval: body.interval || 30, allow_online_booking: body.allowOnlineBooking !== false, require_confirmation: body.requireConfirmation || false, send_reminders: body.sendReminders !== false, reminder_minutes_before: body.reminderMinutesBefore || 60, allow_cancellation: body.allowCancellation !== false, booking_advance_min: body.bookingAdvanceMin || 60, booking_advance_max_days: body.bookingAdvanceMaxDays || 30, cancellation_limit_min: body.cancellationLimitMin || 120, tolerance_minutes: body.toleranceMinutes || 15, notify_new_appointment: body.notifyNewAppointment !== false, notify_cancellation: body.notifyCancellation !== false, notify_client_reminder: body.notifyClientReminder !== false, daily_summary: body.dailySummary || false, weekly_report: body.weeklyReport !== false, daily_schedules: body.dailySchedules || null, prof_schedules: body.profSchedules || {}, }; const res = await query(` UPDATE tenants SET name = COALESCE($1, name), business_type = COALESCE($2, business_type), phone = $3, email = $4, address = $5, city = $6, state = $7, zip_code = $8, description = $9, settings = $10, updated_at = NOW() WHERE slug = $11 RETURNING id, name, slug `, [ body.name, body.businessType, body.phone || null, body.email || null, body.address || null, body.city || null, body.state || null, body.zipCode || null, body.description || null, JSON.stringify(settingsJson), slug ]); if (res.rows.length === 0) { return NextResponse.json({ error: 'Tenant not found' }, { status: 404 }); } return NextResponse.json({ success: true, tenant: res.rows[0] }); } catch (error: any) { console.error('PUT Settings error:', error); return NextResponse.json({ error: error.message }, { status: 500 }); } }