
CREATE TABLE public.board_resources (
  id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
  association_id UUID REFERENCES public.associations(id) ON DELETE CASCADE NOT NULL,
  title TEXT NOT NULL,
  category TEXT NOT NULL DEFAULT 'General',
  resource_type TEXT NOT NULL DEFAULT 'link',
  url TEXT,
  file_url TEXT,
  description TEXT,
  sort_order INTEGER DEFAULT 0,
  is_active BOOLEAN NOT NULL DEFAULT true,
  created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
  updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()
);

ALTER TABLE public.board_resources ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Authenticated users can view active board resources"
ON public.board_resources FOR SELECT TO authenticated
USING (is_active = true);

CREATE POLICY "Admins and managers can insert board resources"
ON public.board_resources FOR INSERT TO authenticated
WITH CHECK (
  public.has_role(auth.uid(), 'admin') OR public.has_role(auth.uid(), 'manager')
);

CREATE POLICY "Admins and managers can update board resources"
ON public.board_resources FOR UPDATE TO authenticated
USING (
  public.has_role(auth.uid(), 'admin') OR public.has_role(auth.uid(), 'manager')
);

CREATE POLICY "Admins and managers can delete board resources"
ON public.board_resources FOR DELETE TO authenticated
USING (
  public.has_role(auth.uid(), 'admin') OR public.has_role(auth.uid(), 'manager')
);

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