"""ad_pangle_daily_revenue: 穿山甲 GroMore 天级收益报表(后台结算口径)

新建表存放从 GroMore 数据 API 按天拉取的收益(revenue 预估 + api_revenue 收益Api),
粒度 = 日期 × 应用(app_env) × 代码位(our_code_id) × 广告源(adn)。供广告收益报表的
汇总/趋势级展示「穿山甲后台收益」,与客户端自报 eCPM 折算的预估互为对照。

Revision ID: ad_pangle_daily_revenue
Revises: 7db22acee504
Create Date: 2026-06-28
"""

from __future__ import annotations

from alembic import op
import sqlalchemy as sa


revision = "ad_pangle_daily_revenue"
down_revision = "7db22acee504"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "ad_pangle_daily_revenue",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("report_date", sa.String(length=10), nullable=False),
        sa.Column("app_env", sa.String(length=16), nullable=False),
        sa.Column("site_id", sa.String(length=32), nullable=True),
        sa.Column("our_code_id", sa.String(length=64), nullable=False),
        sa.Column("adn", sa.String(length=16), nullable=False, server_default=""),
        sa.Column("revenue_yuan", sa.Float(), nullable=False, server_default="0"),
        sa.Column("api_revenue_yuan", sa.Float(), nullable=True),
        sa.Column("ecpm", sa.String(length=32), nullable=True),
        sa.Column("impressions", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("currency", sa.String(length=8), nullable=False, server_default="cny"),
        sa.Column(
            "synced_at",
            sa.DateTime(timezone=True),
            server_default=sa.func.now(),
            nullable=False,
        ),
        sa.UniqueConstraint(
            "report_date", "app_env", "our_code_id", "adn", name="uq_ad_pangle_daily"
        ),
    )
    op.create_index(
        "ix_ad_pangle_daily_revenue_report_date",
        "ad_pangle_daily_revenue",
        ["report_date"],
    )
    op.create_index(
        "ix_ad_pangle_daily_revenue_our_code_id",
        "ad_pangle_daily_revenue",
        ["our_code_id"],
    )


def downgrade() -> None:
    op.drop_index("ix_ad_pangle_daily_revenue_our_code_id", table_name="ad_pangle_daily_revenue")
    op.drop_index("ix_ad_pangle_daily_revenue_report_date", table_name="ad_pangle_daily_revenue")
    op.drop_table("ad_pangle_daily_revenue")
