CREATE TABLE IF NOT EXISTS mobile_app_versions (
    id BIGSERIAL PRIMARY KEY,
    platform VARCHAR(20) NOT NULL CHECK (platform IN ('ANDROID', 'IOS')),
    version_name VARCHAR(50) NOT NULL,
    version_code INT NOT NULL CHECK (version_code > 0),
    update_type VARCHAR(20) NOT NULL DEFAULT 'OPTIONAL' CHECK (update_type IN ('FORCE', 'OPTIONAL')),
    release_notes TEXT,
    store_url TEXT,
    min_supported_version_code INT CHECK (min_supported_version_code IS NULL OR min_supported_version_code > 0),
    status VARCHAR(20) NOT NULL DEFAULT 'ACTIVE' CHECK (status IN ('ACTIVE', 'INACTIVE')),
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ,
    CONSTRAINT mobile_app_versions_platform_version_code UNIQUE (platform, version_code),
    CONSTRAINT mobile_app_versions_platform_version_name UNIQUE (platform, version_name)
);

CREATE INDEX IF NOT EXISTS idx_mobile_app_versions_platform ON mobile_app_versions (platform);
CREATE INDEX IF NOT EXISTS idx_mobile_app_versions_status ON mobile_app_versions (status);
CREATE INDEX IF NOT EXISTS idx_mobile_app_versions_platform_code ON mobile_app_versions (platform, version_code DESC);
