CREATE TYPE public.item_color AS ENUM ('red', 'green', 'blue');

CREATE TABLE public.items (
    id bigint NOT NULL,
    name text NOT NULL,
    color public.item_color NOT NULL
);

CREATE TABLE public.orders (
    id bigint NOT NULL,
    item_id bigint NOT NULL REFERENCES public.items(id)
);

CREATE FUNCTION public.get_color(p bigint) RETURNS public.item_color LANGUAGE sql AS $$ SELECT color FROM public.items WHERE id = p $$;

CREATE TRIGGER trg_items_audit
    AFTER INSERT OR UPDATE ON public.items
    FOR EACH ROW EXECUTE FUNCTION public.get_color();

CREATE POLICY pol_items_user
    ON public.items
    USING (id > 0);

CREATE INDEX idx_items_name ON public.items (name);
