-- CreateEnum
CREATE TYPE "RoleType" AS ENUM ('COMMUNITY', 'AUTH_PROVINCE_COMPANY', 'PROVINCE_COMPANY', 'AUTH_CITY_COMPANY', 'CITY_COMPANY');

-- CreateEnum
CREATE TYPE "AuthorizationStatus" AS ENUM ('PENDING', 'AUTHORIZED', 'REVOKED');

-- CreateEnum
CREATE TYPE "AssessmentResult" AS ENUM ('NOT_ASSESSED', 'PASS', 'FAIL', 'BYPASSED');

-- CreateEnum
CREATE TYPE "MonthlyTargetType" AS ENUM ('NONE', 'FIXED', 'LADDER');

-- CreateEnum
CREATE TYPE "RestrictionType" AS ENUM ('ACCOUNT_LIMIT', 'TOTAL_LIMIT');

-- CreateEnum
CREATE TYPE "ApprovalStatus" AS ENUM ('PENDING', 'APPROVED', 'REJECTED');

-- CreateEnum
CREATE TYPE "OperationType" AS ENUM ('GRANT_AUTHORIZATION', 'REVOKE_AUTHORIZATION', 'GRANT_BYPASS', 'EXEMPT_PERCENTAGE', 'MODIFY_CONFIG');

-- CreateEnum
CREATE TYPE "RegionType" AS ENUM ('PROVINCE', 'CITY');

-- CreateEnum
CREATE TYPE "SystemAccountType" AS ENUM ('COST_ACCOUNT', 'OPERATION_ACCOUNT', 'HQ_COMMUNITY', 'RWAD_POOL_PENDING', 'SYSTEM_PROVINCE', 'SYSTEM_CITY');

-- CreateEnum
CREATE TYPE "SystemLedgerEntryType" AS ENUM ('PLANTING_ALLOCATION', 'REWARD_EXPIRED', 'TRANSFER_OUT', 'TRANSFER_IN', 'WITHDRAWAL', 'ADJUSTMENT');

