-- This script was generated by the ERD tool in pgAdmin 4.
-- Please log an issue at https://github.com/pgadmin-org/pgadmin4/issues/new/choose if you find any bugs, including reproduction steps.
BEGIN;

ALTER TABLE IF EXISTS public."Email" DROP CONSTRAINT IF EXISTS email_avatar_fkey;
ALTER TABLE IF EXISTS public."EmailDisplayName" DROP CONSTRAINT IF EXISTS email_id_fk;
ALTER TABLE IF EXISTS public."Message" DROP CONSTRAINT IF EXISTS message_to_fkey;
ALTER TABLE IF EXISTS public."Message" DROP CONSTRAINT IF EXISTS message_cc_fkey;
ALTER TABLE IF EXISTS public."Message" DROP CONSTRAINT IF EXISTS message_from_fkey;
ALTER TABLE IF EXISTS public."Message" DROP CONSTRAINT IF EXISTS message_header_fkey;
ALTER TABLE IF EXISTS public."Message" DROP CONSTRAINT IF EXISTS message_file_fkey;
ALTER TABLE IF EXISTS public."Message" DROP CONSTRAINT IF EXISTS message_body_id_fkey;
ALTER TABLE IF EXISTS public."Message" DROP CONSTRAINT IF EXISTS message_thread_fkey;
ALTER TABLE IF EXISTS public."Message" DROP CONSTRAINT IF EXISTS message_tag_fkey;

CREATE TABLE IF NOT EXISTS public."Email"
(
    id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
    address text NOT NULL,
    avatar_id integer,
    PRIMARY KEY (id),
    CONSTRAINT avatar_id UNIQUE (avatar_id)
);

CREATE TABLE IF NOT EXISTS public."EmailDisplayName"
(
    id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
    email_id integer NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS public."Message"
(
    id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
    subject text,
    "from" integer,
    "to" integer,
    cc integer,
    header_id integer,
    hash text NOT NULL,
    file_id integer NOT NULL,
    date timestamp with time zone NOT NULL,
    unread boolean NOT NULL,
    body_id integer NOT NULL,
    thread_id integer NOT NULL,
    tag_id integer,
    CONSTRAINT body_id UNIQUE (body_id)
);

CREATE TABLE IF NOT EXISTS public."Header"
(
    id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
    key text NOT NULL,
    value text NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS public."File"
(
    id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
    path text NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS public."Avatar"
(
    id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
    url text NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS public."Body"
(
    id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
    text text NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS public."Thread"
(
    id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
    PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS public."Tag"
(
    id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
    name text NOT NULL,
    display text,
    fg_color integer,
    bg_color integer,
    PRIMARY KEY (id)
);

ALTER TABLE IF EXISTS public."Email"
    ADD CONSTRAINT email_avatar_fkey FOREIGN KEY (avatar_id)
    REFERENCES public."Avatar" (id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION
    NOT VALID;


ALTER TABLE IF EXISTS public."EmailDisplayName"
    ADD CONSTRAINT email_id_fk FOREIGN KEY (email_id)
    REFERENCES public."Email" (id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION
    NOT VALID;


ALTER TABLE IF EXISTS public."Message"
    ADD CONSTRAINT message_to_fkey FOREIGN KEY ("to")
    REFERENCES public."Email" (id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION
    NOT VALID;


ALTER TABLE IF EXISTS public."Message"
    ADD CONSTRAINT message_cc_fkey FOREIGN KEY (cc)
    REFERENCES public."Email" (id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION
    NOT VALID;


ALTER TABLE IF EXISTS public."Message"
    ADD CONSTRAINT message_from_fkey FOREIGN KEY ("from")
    REFERENCES public."Email" (id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION
    NOT VALID;


ALTER TABLE IF EXISTS public."Message"
    ADD CONSTRAINT message_header_fkey FOREIGN KEY (header_id)
    REFERENCES public."Header" (id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION
    NOT VALID;


ALTER TABLE IF EXISTS public."Message"
    ADD CONSTRAINT message_file_fkey FOREIGN KEY (file_id)
    REFERENCES public."File" (id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION
    NOT VALID;


ALTER TABLE IF EXISTS public."Message"
    ADD CONSTRAINT message_body_id_fkey FOREIGN KEY (body_id)
    REFERENCES public."Body" (id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION
    NOT VALID;


ALTER TABLE IF EXISTS public."Message"
    ADD CONSTRAINT message_thread_fkey FOREIGN KEY (thread_id)
    REFERENCES public."Thread" (id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION
    NOT VALID;


ALTER TABLE IF EXISTS public."Message"
    ADD CONSTRAINT message_tag_fkey FOREIGN KEY (tag_id)
    REFERENCES public."Tag" (id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION
    NOT VALID;

END;
