-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS media_assets(
    id bigserial PRIMARY KEY,
    tenant_id bigint NOT NULL,
    user_id bigint NOT NULL,
    type varchar(32) NOT NULL DEFAULT 'video',
    status varchar(32) NOT NULL DEFAULT 'uploaded',
    provider varchar(64) NOT NULL DEFAULT '',
    bucket varchar(128) NOT NULL DEFAULT '',
    object_key varchar(512) NOT NULL DEFAULT '',
    meta jsonb NOT NULL DEFAULT '{}'::jsonb,
    deleted_at timestamptz,
    created_at timestamptz NOT NULL DEFAULT NOW(),
    updated_at timestamptz NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS ix_media_assets_tenant_id ON media_assets(tenant_id);
CREATE INDEX IF NOT EXISTS ix_media_assets_tenant_user_id ON media_assets(tenant_id, user_id);
CREATE INDEX IF NOT EXISTS ix_media_assets_tenant_status ON media_assets(tenant_id, status);

CREATE TABLE IF NOT EXISTS contents(
    id bigserial PRIMARY KEY,
    tenant_id bigint NOT NULL,
    user_id bigint NOT NULL,
    title varchar(255) NOT NULL DEFAULT '',
    description text NOT NULL DEFAULT '',
    status varchar(32) NOT NULL DEFAULT 'draft',
    visibility varchar(32) NOT NULL DEFAULT 'tenant_only',
    preview_seconds int NOT NULL DEFAULT 60,
    preview_downloadable boolean NOT NULL DEFAULT false,
    published_at timestamptz,
    deleted_at timestamptz,
    created_at timestamptz NOT NULL DEFAULT NOW(),
    updated_at timestamptz NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS ix_contents_tenant_id ON contents(tenant_id);
CREATE INDEX IF NOT EXISTS ix_contents_tenant_user_id ON contents(tenant_id, user_id);
CREATE INDEX IF NOT EXISTS ix_contents_tenant_status ON contents(tenant_id, status);
CREATE INDEX IF NOT EXISTS ix_contents_tenant_visibility ON contents(tenant_id, visibility);

CREATE TABLE IF NOT EXISTS content_assets(
    id bigserial PRIMARY KEY,
    tenant_id bigint NOT NULL,
    user_id bigint NOT NULL,
    content_id bigint NOT NULL,
    asset_id bigint NOT NULL,
    role varchar(32) NOT NULL DEFAULT 'main',
    sort int NOT NULL DEFAULT 0,
    created_at timestamptz NOT NULL DEFAULT NOW(),
    updated_at timestamptz NOT NULL DEFAULT NOW(),
    UNIQUE (tenant_id, content_id, asset_id)
);

CREATE INDEX IF NOT EXISTS ix_content_assets_tenant_content ON content_assets(tenant_id, content_id);
CREATE INDEX IF NOT EXISTS ix_content_assets_tenant_asset ON content_assets(tenant_id, asset_id);
CREATE INDEX IF NOT EXISTS ix_content_assets_tenant_role ON content_assets(tenant_id, content_id, role);

CREATE TABLE IF NOT EXISTS content_prices(
    id bigserial PRIMARY KEY,
    tenant_id bigint NOT NULL,
    user_id bigint NOT NULL,
    content_id bigint NOT NULL,
    currency varchar(16) NOT NULL DEFAULT 'CNY',
    price_amount bigint NOT NULL DEFAULT 0,
    discount_type varchar(16) NOT NULL DEFAULT 'none',
    discount_value bigint NOT NULL DEFAULT 0,
    discount_start_at timestamptz,
    discount_end_at timestamptz,
    created_at timestamptz NOT NULL DEFAULT NOW(),
    updated_at timestamptz NOT NULL DEFAULT NOW(),
    UNIQUE (tenant_id, content_id)
);

CREATE INDEX IF NOT EXISTS ix_content_prices_tenant_id ON content_prices(tenant_id);

CREATE TABLE IF NOT EXISTS content_access(
    id bigserial PRIMARY KEY,
    tenant_id bigint NOT NULL,
    user_id bigint NOT NULL,
    content_id bigint NOT NULL,
    order_id bigint,
    status varchar(16) NOT NULL DEFAULT 'active',
    revoked_at timestamptz,
    created_at timestamptz NOT NULL DEFAULT NOW(),
    updated_at timestamptz NOT NULL DEFAULT NOW(),
    UNIQUE (tenant_id, user_id, content_id)
);

CREATE INDEX IF NOT EXISTS ix_content_access_tenant_user ON content_access(tenant_id, user_id);
CREATE INDEX IF NOT EXISTS ix_content_access_tenant_content ON content_access(tenant_id, content_id);

-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP INDEX IF EXISTS ix_content_access_tenant_content;
DROP INDEX IF EXISTS ix_content_access_tenant_user;
DROP TABLE IF EXISTS content_access;

DROP INDEX IF EXISTS ix_content_prices_tenant_id;
DROP TABLE IF EXISTS content_prices;

DROP INDEX IF EXISTS ix_content_assets_tenant_role;
DROP INDEX IF EXISTS ix_content_assets_tenant_asset;
DROP INDEX IF EXISTS ix_content_assets_tenant_content;
DROP TABLE IF EXISTS content_assets;

DROP INDEX IF EXISTS ix_contents_tenant_visibility;
DROP INDEX IF EXISTS ix_contents_tenant_status;
DROP INDEX IF EXISTS ix_contents_tenant_user_id;
DROP INDEX IF EXISTS ix_contents_tenant_id;
DROP TABLE IF EXISTS contents;

DROP INDEX IF EXISTS ix_media_assets_tenant_status;
DROP INDEX IF EXISTS ix_media_assets_tenant_user_id;
DROP INDEX IF EXISTS ix_media_assets_tenant_id;
DROP TABLE IF EXISTS media_assets;

-- +goose StatementEnd

