
CREATE TABLE public.expense_bundles (
  id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
  name TEXT NOT NULL,
  description TEXT,
  client_id UUID REFERENCES public.associations(id) ON DELETE CASCADE NOT NULL,
  created_by UUID,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE public.bundle_expenses (
  id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
  bundle_id UUID REFERENCES public.expense_bundles(id) ON DELETE CASCADE NOT NULL,
  expense_id UUID REFERENCES public.billable_expenses(id) ON DELETE CASCADE NOT NULL,
  added_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  UNIQUE(bundle_id, expense_id)
);

ALTER TABLE public.expense_bundles ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.bundle_expenses ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Authenticated users can manage expense_bundles" ON public.expense_bundles FOR ALL TO authenticated USING (true) WITH CHECK (true);
CREATE POLICY "Authenticated users can manage bundle_expenses" ON public.bundle_expenses FOR ALL TO authenticated USING (true) WITH CHECK (true);
