-- CreateTable
CREATE TABLE "report_definitions" (
    "definition_id" BIGSERIAL NOT NULL,
    "report_type" VARCHAR(50) NOT NULL,
    "report_name" VARCHAR(200) NOT NULL,
    "report_code" VARCHAR(50) NOT NULL,
    "description" TEXT,
    "parameters" JSONB NOT NULL,
    "schedule_cron" VARCHAR(100),
    "schedule_timezone" VARCHAR(50) DEFAULT 'Asia/Shanghai',
    "schedule_enabled" BOOLEAN NOT NULL DEFAULT false,
    "output_formats" TEXT[],
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,
    "last_generated_at" TIMESTAMP(3),

    CONSTRAINT "report_definitions_pkey" PRIMARY KEY ("definition_id")
);

-- CreateTable
CREATE TABLE "report_snapshots" (
    "snapshot_id" BIGSERIAL NOT NULL,
    "report_type" VARCHAR(50) NOT NULL,
    "report_code" VARCHAR(50) NOT NULL,
    "report_period" VARCHAR(20) NOT NULL,
    "period_key" VARCHAR(30) NOT NULL,
    "snapshot_data" JSONB NOT NULL,
    "summary_data" JSONB,
    "data_sources" TEXT[],
    "data_freshness" INTEGER NOT NULL DEFAULT 0,
    "filter_params" JSONB,
    "row_count" INTEGER NOT NULL DEFAULT 0,
    "period_start_at" TIMESTAMP(3) NOT NULL,
    "period_end_at" TIMESTAMP(3) NOT NULL,
    "generated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expires_at" TIMESTAMP(3),

    CONSTRAINT "report_snapshots_pkey" PRIMARY KEY ("snapshot_id")
);

-- CreateTable
CREATE TABLE "report_files" (
    "file_id" BIGSERIAL NOT NULL,
    "snapshot_id" BIGINT NOT NULL,
    "file_name" VARCHAR(500) NOT NULL,
    "file_path" VARCHAR(1000) NOT NULL,
    "file_url" VARCHAR(1000),
    "file_size" BIGINT NOT NULL,
    "file_format" VARCHAR(20) NOT NULL,
    "mime_type" VARCHAR(100) NOT NULL,
    "download_count" INTEGER NOT NULL DEFAULT 0,
    "last_download_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expires_at" TIMESTAMP(3),

    CONSTRAINT "report_files_pkey" PRIMARY KEY ("file_id")
);

-- CreateTable
CREATE TABLE "analytics_metrics" (
    "metric_id" BIGSERIAL NOT NULL,
    "metric_type" VARCHAR(50) NOT NULL,
    "metric_code" VARCHAR(50) NOT NULL,
    "dimension_time" DATE,
    "dimension_region" VARCHAR(100),
    "dimension_user_type" VARCHAR(50),
    "dimension_right_type" VARCHAR(50),
    "metric_value" DECIMAL(20,8) NOT NULL,
    "metric_data" JSONB,
    "calculated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "analytics_metrics_pkey" PRIMARY KEY ("metric_id")
);

-- CreateTable
CREATE TABLE "planting_daily_stats" (
    "stat_id" BIGSERIAL NOT NULL,
    "stat_date" DATE NOT NULL,
    "province_code" VARCHAR(10),
    "city_code" VARCHAR(10),
    "order_count" INTEGER NOT NULL DEFAULT 0,
    "tree_count" INTEGER NOT NULL DEFAULT 0,
    "total_amount" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "new_user_count" INTEGER NOT NULL DEFAULT 0,
    "active_user_count" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "planting_daily_stats_pkey" PRIMARY KEY ("stat_id")
);

-- CreateTable
CREATE TABLE "community_stats" (
    "stat_id" BIGSERIAL NOT NULL,
    "community_id" BIGINT NOT NULL,
    "community_name" VARCHAR(200) NOT NULL,
    "parent_community_id" BIGINT,
    "stat_date" DATE NOT NULL,
    "total_planting" INTEGER NOT NULL DEFAULT 0,
    "daily_planting" INTEGER NOT NULL DEFAULT 0,
    "weekly_planting" INTEGER NOT NULL DEFAULT 0,
    "monthly_planting" INTEGER NOT NULL DEFAULT 0,
    "member_count" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "community_stats_pkey" PRIMARY KEY ("stat_id")
);

-- CreateTable
CREATE TABLE "system_account_monthly_stats" (
    "stat_id" BIGSERIAL NOT NULL,
    "account_id" BIGINT NOT NULL,
    "account_type" VARCHAR(30) NOT NULL,
    "account_name" VARCHAR(200) NOT NULL,
    "region_code" VARCHAR(10) NOT NULL,
    "stat_month" VARCHAR(7) NOT NULL,
    "monthly_hashpower" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "cumulative_hashpower" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "monthly_mining" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "cumulative_mining" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "monthly_commission" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "cumulative_commission" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "monthly_planting_bonus" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "cumulative_planting_bonus" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "system_account_monthly_stats_pkey" PRIMARY KEY ("stat_id")
);

