from __future__ import annotations

import sqlite3

from backend.database.migrations.runner import Migration


def upgrade(connection: sqlite3.Connection) -> None:
    connection.execute(
        """
        CREATE TABLE sector_member_snapshots (
            trade_date TEXT NOT NULL,
            sector_name TEXT NOT NULL,
            sector_code TEXT NOT NULL,
            observed_at TEXT NOT NULL,
            source TEXT NOT NULL,
            coverage REAL NOT NULL CHECK (coverage >= 0 AND coverage <= 1),
            payload_json TEXT NOT NULL,
            PRIMARY KEY (trade_date, sector_name)
        )
        """
    )


def downgrade(connection: sqlite3.Connection) -> None:
    connection.execute("DROP TABLE sector_member_snapshots")


MIGRATION = Migration(
    version=4,
    name="create_sector_member_snapshots",
    signature="market:v2:dated-shenwan-sector-members",
    upgrade=upgrade,
    downgrade=downgrade,
)
