DO
$$
    DECLARE
        _path varchar(256) := 'patch-2025_11_12-DlpSettings';
    BEGIN
        IF EXISTS (SELECT 1 FROM public."Versions" WHERE "Path" = _path) THEN
            RETURN;
        END IF;
        IF EXISTS (SELECT 1 FROM public."Versions" WHERE "Build" >= 15656) THEN
            RETURN;
        END IF;

        BEGIN
            CREATE TABLE IF NOT EXISTS public."DlpSettings"
            (
                "Id"           BIGSERIAL PRIMARY KEY,
                "CustomerId"   BIGINT                      NULL,
                "IsActive"     BOOLEAN                     NOT NULL DEFAULT FALSE,
                "Enabled"      BOOLEAN                     NOT NULL DEFAULT FALSE,
                "Host"         VARCHAR(255)                NULL,
                "Port"         INTEGER                     NOT NULL DEFAULT 1344,
                "ServiceName"  VARCHAR(255)                NULL,
                "ScanActions"  INTEGER                     NOT NULL DEFAULT 1,
                "Options"      JSONB                       NOT NULL DEFAULT '{}'::jsonb,
                "CreatedAtUtc" TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT timezone('utc', now()),
                "UpdatedAtUtc" TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT timezone('utc', now()),
                "UpdatedBy"    BIGINT                      NULL
            );

            CREATE INDEX IF NOT EXISTS "IDX_DlpSettings_UpdatedAtUtc"
                ON public."DlpSettings" ("UpdatedAtUtc");

            INSERT INTO public."Versions" ("Path", "Build") VALUES (_path, 0);

        EXCEPTION
            WHEN OTHERS THEN
                RAISE NOTICE '[ERR] %: %', _path, SQLERRM;
        END;
    END;
$$;
