-- Flyway Migration: V2.0.0
-- Purpose: Initial schema creation for Gameyfin application.

/******************************************************************************************
 * 1. Sequences (hi/lo allocation size = 50 for performance)
 ******************************************************************************************/
CREATE SEQUENCE COMPANY_SEQ
    INCREMENT BY 50;

CREATE SEQUENCE DIRECTORY_MAPPING_SEQ
    INCREMENT BY 50;

CREATE SEQUENCE GAME_FIELD_METADATA_SEQ
    INCREMENT BY 50;

CREATE SEQUENCE GAME_FIELD_SOURCE_SEQ
    INCREMENT BY 50;

CREATE SEQUENCE GAME_SEQ
    INCREMENT BY 50;

CREATE SEQUENCE IMAGE_SEQ
    INCREMENT BY 50;

CREATE SEQUENCE LIBRARY_SEQ
    INCREMENT BY 50;

CREATE SEQUENCE USERS_SEQ
    INCREMENT BY 50;

/******************************************************************************************
 * 2. Tables
 ******************************************************************************************/
CREATE TABLE APP_CONFIG
(
    "key"   CHARACTER VARYING(255) NOT NULL
        PRIMARY KEY,
    "value" CHARACTER VARYING(255)
);

CREATE TABLE COMPANY
(
    ID   BIGINT NOT NULL
        PRIMARY KEY,
    NAME CHARACTER VARYING(255),
    TYPE TINYINT,
    CONSTRAINT UK4UCNYHR8I0URHWDUDFAHKOB9E
        UNIQUE (NAME, TYPE),
    CHECK ("TYPE" BETWEEN 0 AND 1)
);

CREATE TABLE DIRECTORY_MAPPING
(
    ID            BIGINT NOT NULL
        PRIMARY KEY,
    EXTERNAL_PATH CHARACTER VARYING(255),
    INTERNAL_PATH CHARACTER VARYING(255)
        CONSTRAINT UKJ3GSATFAHEWFOLSEAJ29O3KYT
            UNIQUE
);

CREATE TABLE IMAGE
(
    ID             BIGINT NOT NULL
        PRIMARY KEY,
    CONTENT_ID     CHARACTER VARYING(255),
    CONTENT_LENGTH BIGINT,
    MIME_TYPE      CHARACTER VARYING(255),
    ORIGINAL_URL   CHARACTER VARYING(255),
    TYPE           TINYINT,
    CHECK ("TYPE" BETWEEN 0 AND 3)
);

CREATE TABLE LIBRARY
(
    ID         BIGINT                   NOT NULL
        PRIMARY KEY,
    CREATED_AT TIMESTAMP WITH TIME ZONE NOT NULL,
    NAME       CHARACTER VARYING(255),
    UPDATED_AT TIMESTAMP WITH TIME ZONE NOT NULL
);

CREATE TABLE GAME
(
    ID              BIGINT                   NOT NULL
        PRIMARY KEY,
    COMMENT         CHARACTER LARGE OBJECT,
    CREATED_AT      TIMESTAMP WITH TIME ZONE NOT NULL,
    CRITIC_RATING   INTEGER,
    DOWNLOAD_COUNT  INTEGER,
    FILE_SIZE       BIGINT,
    MATCH_CONFIRMED BOOLEAN,
    PATH            CHARACTER VARYING(255)
        CONSTRAINT UK4WXN9FPXFQ8QXPSB7FY0O3NOA
            UNIQUE,
    RELEASE         TIMESTAMP WITH TIME ZONE,
    SUMMARY         CHARACTER LARGE OBJECT,
    TITLE           CHARACTER VARYING(255),
    UPDATED_AT      TIMESTAMP WITH TIME ZONE NOT NULL,
    USER_RATING     INTEGER,
    COVER_IMAGE_ID  BIGINT
        CONSTRAINT UK52RQ62FLPBNTI77BYKM7UAHKQ
            UNIQUE,
    HEADER_IMAGE_ID BIGINT
        CONSTRAINT UK30B16LLQV54H40XIOGP7T9P35
            UNIQUE,
    LIBRARY_ID      BIGINT,
    CONSTRAINT FK6CVB43REAYSNYPI0XDY6HQTVF
        FOREIGN KEY (COVER_IMAGE_ID) REFERENCES IMAGE,
    CONSTRAINT FK8N86NDPGKMOO7YOLX6HL8N84G
        FOREIGN KEY (HEADER_IMAGE_ID) REFERENCES IMAGE,
    CONSTRAINT FKIUVR8XFB63T1K6T43EYYXVO2C
        FOREIGN KEY (LIBRARY_ID) REFERENCES LIBRARY
);

