CREATE TABLE public.investment_profits (
  id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
  user_id UUID NOT NULL,
  investor_name TEXT NOT NULL,
  amount NUMERIC NOT NULL CHECK (amount >= 0),
  taken_at DATE NOT NULL DEFAULT CURRENT_DATE,
  note TEXT,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

GRANT SELECT, INSERT, UPDATE, DELETE ON public.investment_profits TO authenticated;
GRANT ALL ON public.investment_profits TO service_role;

ALTER TABLE public.investment_profits ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Users manage own investment_profits"
  ON public.investment_profits FOR ALL
  USING (auth.uid() = user_id) WITH CHECK (auth.uid() = user_id);

CREATE TRIGGER touch_investment_profits BEFORE UPDATE ON public.investment_profits
  FOR EACH ROW EXECUTE FUNCTION public.touch_updated_at();