-- CreateTable
CREATE TABLE "authorization_roles" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "role_type" "RoleType" NOT NULL,
    "region_code" TEXT NOT NULL,
    "region_name" TEXT NOT NULL,
    "status" "AuthorizationStatus" NOT NULL DEFAULT 'PENDING',
    "display_title" TEXT NOT NULL,
    "authorized_at" TIMESTAMP(3),
    "authorized_by" TEXT,
    "revoked_at" TIMESTAMP(3),
    "revoked_by" TEXT,
    "revoke_reason" TEXT,
    "initial_target_tree_count" INTEGER NOT NULL,
    "monthly_target_type" "MonthlyTargetType" NOT NULL,
    "require_local_percentage" DECIMAL(5,2) NOT NULL DEFAULT 5.0,
    "exempt_from_percentage_check" BOOLEAN NOT NULL DEFAULT false,
    "benefit_active" BOOLEAN NOT NULL DEFAULT false,
    "benefit_activated_at" TIMESTAMP(3),
    "benefit_deactivated_at" TIMESTAMP(3),
    "current_month_index" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "authorization_roles_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "monthly_assessments" (
    "id" TEXT NOT NULL,
    "authorization_id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "role_type" "RoleType" NOT NULL,
    "region_code" TEXT NOT NULL,
    "assessment_month" TEXT NOT NULL,
    "month_index" INTEGER NOT NULL,
    "monthly_target" INTEGER NOT NULL,
    "cumulative_target" INTEGER NOT NULL,
    "monthly_completed" INTEGER NOT NULL DEFAULT 0,
    "cumulative_completed" INTEGER NOT NULL DEFAULT 0,
    "completed_at" TIMESTAMP(3),
    "local_team_count" INTEGER NOT NULL DEFAULT 0,
    "total_team_count" INTEGER NOT NULL DEFAULT 0,
    "local_percentage" DECIMAL(5,2) NOT NULL DEFAULT 0,
    "local_percentage_pass" BOOLEAN NOT NULL DEFAULT false,
    "exceed_ratio" DECIMAL(10,4) NOT NULL DEFAULT 0,
    "result" "AssessmentResult" NOT NULL DEFAULT 'NOT_ASSESSED',
    "ranking_in_region" INTEGER,
    "is_first_place" BOOLEAN NOT NULL DEFAULT false,
    "is_bypassed" BOOLEAN NOT NULL DEFAULT false,
    "bypassed_by" TEXT,
    "bypassed_at" TIMESTAMP(3),
    "assessed_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "monthly_assessments_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "monthly_bypasses" (
    "id" TEXT NOT NULL,
    "authorization_id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "role_type" "RoleType" NOT NULL,
    "bypass_month" TEXT NOT NULL,
    "granted_by" TEXT NOT NULL,
    "granted_at" TIMESTAMP(3) NOT NULL,
    "reason" TEXT,
    "approver1_id" TEXT NOT NULL,
    "approver1_at" TIMESTAMP(3) NOT NULL,
    "approver2_id" TEXT,
    "approver2_at" TIMESTAMP(3),
    "approver3_id" TEXT,
    "approver3_at" TIMESTAMP(3),
    "approval_status" "ApprovalStatus" NOT NULL DEFAULT 'PENDING',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "monthly_bypasses_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "ladder_target_configs" (
    "id" TEXT NOT NULL,
    "role_type" "RoleType" NOT NULL,
    "month_index" INTEGER NOT NULL,
    "monthly_target" INTEGER NOT NULL,
    "cumulative_target" INTEGER NOT NULL,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "ladder_target_configs_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "planting_restrictions" (
    "id" TEXT NOT NULL,
    "restriction_type" "RestrictionType" NOT NULL,
    "account_limit_days" INTEGER,
    "account_limit_count" INTEGER,
    "total_limit_days" INTEGER,
    "total_limit_count" INTEGER,
    "current_total_count" INTEGER NOT NULL DEFAULT 0,
    "start_at" TIMESTAMP(3) NOT NULL,
    "end_at" TIMESTAMP(3) NOT NULL,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_by" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "planting_restrictions_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "admin_approvals" (
    "id" TEXT NOT NULL,
    "operation_type" "OperationType" NOT NULL,
    "target_id" TEXT NOT NULL,
    "target_type" TEXT NOT NULL,
    "request_data" JSONB NOT NULL,
    "status" "ApprovalStatus" NOT NULL DEFAULT 'PENDING',
    "requester_id" TEXT NOT NULL,
    "approver1_id" TEXT,
    "approver1_at" TIMESTAMP(3),
    "approver2_id" TEXT,
    "approver2_at" TIMESTAMP(3),
    "approver3_id" TEXT,
    "approver3_at" TIMESTAMP(3),
    "completed_at" TIMESTAMP(3),
    "rejected_by" TEXT,
    "rejected_at" TIMESTAMP(3),
    "reject_reason" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "admin_approvals_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "authorization_audit_logs" (
    "id" TEXT NOT NULL,
    "operation_type" TEXT NOT NULL,
    "target_user_id" TEXT NOT NULL,
    "target_role_type" "RoleType",
    "target_region_code" TEXT,
    "operator_id" TEXT NOT NULL,
    "operator_role" TEXT NOT NULL,
    "before_state" JSONB,
    "after_state" JSONB,
    "ip_address" TEXT,
    "user_agent" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "authorization_audit_logs_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "region_heat_maps" (
    "id" TEXT NOT NULL,
    "region_code" TEXT NOT NULL,
    "region_name" TEXT NOT NULL,
    "region_type" "RegionType" NOT NULL,
    "total_plantings" INTEGER NOT NULL DEFAULT 0,
    "monthly_plantings" INTEGER NOT NULL DEFAULT 0,
    "weekly_plantings" INTEGER NOT NULL DEFAULT 0,
    "daily_plantings" INTEGER NOT NULL DEFAULT 0,
    "active_users" INTEGER NOT NULL DEFAULT 0,
    "auth_company_count" INTEGER NOT NULL DEFAULT 0,
    "heat_score" DECIMAL(10,2) NOT NULL DEFAULT 0,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "region_heat_maps_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "stickman_rankings" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "authorization_id" TEXT NOT NULL,
    "role_type" "RoleType" NOT NULL,
    "region_code" TEXT NOT NULL,
    "region_name" TEXT NOT NULL,
    "nickname" TEXT NOT NULL,
    "avatar_url" TEXT,
    "current_month" TEXT NOT NULL,
    "cumulative_completed" INTEGER NOT NULL,
    "cumulative_target" INTEGER NOT NULL,
    "progress_percentage" DECIMAL(5,2) NOT NULL,
    "exceed_ratio" DECIMAL(10,4) NOT NULL,
    "ranking" INTEGER NOT NULL,
    "is_first_place" BOOLEAN NOT NULL,
    "monthly_reward_usdt" DECIMAL(18,2) NOT NULL,
    "monthly_reward_rwad" DECIMAL(18,8) NOT NULL,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "stickman_rankings_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "authorization_configs" (
    "id" TEXT NOT NULL,
    "config_key" TEXT NOT NULL,
    "config_value" TEXT NOT NULL,
    "description" TEXT,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "authorization_configs_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "system_accounts" (
    "account_id" BIGSERIAL NOT NULL,
    "account_type" "SystemAccountType" NOT NULL,
    "region_code" VARCHAR(10),
    "region_name" VARCHAR(50),
    "wallet_address" VARCHAR(42),
    "mpc_public_key" VARCHAR(130),
    "usdt_balance" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "hashpower" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "total_received" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "total_transferred" DECIMAL(20,8) NOT NULL DEFAULT 0,
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "system_accounts_pkey" PRIMARY KEY ("account_id")
);

-- CreateTable
CREATE TABLE "system_account_ledgers" (
    "ledger_id" BIGSERIAL NOT NULL,
    "account_id" BIGINT NOT NULL,
    "entry_type" "SystemLedgerEntryType" NOT NULL,
    "amount" DECIMAL(20,8) NOT NULL,
    "balance_after" DECIMAL(20,8) NOT NULL,
    "source_order_id" BIGINT,
    "source_reward_id" BIGINT,
    "tx_hash" VARCHAR(66),
    "memo" VARCHAR(500),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "system_account_ledgers_pkey" PRIMARY KEY ("ledger_id")
);

-- CreateIndex
CREATE INDEX "authorization_roles_user_id_idx" ON "authorization_roles"("user_id");

-- CreateIndex
CREATE INDEX "authorization_roles_role_type_region_code_idx" ON "authorization_roles"("role_type", "region_code");

-- CreateIndex
CREATE INDEX "authorization_roles_status_idx" ON "authorization_roles"("status");

-- CreateIndex
CREATE INDEX "authorization_roles_role_type_status_idx" ON "authorization_roles"("role_type", "status");

-- CreateIndex
CREATE UNIQUE INDEX "authorization_roles_user_id_role_type_region_code_key" ON "authorization_roles"("user_id", "role_type", "region_code");

-- CreateIndex
CREATE INDEX "monthly_assessments_user_id_assessment_month_idx" ON "monthly_assessments"("user_id", "assessment_month");

-- CreateIndex
CREATE INDEX "monthly_assessments_role_type_region_code_assessment_month_idx" ON "monthly_assessments"("role_type", "region_code", "assessment_month");

-- CreateIndex
CREATE INDEX "monthly_assessments_assessment_month_result_idx" ON "monthly_assessments"("assessment_month", "result");

-- CreateIndex
CREATE INDEX "monthly_assessments_assessment_month_role_type_exceed_ratio_idx" ON "monthly_assessments"("assessment_month", "role_type", "exceed_ratio" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "monthly_assessments_authorization_id_assessment_month_key" ON "monthly_assessments"("authorization_id", "assessment_month");

-- CreateIndex
CREATE INDEX "monthly_bypasses_user_id_bypass_month_idx" ON "monthly_bypasses"("user_id", "bypass_month");

-- CreateIndex
CREATE UNIQUE INDEX "monthly_bypasses_authorization_id_bypass_month_key" ON "monthly_bypasses"("authorization_id", "bypass_month");

-- CreateIndex
CREATE UNIQUE INDEX "ladder_target_configs_role_type_month_index_key" ON "ladder_target_configs"("role_type", "month_index");

-- CreateIndex
CREATE INDEX "admin_approvals_status_idx" ON "admin_approvals"("status");

-- CreateIndex
CREATE INDEX "admin_approvals_target_id_target_type_idx" ON "admin_approvals"("target_id", "target_type");

-- CreateIndex
CREATE INDEX "authorization_audit_logs_target_user_id_idx" ON "authorization_audit_logs"("target_user_id");

-- CreateIndex
CREATE INDEX "authorization_audit_logs_operator_id_idx" ON "authorization_audit_logs"("operator_id");

-- CreateIndex
CREATE INDEX "authorization_audit_logs_created_at_idx" ON "authorization_audit_logs"("created_at");

-- CreateIndex
CREATE UNIQUE INDEX "region_heat_maps_region_code_region_type_key" ON "region_heat_maps"("region_code", "region_type");

-- CreateIndex
CREATE INDEX "stickman_rankings_role_type_region_code_current_month_idx" ON "stickman_rankings"("role_type", "region_code", "current_month");

-- CreateIndex
CREATE UNIQUE INDEX "stickman_rankings_authorization_id_current_month_key" ON "stickman_rankings"("authorization_id", "current_month");

-- CreateIndex
CREATE UNIQUE INDEX "authorization_configs_config_key_key" ON "authorization_configs"("config_key");

-- CreateIndex
CREATE INDEX "idx_system_account_type" ON "system_accounts"("account_type");

-- CreateIndex
CREATE INDEX "idx_system_wallet_address" ON "system_accounts"("wallet_address");

-- CreateIndex
CREATE UNIQUE INDEX "system_accounts_account_type_region_code_key" ON "system_accounts"("account_type", "region_code");

-- CreateIndex
CREATE INDEX "idx_system_ledger_account_created" ON "system_account_ledgers"("account_id", "created_at" DESC);

-- CreateIndex
CREATE INDEX "idx_system_ledger_source_order" ON "system_account_ledgers"("source_order_id");

-- CreateIndex
CREATE INDEX "idx_system_ledger_tx_hash" ON "system_account_ledgers"("tx_hash");

-- AddForeignKey
ALTER TABLE "monthly_assessments" ADD CONSTRAINT "monthly_assessments_authorization_id_fkey" FOREIGN KEY ("authorization_id") REFERENCES "authorization_roles"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "monthly_bypasses" ADD CONSTRAINT "monthly_bypasses_authorization_id_fkey" FOREIGN KEY ("authorization_id") REFERENCES "authorization_roles"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "system_account_ledgers" ADD CONSTRAINT "system_account_ledgers_account_id_fkey" FOREIGN KEY ("account_id") REFERENCES "system_accounts"("account_id") ON DELETE RESTRICT ON UPDATE CASCADE;

