"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.metadata = void 0;
// Admin approve leader application
const db_1 = require("@b/db");
const error_1 = require("@b/utils/error");
const utils_1 = require("@b/api/(ext)/copy-trading/utils");
exports.metadata = {
    summary: "Approve Leader Application (Admin)",
    description: "Approves a pending leader application.",
    operationId: "adminApproveCopyTradingLeader",
    tags: ["Admin", "Copy Trading"],
    requiresAuth: true,
    permission: "access.copy_trading",
    middleware: ["copyTradingAdmin"],
    logModule: "ADMIN_COPY",
    logTitle: "Approve copy trading leader",
    parameters: [
        {
            name: "id",
            in: "path",
            required: true,
            schema: { type: "string" },
        },
    ],
    responses: {
        200: { description: "Leader approved successfully" },
        400: { description: "Bad Request" },
        401: { description: "Unauthorized" },
        403: { description: "Forbidden" },
        404: { description: "Leader not found" },
        500: { description: "Internal Server Error" },
    },
};
exports.default = async (data) => {
    const { params, user, ctx } = data;
    const { id } = params;
    // Validate UUID format
    if (!(0, utils_1.isValidUUID)(id)) {
        throw (0, error_1.createError)({ statusCode: 400, message: "Invalid leader ID format" });
    }
    ctx === null || ctx === void 0 ? void 0 : ctx.step("Fetching leader application");
    const leader = await db_1.models.copyTradingLeader.findByPk(id);
    if (!leader) {
        ctx === null || ctx === void 0 ? void 0 : ctx.fail("Leader not found");
        throw (0, error_1.createError)({ statusCode: 404, message: "Leader not found" });
    }
    ctx === null || ctx === void 0 ? void 0 : ctx.step("Validating leader status");
    if (leader.status !== "PENDING") {
        ctx === null || ctx === void 0 ? void 0 : ctx.fail(`Cannot approve leader with status: ${leader.status}`);
        throw (0, error_1.createError)({
            statusCode: 400,
            message: `Cannot approve leader with status: ${leader.status}`,
        });
    }
    ctx === null || ctx === void 0 ? void 0 : ctx.step("Approving leader application");
    const oldStatus = leader.status;
    await leader.update({ status: "ACTIVE" });
    ctx === null || ctx === void 0 ? void 0 : ctx.step("Creating audit log");
    await (0, utils_1.createAuditLog)({
        entityType: "LEADER",
        entityId: id,
        action: "APPROVE",
        oldValue: { status: oldStatus },
        newValue: { status: "ACTIVE" },
        adminId: user === null || user === void 0 ? void 0 : user.id,
    });
    // Notify user about approval
    ctx === null || ctx === void 0 ? void 0 : ctx.step("Sending approval notification");
    await (0, utils_1.notifyLeaderApplicationEvent)(leader.userId, id, "APPROVED", undefined, ctx);
    ctx === null || ctx === void 0 ? void 0 : ctx.success("Leader approved successfully");
    return {
        message: "Leader approved successfully",
        leader: leader.toJSON(),
    };
};
