CREATE TYPE post_state AS ENUM (
    'draft',
    'published'
);

CREATE TYPE target_role AS ENUM (
    'production'
);

CREATE TYPE target_type AS ENUM (
    'netlify'
);

CREATE TABLE site (
    id    BIGSERIAL NOT NULL PRIMARY KEY,
    name  TEXT      NOT NULL UNIQUE,
    title TEXT      NOT NULL,
    url   TEXT      NOT NULL,
    theme TEXT      NOT NULL,
    props JSON      NOT NULL
);

CREATE TABLE post (
    id         BIGSERIAL  NOT NULL PRIMARY KEY,
    site_id    BIGINT     NOT NULL,
    title      TEXT,
    body       TEXT       NOT NULL,
    state      post_state NOT NULL,
    props      JSON       NOT NULL,
    post_date  TIMESTAMP WITH TIME ZONE,
    created_at TIMESTAMP  NOT NULL,

    FOREIGN KEY (site_id) REFERENCES site (id)
);

CREATE TABLE publish_target (
    id          BIGSERIAL   NOT NULL PRIMARY KEY,
    site_id     BIGINT      NOT NULL,
    role        target_role NOT NULL,
    target_type target_type NOT NULL,
    url         TEXT        NOT NULL,
    target_ref  TEXT        NOT NULL,

    FOREIGN KEY (site_id) REFERENCES site (id)
);