CREATE TABLE "user" (
	"id" UUID NOT NULL UNIQUE,
	"mail" TEXT NOT NULL UNIQUE CHECK( (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]) 1 (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])),
	"active" BOOLEAN NOT NULL DEFAULT true,
	PRIMARY KEY("id")
);

CREATE TABLE "feed" (
	"id" UUID NOT NULL UNIQUE,
	"name" TEXT,
	"url" TEXT NOT NULL,
	"public" BOOLEAN NOT NULL DEFAULT false,
	PRIMARY KEY("id")
);

CREATE TABLE "articles" (
	"id" UUID NOT NULL UNIQUE,
	"title" TEXT NOT NULL,
	"description" TEXT,
	"site_url" TEXT NOT NULL,
	"image_url" TEXT,
	"publish_date" TIMESTAMPTZ NOT NULL,
	"feed_id" UUID NOT NULL,
	PRIMARY KEY("id")
);

CREATE TABLE "user_to_feed" (
	"id" INTEGER NOT NULL UNIQUE GENERATED BY DEFAULT AS IDENTITY,
	"user_id" UUID NOT NULL,
	"feed_id" UUID NOT NULL,
	"owner" BOOLEAN NOT NULL DEFAULT False,
	PRIMARY KEY("id")
);

ALTER TABLE "user_to_feed"
ADD FOREIGN KEY("user_id") REFERENCES "user"("id")
ON UPDATE NO ACTION ON DELETE NO ACTION;

ALTER TABLE "user_to_feed"
ADD FOREIGN KEY("feed_id") REFERENCES "feed"("id")
ON UPDATE NO ACTION ON DELETE NO ACTION;

ALTER TABLE "articles"
ADD FOREIGN KEY("feed_id") REFERENCES "feed"("id")
ON UPDATE NO ACTION ON DELETE NO ACTION;