CREATE TABLE public.users (
    id integer NOT NULL,
    username varchar(255) NOT NULL,
    email varchar(255) NOT NULL,
    created_at timestamptz NOT NULL,
    profile_id integer
);

CREATE TABLE public.profiles (
    id integer NOT NULL,
    bio text,
    created_at timestamptz NOT NULL
);

CREATE TABLE public.posts (
    id integer NOT NULL,
    user_id integer NOT NULL,
    title varchar(255) NOT NULL,
    body text,
    published_at timestamptz,
    view_count integer DEFAULT 0
);

ALTER TABLE public.users ADD PRIMARY KEY (id);
ALTER TABLE public.profiles ADD PRIMARY KEY (id);
ALTER TABLE public.posts ADD PRIMARY KEY (id);

CREATE UNIQUE INDEX posts_user_id_title_idx ON public.posts (user_id, title);

ALTER TABLE public.users ADD CONSTRAINT users_profile_id_fkey FOREIGN KEY (profile_id) REFERENCES public.profiles (id);