CREATE TABLE GAME_DEVELOPERS
(
    GAME_ID       BIGINT NOT NULL,
    DEVELOPERS_ID BIGINT NOT NULL,
    CONSTRAINT FKB12PO9L2B9OJBAIHC82MM2QXB
        FOREIGN KEY (DEVELOPERS_ID) REFERENCES COMPANY,
    CONSTRAINT FKS4IJSVPIJ53DSL143XVRGBS09
        FOREIGN KEY (GAME_ID) REFERENCES GAME
);

CREATE TABLE GAME_FEATURES
(
    GAME_ID  BIGINT NOT NULL,
    FEATURES TINYINT,
    CONSTRAINT FK63XLTCT60SCIMPM06K8BHBE4A
        FOREIGN KEY (GAME_ID) REFERENCES GAME,
    CHECK ("FEATURES" BETWEEN 0 AND 23)
);

CREATE TABLE GAME_GENRES
(
    GAME_ID BIGINT NOT NULL,
    GENRES  TINYINT,
    CONSTRAINT FKDTSX09YOPD98E0LUEWRUSJD9E
        FOREIGN KEY (GAME_ID) REFERENCES GAME,
    CHECK ("GENRES" BETWEEN 0 AND 25)
);

CREATE TABLE GAME_IMAGES
(
    GAME_ID   BIGINT NOT NULL,
    IMAGES_ID BIGINT NOT NULL
        CONSTRAINT UKBDE7M3TKHIEEYBINM2ED0B6X1
            UNIQUE,
    CONSTRAINT FK5YWV1DMXCM2VSQUEB7RHQ3JK9
        FOREIGN KEY (IMAGES_ID) REFERENCES IMAGE,
    CONSTRAINT FKOWCPUCV45OX8GT28TXGVHF1AA
        FOREIGN KEY (GAME_ID) REFERENCES GAME
);

CREATE TABLE GAME_KEYWORDS
(
    GAME_ID  BIGINT NOT NULL,
    KEYWORDS CHARACTER VARYING(255),
    CONSTRAINT FKMVF6HNJ7ROMQQM2EX70A9NVAC
        FOREIGN KEY (GAME_ID) REFERENCES GAME
);

CREATE TABLE GAME_PERSPECTIVES
(
    GAME_ID      BIGINT NOT NULL,
    PERSPECTIVES TINYINT,
    CONSTRAINT FKHUEENG29Y1GHBRDI5QHGUXH6E
        FOREIGN KEY (GAME_ID) REFERENCES GAME,
    CHECK ("PERSPECTIVES" BETWEEN 0 AND 7)
);

CREATE TABLE GAME_PUBLISHERS
(
    GAME_ID       BIGINT NOT NULL,
    PUBLISHERS_ID BIGINT NOT NULL,
    CONSTRAINT FK49R2KB61LIJ54BQB4VNTST97N
        FOREIGN KEY (GAME_ID) REFERENCES GAME,
    CONSTRAINT FKNGLD5ESGRBRH95J5BJF0HEF85
        FOREIGN KEY (PUBLISHERS_ID) REFERENCES COMPANY
);

CREATE TABLE GAME_THEMES
(
    GAME_ID BIGINT NOT NULL,
    THEMES  TINYINT,
    CONSTRAINT FKRV351JXLIOY0A17Y5BBJJ6FW4
        FOREIGN KEY (GAME_ID) REFERENCES GAME,
    CHECK ("THEMES" BETWEEN 0 AND 22)
);

