CREATE SCHEMA IF NOT EXISTS permission;

-- Permissions
CREATE TABLE permission.permissions
(
    Id          SERIAL PRIMARY KEY,
    Name        VARCHAR(255) NOT NULL,
    Description TEXT         NULL,

    -- for history
    Deleted     BOOLEAN      NOT NULL DEFAULT FALSE,
    EditorId    INT          NULL REFERENCES administration.users (Id),
    CreatedUtc  timestamptz  NOT NULL DEFAULT NOW(),
    UpdatedUtc  timestamptz  NOT NULL DEFAULT NOW(),
    CONSTRAINT UQ_PermissionName UNIQUE (Name)
);

CREATE TABLE permission.permissions_history
(
    LIKE permission.permissions
);

CREATE TRIGGER versioning_trigger
    BEFORE INSERT OR UPDATE OR DELETE
    ON permission.permissions
    FOR EACH ROW
EXECUTE PROCEDURE public.history_trigger_function();

-- Roles
CREATE TABLE permission.roles
(
    Id          SERIAL PRIMARY KEY,
    Name        VARCHAR(255) NOT NULL,
    Description TEXT         NULL,

    -- for history
    Deleted     BOOLEAN      NOT NULL DEFAULT FALSE,
    EditorId    INT          NULL REFERENCES administration.users (Id),
    CreatedUtc  timestamptz  NOT NULL DEFAULT NOW(),
    UpdatedUtc  timestamptz  NOT NULL DEFAULT NOW(),
    CONSTRAINT UQ_RoleName UNIQUE (Name)
);

CREATE TABLE permission.roles_history
(
    LIKE permission.roles
);

CREATE TRIGGER versioning_trigger
    BEFORE INSERT OR UPDATE OR DELETE
    ON permission.roles
    FOR EACH ROW
EXECUTE PROCEDURE public.history_trigger_function();

-- Role permissions
CREATE TABLE permission.role_permissions
(
    Id           SERIAL PRIMARY KEY,
    RoleId       INT         NOT NULL REFERENCES permission.roles (Id) ON DELETE CASCADE,
    PermissionId INT         NOT NULL REFERENCES permission.permissions (Id) ON DELETE CASCADE,

    -- for history
    Deleted      BOOLEAN     NOT NULL DEFAULT FALSE,
    EditorId     INT         NULL REFERENCES administration.users (Id),
    CreatedUtc   timestamptz NOT NULL DEFAULT NOW(),
    UpdatedUtc   timestamptz NOT NULL DEFAULT NOW(),
    CONSTRAINT UQ_RolePermission UNIQUE (RoleId, PermissionId)
);

CREATE TABLE permission.role_permissions_history
(
    LIKE permission.role_permissions
);

CREATE TRIGGER versioning_trigger
    BEFORE INSERT OR UPDATE OR DELETE
    ON permission.role_permissions
    FOR EACH ROW
EXECUTE PROCEDURE public.history_trigger_function();

-- Role user
CREATE TABLE permission.role_users
(
    Id         SERIAL PRIMARY KEY,
    RoleId     INT         NOT NULL REFERENCES permission.roles (Id) ON DELETE CASCADE,
    UserId     INT         NOT NULL REFERENCES administration.users (Id) ON DELETE CASCADE,

    -- for history
    Deleted    BOOLEAN     NOT NULL DEFAULT FALSE,
    EditorId   INT         NULL REFERENCES administration.users (Id),
    CreatedUtc timestamptz NOT NULL DEFAULT NOW(),
    UpdatedUtc timestamptz NOT NULL DEFAULT NOW()
);

CREATE TABLE permission.role_users_history
(
    LIKE permission.role_users
);

CREATE TRIGGER versioning_trigger
    BEFORE INSERT OR UPDATE OR DELETE
    ON permission.role_users
    FOR EACH ROW
EXECUTE PROCEDURE public.history_trigger_function();