
CREATE TABLE public.migration_field_mappings (
  id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
  mapping_type TEXT NOT NULL CHECK (mapping_type IN ('table', 'column', 'id_value')),
  source_table TEXT,
  destination_table TEXT,
  source_field TEXT,
  destination_field TEXT,
  source_value TEXT,
  destination_value TEXT,
  description TEXT,
  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(),
  created_by UUID REFERENCES auth.users(id)
);

ALTER TABLE public.migration_field_mappings ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Admins can manage migration mappings"
ON public.migration_field_mappings
FOR ALL
TO authenticated
USING (public.has_role(auth.uid(), 'admin'))
WITH CHECK (public.has_role(auth.uid(), 'admin'));
