
CREATE TABLE public.vendor_coa_mappings (
  id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
  association_id UUID NOT NULL REFERENCES public.associations(id) ON DELETE CASCADE,
  vendor_id UUID NOT NULL REFERENCES public.vendors(id) ON DELETE CASCADE,
  chart_of_accounts_id UUID NOT NULL REFERENCES public.chart_of_accounts(id) ON DELETE CASCADE,
  notes TEXT,
  created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
  updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
  UNIQUE(association_id, vendor_id)
);

ALTER TABLE public.vendor_coa_mappings ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Authenticated users can manage vendor COA mappings"
ON public.vendor_coa_mappings FOR ALL TO authenticated USING (true) WITH CHECK (true);

CREATE TRIGGER update_vendor_coa_mappings_updated_at
BEFORE UPDATE ON public.vendor_coa_mappings
FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();