-- CreateTable
CREATE TABLE "system_account_income_records" (
    "record_id" BIGSERIAL NOT NULL,
    "account_id" BIGINT NOT NULL,
    "account_type" VARCHAR(30) NOT NULL,
    "income_type" VARCHAR(50) NOT NULL,
    "income_amount" DECIMAL(20,8) NOT NULL,
    "currency" VARCHAR(10) NOT NULL,
    "source_type" VARCHAR(50) NOT NULL,
    "source_id" VARCHAR(100),
    "source_user_id" BIGINT,
    "source_address" VARCHAR(200),
    "transaction_no" VARCHAR(100),
    "memo" TEXT,
    "occurred_at" TIMESTAMP(3) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "system_account_income_records_pkey" PRIMARY KEY ("record_id")
);

-- CreateTable
CREATE TABLE "report_events" (
    "event_id" BIGSERIAL NOT NULL,
    "event_type" VARCHAR(50) NOT NULL,
    "aggregate_id" VARCHAR(100) NOT NULL,
    "aggregate_type" VARCHAR(50) NOT NULL,
    "event_data" JSONB NOT NULL,
    "user_id" BIGINT,
    "occurred_at" TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "version" INTEGER NOT NULL DEFAULT 1,

    CONSTRAINT "report_events_pkey" PRIMARY KEY ("event_id")
);

-- CreateTable
CREATE TABLE "dashboard_stats_snapshots" (
    "snapshot_id" BIGSERIAL NOT NULL,
    "snapshot_date" DATE NOT NULL,
    "total_planting_count" INTEGER NOT NULL DEFAULT 0,
    "total_planting_change" DECIMAL(5,2) NOT NULL DEFAULT 0,
    "active_user_count" INTEGER NOT NULL DEFAULT 0,
    "active_user_change" DECIMAL(5,2) NOT NULL DEFAULT 0,
    "province_company_count" INTEGER NOT NULL DEFAULT 0,
    "province_company_change" DECIMAL(5,2) NOT NULL DEFAULT 0,
    "city_company_count" INTEGER NOT NULL DEFAULT 0,
    "city_company_change" DECIMAL(5,2) NOT NULL DEFAULT 0,
    "region_distribution" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "dashboard_stats_snapshots_pkey" PRIMARY KEY ("snapshot_id")
);

-- CreateTable
CREATE TABLE "dashboard_trend_data" (
    "trend_id" BIGSERIAL NOT NULL,
    "trend_date" DATE NOT NULL,
    "planting_count" INTEGER NOT NULL DEFAULT 0,
    "order_count" INTEGER NOT NULL DEFAULT 0,
    "new_user_count" INTEGER NOT NULL DEFAULT 0,
    "active_user_count" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "dashboard_trend_data_pkey" PRIMARY KEY ("trend_id")
);

-- CreateTable
CREATE TABLE "system_activities" (
    "activity_id" BIGSERIAL NOT NULL,
    "activity_type" VARCHAR(50) NOT NULL,
    "title" VARCHAR(200) NOT NULL,
    "description" VARCHAR(500) NOT NULL,
    "icon" VARCHAR(10) NOT NULL DEFAULT '📌',
    "related_user_id" BIGINT,
    "related_entity_id" VARCHAR(100),
    "related_entity_type" VARCHAR(50),
    "metadata" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "system_activities_pkey" PRIMARY KEY ("activity_id")
);

-- CreateIndex
CREATE UNIQUE INDEX "report_definitions_report_code_key" ON "report_definitions"("report_code");

-- CreateIndex
CREATE INDEX "idx_def_type" ON "report_definitions"("report_type");

-- CreateIndex
CREATE INDEX "idx_def_active" ON "report_definitions"("is_active");

-- CreateIndex
CREATE INDEX "idx_def_scheduled" ON "report_definitions"("schedule_enabled");

-- CreateIndex
CREATE INDEX "idx_snapshot_type" ON "report_snapshots"("report_type");

-- CreateIndex
CREATE INDEX "idx_snapshot_code" ON "report_snapshots"("report_code");

-- CreateIndex
CREATE INDEX "idx_snapshot_period" ON "report_snapshots"("period_key");

-- CreateIndex
CREATE INDEX "idx_snapshot_generated" ON "report_snapshots"("generated_at" DESC);

-- CreateIndex
CREATE INDEX "idx_snapshot_expires" ON "report_snapshots"("expires_at");

-- CreateIndex
CREATE UNIQUE INDEX "report_snapshots_report_code_period_key_key" ON "report_snapshots"("report_code", "period_key");

-- CreateIndex
CREATE INDEX "idx_file_snapshot" ON "report_files"("snapshot_id");