CREATE TABLE GAME_VIDEO_URLS
(
    GAME_ID    BIGINT NOT NULL,
    VIDEO_URLS BINARY VARYING(255),
    CONSTRAINT FKJKKWO8WDS086AS7B2KSLSVKM6
        FOREIGN KEY (GAME_ID) REFERENCES GAME
);

CREATE TABLE LIBRARY_DIRECTORIES
(
    LIBRARY_ID     BIGINT NOT NULL,
    DIRECTORIES_ID BIGINT NOT NULL
        CONSTRAINT UKB5UM4CADBNC6UC8DVOMO81N5F
            UNIQUE,
    CONSTRAINT FKFNCKIU58I9L89MLXV388DY13B
        FOREIGN KEY (LIBRARY_ID) REFERENCES LIBRARY,
    CONSTRAINT FKJDXS58Q1IRTU0IDP6DXJHWAPM
        FOREIGN KEY (DIRECTORIES_ID) REFERENCES DIRECTORY_MAPPING
);

CREATE TABLE LIBRARY_GAMES
(
    LIBRARY_ID BIGINT NOT NULL,
    GAMES_ID   BIGINT NOT NULL
        CONSTRAINT UK3E4VB9NQXPY27VMTA27GU5FY8
            UNIQUE,
    CONSTRAINT FK6C71EEDM0I2N1JXDE9BOBWG5M
        FOREIGN KEY (LIBRARY_ID) REFERENCES LIBRARY,
    CONSTRAINT FKDKKKES3DAY0WJ1QMV42KMMFDK
        FOREIGN KEY (GAMES_ID) REFERENCES GAME
);

CREATE TABLE LIBRARY_UNMATCHED_PATHS
(
    LIBRARY_ID      BIGINT NOT NULL,
    UNMATCHED_PATHS CHARACTER VARYING(255),
    CONSTRAINT FKSJ51WC2LBNNXY0LKLWELI6VSB
        FOREIGN KEY (LIBRARY_ID) REFERENCES LIBRARY
);

CREATE TABLE PLUGIN_CONFIG
(
    "key"     CHARACTER VARYING(255) NOT NULL,
    PLUGIN_ID CHARACTER VARYING(255) NOT NULL,
    "value"   CHARACTER VARYING(255),
    PRIMARY KEY ("key", PLUGIN_ID)
);

CREATE TABLE PLUGIN_MANAGEMENT_ENTRY
(
    PLUGIN_ID   CHARACTER VARYING(255) NOT NULL
        PRIMARY KEY,
    ENABLED     BOOLEAN                NOT NULL,
    PRIORITY    INTEGER                NOT NULL,
    TRUST_LEVEL TINYINT,
    CHECK ("TRUST_LEVEL" BETWEEN 0 AND 4)
);

CREATE TABLE GAME_ORIGINAL_IDS
(
    GAME_ID          BIGINT                 NOT NULL,
    ORIGINAL_IDS     CHARACTER VARYING(255),
    ORIGINAL_IDS_KEY CHARACTER VARYING(255) NOT NULL,
    PRIMARY KEY (GAME_ID, ORIGINAL_IDS_KEY),
    CONSTRAINT FK1CSD5QD7VJT7BTTA3G7HGYBUX
        FOREIGN KEY (GAME_ID) REFERENCES GAME,
    CONSTRAINT FKMT0XWLPWPU9NP0Q289JBAHJRY
        FOREIGN KEY (ORIGINAL_IDS_KEY) REFERENCES PLUGIN_MANAGEMENT_ENTRY
);

