CREATE TABLE IF NOT EXISTS ops.desktop_client_releases (
    id                    BIGSERIAL PRIMARY KEY,
    platform              VARCHAR(32) NOT NULL,
    arch                  VARCHAR(32) NOT NULL DEFAULT 'universal',
    channel               VARCHAR(32) NOT NULL DEFAULT 'stable',
    version               VARCHAR(64) NOT NULL,
    min_supported_version VARCHAR(64),
    download_source       VARCHAR(16) NOT NULL DEFAULT 'oss',
    oss_object_key        TEXT,
    custom_download_url   TEXT,
    file_name             TEXT,
    file_size_bytes       BIGINT,
    sha256                VARCHAR(64),
    updater_download_source VARCHAR(16),
    updater_oss_object_key  TEXT,
    updater_custom_download_url TEXT,
    updater_file_name       TEXT,
    updater_file_size_bytes BIGINT,
    updater_sha256          VARCHAR(64),
    release_notes         TEXT,
    force_update          BOOLEAN NOT NULL DEFAULT FALSE,
    enabled               BOOLEAN NOT NULL DEFAULT TRUE,
    published_at          TIMESTAMPTZ,
    created_by            BIGINT,
    updated_by            BIGINT,
    created_at            TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at            TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    CONSTRAINT ck_desktop_client_release_file_size
        CHECK (file_size_bytes IS NULL OR file_size_bytes >= 0),
    CONSTRAINT ck_desktop_client_release_sha256
        CHECK (sha256 IS NULL OR sha256 ~ '^[0-9a-f]{64}$'),
    CONSTRAINT ck_desktop_client_release_updater_file_size
        CHECK (updater_file_size_bytes IS NULL OR updater_file_size_bytes >= 0),
    CONSTRAINT ck_desktop_client_release_updater_sha256
        CHECK (updater_sha256 IS NULL OR updater_sha256 ~ '^[0-9a-f]{64}$'),
    CONSTRAINT ck_desktop_client_release_source
        CHECK (download_source IN ('oss', 'custom')),
    CONSTRAINT ck_desktop_client_release_updater_source
        CHECK (updater_download_source IS NULL OR updater_download_source IN ('oss', 'custom')),
    CONSTRAINT ck_desktop_client_release_download_target
        CHECK (
            (download_source = 'oss' AND NULLIF(BTRIM(COALESCE(oss_object_key, '')), '') IS NOT NULL)
            OR
            (download_source = 'custom' AND NULLIF(BTRIM(COALESCE(custom_download_url, '')), '') IS NOT NULL)
        ),
    CONSTRAINT ck_desktop_client_release_updater_target
        CHECK (
            updater_download_source IS NULL
            OR
            (updater_download_source = 'oss' AND NULLIF(BTRIM(COALESCE(updater_oss_object_key, '')), '') IS NOT NULL)
            OR
            (updater_download_source = 'custom' AND NULLIF(BTRIM(COALESCE(updater_custom_download_url, '')), '') IS NOT NULL)
        )
);

CREATE UNIQUE INDEX IF NOT EXISTS uk_desktop_client_releases_target
    ON ops.desktop_client_releases(platform, arch, channel);

CREATE INDEX IF NOT EXISTS idx_desktop_client_releases_lookup
    ON ops.desktop_client_releases(channel, platform, arch)
    WHERE enabled = TRUE;

CREATE INDEX IF NOT EXISTS idx_desktop_client_releases_updated_at
    ON ops.desktop_client_releases(updated_at DESC);
