-- Create customers table CREATE TABLE public.customers ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), email TEXT NOT NULL UNIQUE, name TEXT, phone TEXT, company TEXT, total_orders INTEGER DEFAULT 0, total_spent DECIMAL(10,2) DEFAULT 0, created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now() ); -- Create payments table CREATE TABLE public.payments ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), order_id UUID REFERENCES public.orders(id) ON DELETE CASCADE, amount DECIMAL(10,2) NOT NULL, currency TEXT NOT NULL DEFAULT 'SEK', status TEXT NOT NULL DEFAULT 'pending', payment_method TEXT, shopify_payment_id TEXT, created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now() ); -- Add customer reference to orders ALTER TABLE public.orders ADD COLUMN customer_id UUID REFERENCES public.customers(id); -- Enable RLS ALTER TABLE public.customers ENABLE ROW LEVEL SECURITY; ALTER TABLE public.payments ENABLE ROW LEVEL SECURITY; -- RLS policies for customers (only authenticated admins) CREATE POLICY "Authenticated users can view customers" ON public.customers FOR SELECT TO authenticated USING (true); CREATE POLICY "Authenticated users can manage customers" ON public.customers FOR ALL TO authenticated USING (true) WITH CHECK (true); -- RLS policies for payments CREATE POLICY "Authenticated users can view payments" ON public.payments FOR SELECT TO authenticated USING (true); CREATE POLICY "Service can insert payments" ON public.payments FOR INSERT WITH CHECK (true); -- Triggers for updated_at CREATE TRIGGER update_customers_updated_at BEFORE UPDATE ON public.customers FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();