CREATE TABLE USERS
(
    ID               BIGINT  NOT NULL
        PRIMARY KEY,
    EMAIL            CHARACTER VARYING(255)
        CONSTRAINT UK6DOTKOTT2KJSP8VW4D0M25FB7
            UNIQUE,
    EMAIL_CONFIRMED  BOOLEAN NOT NULL,
    ENABLED          BOOLEAN NOT NULL,
    OIDC_PROVIDER_ID CHARACTER VARYING(255),
    PASSWORD         CHARACTER VARYING(255),
    USERNAME         CHARACTER VARYING(255)
        CONSTRAINT UKR43AF9AP4EDM43MMTQ01ODDJ6
            UNIQUE,
    AVATAR_ID        BIGINT
        CONSTRAINT UKRSULCN2GYNJY3CDDPWMOSV881
            UNIQUE,
    CONSTRAINT FK19LFLPG5SEIS4DWRM2LVJLXFV
        FOREIGN KEY (AVATAR_ID) REFERENCES IMAGE
);

CREATE TABLE GAME_FIELD_SOURCE
(
    DTYPE            CHARACTER VARYING(31) NOT NULL,
    ID               BIGINT                NOT NULL
        PRIMARY KEY,
    PLUGIN_PLUGIN_ID CHARACTER VARYING(255),
    USER_ID          BIGINT,
    CONSTRAINT FKNJC4QSS5APFHTPWP42OAEAL5G
        FOREIGN KEY (PLUGIN_PLUGIN_ID) REFERENCES PLUGIN_MANAGEMENT_ENTRY,
    CONSTRAINT FKSR1BGTX5XJVMAL7FEFGL982TP
        FOREIGN KEY (USER_ID) REFERENCES USERS
);

CREATE TABLE GAME_FIELD_METADATA
(
    ID         BIGINT NOT NULL
        PRIMARY KEY,
    UPDATED_AT TIMESTAMP WITH TIME ZONE,
    SOURCE_ID  BIGINT
        CONSTRAINT UKHW6U2Y9FLWPTI57QB7K0P27BL
            UNIQUE,
    CONSTRAINT FKQ4RC409TP8FUBTTM733PMJD8F
        FOREIGN KEY (SOURCE_ID) REFERENCES GAME_FIELD_SOURCE
);

CREATE TABLE GAME_FIELDS
(
    GAME_ID    BIGINT                 NOT NULL,
    FIELDS_ID  BIGINT                 NOT NULL
        CONSTRAINT UK1L5OAH0UOOUV4V5A9P0PAK77X
            UNIQUE,
    FIELDS_KEY CHARACTER VARYING(255) NOT NULL,
    PRIMARY KEY (GAME_ID, FIELDS_KEY),
    CONSTRAINT FKLNEPI7YWCI86YH21KO9WD9PYF
        FOREIGN KEY (GAME_ID) REFERENCES GAME,
    CONSTRAINT FKT8FLOFDAPX5M746S5LW54C5B3
        FOREIGN KEY (FIELDS_ID) REFERENCES GAME_FIELD_METADATA
);

CREATE TABLE TOKEN
(
    SECRET     CHARACTER VARYING(255) NOT NULL
        PRIMARY KEY,
    CREATED_ON TIMESTAMP WITH TIME ZONE,
    PAYLOAD    CHARACTER VARYING(255),
    TYPE       CHARACTER VARYING(255),
    CREATOR_ID BIGINT,
    CONSTRAINT FKGHOIALAPTI5JFEJ506JBB1O8Y
        FOREIGN KEY (CREATOR_ID) REFERENCES USERS
            ON DELETE CASCADE
);

CREATE TABLE USER_PREFERENCE
(
    "key"   CHARACTER VARYING(255) NOT NULL,
    USER_ID BIGINT                 NOT NULL,
    "value" CHARACTER VARYING(255),
    PRIMARY KEY ("key", USER_ID)
);

CREATE TABLE USER_ROLES
(
    USER_ID BIGINT NOT NULL,
    ROLES   ENUM ('ADMIN', 'SUPERADMIN', 'USER'),
    CONSTRAINT FKHFH9DX7W3UBF1CO1VDEV94G3F
        FOREIGN KEY (USER_ID) REFERENCES USERS
);

CREATE TABLE JOB_RUN_RESULT
(
    ID          BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,
    JOB_NAME    VARCHAR(255),
    STARTED_AT  TIMESTAMP,
    FINISHED_AT TIMESTAMP,
    STATUS      VARCHAR(255),
    MESSAGE     VARCHAR(255)
);