-- CreateIndex
CREATE INDEX "idx_file_format" ON "report_files"("file_format");

-- CreateIndex
CREATE INDEX "idx_file_created" ON "report_files"("created_at" DESC);

-- CreateIndex
CREATE INDEX "idx_metric_type" ON "analytics_metrics"("metric_type");

-- CreateIndex
CREATE INDEX "idx_metric_code" ON "analytics_metrics"("metric_code");

-- CreateIndex
CREATE INDEX "idx_metric_time" ON "analytics_metrics"("dimension_time");

-- CreateIndex
CREATE INDEX "idx_metric_region" ON "analytics_metrics"("dimension_region");

-- CreateIndex
CREATE UNIQUE INDEX "analytics_metrics_metric_code_dimension_time_dimension_regi_key" ON "analytics_metrics"("metric_code", "dimension_time", "dimension_region", "dimension_user_type", "dimension_right_type");

-- CreateIndex
CREATE INDEX "idx_pds_date" ON "planting_daily_stats"("stat_date");

-- CreateIndex
CREATE INDEX "idx_pds_province" ON "planting_daily_stats"("province_code");

-- CreateIndex
CREATE INDEX "idx_pds_city" ON "planting_daily_stats"("city_code");

-- CreateIndex
CREATE UNIQUE INDEX "planting_daily_stats_stat_date_province_code_city_code_key" ON "planting_daily_stats"("stat_date", "province_code", "city_code");

-- CreateIndex
CREATE INDEX "idx_cs_community" ON "community_stats"("community_id");

-- CreateIndex
CREATE INDEX "idx_cs_name" ON "community_stats"("community_name");

-- CreateIndex
CREATE INDEX "idx_cs_date" ON "community_stats"("stat_date");

-- CreateIndex
CREATE INDEX "idx_cs_parent" ON "community_stats"("parent_community_id");

-- CreateIndex
CREATE UNIQUE INDEX "community_stats_community_id_stat_date_key" ON "community_stats"("community_id", "stat_date");

-- CreateIndex
CREATE INDEX "idx_sams_type" ON "system_account_monthly_stats"("account_type");

-- CreateIndex
CREATE INDEX "idx_sams_month" ON "system_account_monthly_stats"("stat_month");

-- CreateIndex
CREATE INDEX "idx_sams_region" ON "system_account_monthly_stats"("region_code");

-- CreateIndex
CREATE UNIQUE INDEX "system_account_monthly_stats_account_id_stat_month_key" ON "system_account_monthly_stats"("account_id", "stat_month");

-- CreateIndex
CREATE INDEX "idx_sair_account" ON "system_account_income_records"("account_id");

-- CreateIndex
CREATE INDEX "idx_sair_type" ON "system_account_income_records"("account_type");

-- CreateIndex
CREATE INDEX "idx_sair_income_type" ON "system_account_income_records"("income_type");

-- CreateIndex
CREATE INDEX "idx_sair_source_type" ON "system_account_income_records"("source_type");

-- CreateIndex
CREATE INDEX "idx_sair_address" ON "system_account_income_records"("source_address");

-- CreateIndex
CREATE INDEX "idx_sair_txno" ON "system_account_income_records"("transaction_no");

-- CreateIndex
CREATE INDEX "idx_sair_occurred" ON "system_account_income_records"("occurred_at" DESC);

-- CreateIndex
CREATE INDEX "idx_report_event_aggregate" ON "report_events"("aggregate_type", "aggregate_id");

-- CreateIndex
CREATE INDEX "idx_report_event_type" ON "report_events"("event_type");

-- CreateIndex
CREATE INDEX "idx_report_event_occurred" ON "report_events"("occurred_at");

-- CreateIndex
CREATE INDEX "idx_dss_date" ON "dashboard_stats_snapshots"("snapshot_date" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "dashboard_stats_snapshots_snapshot_date_key" ON "dashboard_stats_snapshots"("snapshot_date");

-- CreateIndex
CREATE INDEX "idx_dtd_date" ON "dashboard_trend_data"("trend_date" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "dashboard_trend_data_trend_date_key" ON "dashboard_trend_data"("trend_date");

-- CreateIndex
CREATE INDEX "idx_sa_type" ON "system_activities"("activity_type");

-- CreateIndex
CREATE INDEX "idx_sa_created" ON "system_activities"("created_at" DESC);

-- CreateIndex
CREATE INDEX "idx_sa_user" ON "system_activities"("related_user_id");

-- CreateIndex
CREATE INDEX "idx_sa_entity" ON "system_activities"("related_entity_type", "related_entity_id");

-- AddForeignKey
ALTER TABLE "report_files" ADD CONSTRAINT "report_files_snapshot_id_fkey" FOREIGN KEY ("snapshot_id") REFERENCES "report_snapshots"("snapshot_id") ON DELETE CASCADE ON UPDATE CASCADE;
