import {post, get} from '@/shared/services/http';
import {type UserInfoBody} from "@/modules/auth/controllers/auth-api";
import {type Nullable} from "@/shared/utils/display";


// ============================================================
// VipApiUrl — VIP 模块路由常量
// ============================================================

const VipApiUrl = {
    ActivateVipCode: "/api/v1/vip/activate",
    GetVipLevelsList: "/api/v1/vip/levels",
    VipOrders: "/api/v1/vip/orders",
    MockPay: "/api/v1/vip/mock-pay",
} as const;

// ============================================================
// 激活码相关
// ============================================================

export interface ActivateVipCodeRequestParams {
    code: string;
}

export type ActivateVipCodeResponseBody = UserInfoBody;

// ============================================================
// 账户类型常量
// ============================================================

export const TargetAccountType = {
    Individual: "individual",
    Company: "company"
} as const;

export type TargetAccountTypeValue = typeof TargetAccountType[keyof typeof TargetAccountType];
export const TargetAccountTypeLabelMap: Record<TargetAccountTypeValue, string> = {
    [TargetAccountType.Individual]: "个人套餐",
    [TargetAccountType.Company]: "企业套餐"
};

// ============================================================
// VIP 等级
// ============================================================

export interface VipLevelsItem {
    id: number;
    name: string;
    level: number;
    price_cent: number;
    duration_days: number;
    gift_balance_cent: number;
    model_package_id: number;
    description: Nullable<string>;
    image: Nullable<string>;
    is_active: boolean;
    sort_order: number;
    target_account_type: TargetAccountTypeValue;
    seat_limit: Nullable<number>;
    created_at: Date;
    updated_at: Date;
}

// ============================================================
// VIP 订单
// ============================================================

/** VIP 订单状态常量 */
export const VipOrderStatus = {
    Pending: "pending",
    Paid: "paid",
    Expired: "expired",
    Cancelled: "cancelled",
} as const;

export type VipOrderStatusValue = typeof VipOrderStatus[keyof typeof VipOrderStatus];

export const VipOrderStatusLabelMap: Record<VipOrderStatusValue, string> = {
    [VipOrderStatus.Pending]: "待支付",
    [VipOrderStatus.Paid]: "已支付",
    [VipOrderStatus.Expired]: "已过期",
    [VipOrderStatus.Cancelled]: "已取消",
};

/** POST /api/v1/vip/orders — 创建购买订单 */
export interface CreateVipOrderRequest {
    /** 购买的 VIP 等级 ID */
    vip_level_id: number;
    /** 支付渠道：alipay | mock，默认 alipay */
    payment_channel?: string;
}

/** 订单列表查询参数 */
export interface VipOrderListParams {
    /** 按状态筛选，可选 */
    status?: VipOrderStatusValue;
    /** 页码，默认 1 */
    page?: number;
    /** 每页条数，默认 20 */
    page_size?: number;
}

/** VIP 订单读取 Schema */
export interface VipOrderRead {
    id: string;
    user_id: string;
    vip_level_id: number;
    amount_cent: number;
    status: VipOrderStatusValue;
    out_trade_no: string;
    /** 支付宝/微信交易号 */
    transaction_id: Nullable<string>;
    /** 支付链接（扫码支付用） */
    pay_url: Nullable<string>;
    /** 订单过期时间 */
    expires_at: Nullable<Date>;
    /** 支付完成时间 */
    paid_at: Nullable<Date>;
    created_at: Date;
}

// ============================================================
// VipAPI — VIP 模块 API 静态方法类
// 调用方式：VipAPI.activateVipCode() / VipAPI.getVipLevels() 等
// ============================================================

// TODO(human): 请确认 VipOrderStatus 的状态值和中文标签是否正确，根据实际后端逻辑补充/修改

export class VipAPI {
    /** 使用激活码激活 VIP 权益 */
    static async activateVipCode(data: ActivateVipCodeRequestParams): Promise<ActivateVipCodeResponseBody> {
        return await post<ActivateVipCodeResponseBody>(VipApiUrl.ActivateVipCode, data);
    }

    /** 获取 VIP 等级列表（已登录时按账户类型过滤） */
    static async getVipLevels(signal?: AbortSignal): Promise<VipLevelsItem[]> {
        return await get<VipLevelsItem[]>(VipApiUrl.GetVipLevelsList, undefined, {signal});
    }

    /** 创建 VIP 套餐购买订单 */
    static async createVipOrder(data: CreateVipOrderRequest): Promise<VipOrderRead> {
        return await post<VipOrderRead>(VipApiUrl.VipOrders, data);
    }

    /** 查询当前用户的 VIP 订单列表 */
    static async getVipOrders(params?: VipOrderListParams): Promise<VipOrderRead[]> {
        return await get<VipOrderRead[]>(VipApiUrl.VipOrders, params as Record<string, unknown>);
    }

    /** 获取单个 VIP 订单详情 */
    static async getVipOrder(orderId: string): Promise<VipOrderRead> {
        const url = `${VipApiUrl.VipOrders}/${orderId}`;
        return await get<VipOrderRead>(url);
    }

    /** 模拟支付成功（仅开发测试用） */
    static async mockPay(orderId: string): Promise<VipOrderRead> {
        return await get<VipOrderRead>(`${VipApiUrl.MockPay}/${orderId}`);
    }
}
