-- Create trigger function
CREATE OR REPLACE FUNCTION update_updated_at()
RETURNS TRIGGER AS $$
BEGIN
  NEW."updatedAt" = NOW();
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- Apply to all tables with updatedAt
CREATE TRIGGER update_reservation_timestamp BEFORE UPDATE ON "Reservation" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_recurring_booking_timestamp BEFORE UPDATE ON "RecurringBooking" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_waiting_list_timestamp BEFORE UPDATE ON "WaitingList" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_customer_timestamp BEFORE UPDATE ON "Customer" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_customer_loyalty_timestamp BEFORE UPDATE ON "CustomerLoyalty" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_customer_offer_timestamp BEFORE UPDATE ON "CustomerOffer" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_customer_segment_timestamp BEFORE UPDATE ON "CustomerSegment" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_service_timestamp BEFORE UPDATE ON "Service" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_service_pricing_timestamp BEFORE UPDATE ON "ServicePricing" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_service_package_timestamp BEFORE UPDATE ON "ServicePackage" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_package_purchase_timestamp BEFORE UPDATE ON "PackagePurchase" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_contact_message_timestamp BEFORE UPDATE ON "ContactMessage" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_newsletter_timestamp BEFORE UPDATE ON "Newsletter" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_campaign_timestamp BEFORE UPDATE ON "Campaign" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_newsletter_subscription_timestamp BEFORE UPDATE ON "NewsletterSubscription" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_push_subscription_timestamp BEFORE UPDATE ON "PushSubscription" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_notification_template_timestamp BEFORE UPDATE ON "NotificationTemplate" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_notification_timestamp BEFORE UPDATE ON "Notification" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_marketing_campaign_timestamp BEFORE UPDATE ON "MarketingCampaign" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_admin_timestamp BEFORE UPDATE ON "Admin" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_business_settings_timestamp BEFORE UPDATE ON "BusinessSettings" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_business_metrics_timestamp BEFORE UPDATE ON "BusinessMetrics" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_review_timestamp BEFORE UPDATE ON "Review" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_user_timestamp BEFORE UPDATE ON "user" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_session_timestamp BEFORE UPDATE ON "session" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_account_timestamp BEFORE UPDATE ON "account" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_verification_timestamp BEFORE UPDATE ON "verification" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_two_factor_timestamp BEFORE UPDATE ON "twoFactor" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
CREATE TRIGGER update_user_device_timestamp BEFORE UPDATE ON "userDevice" FOR EACH ROW EXECUTE FUNCTION update_updated_at();
