"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.metadata = void 0;
const db_1 = require("@b/db");
const error_1 = require("@b/utils/error");
const console_1 = require("@b/utils/console");
const Middleware_1 = require("@b/handler/Middleware");
const ownership_1 = require("../../../../p2p/utils/ownership");
const wallet_1 = require("@b/services/wallet");
const errors_1 = require("@b/utils/schema/errors");
exports.metadata = {
    summary: "Update P2P dispute",
    description: "Updates a P2P dispute including status changes, resolution details, and admin messages. Handles fund distribution when resolving disputes based on the outcome (BUYER_WINS, SELLER_WINS, SPLIT, CANCELLED).",
    operationId: "updateAdminP2PDispute",
    tags: ["Admin", "P2P", "Dispute"],
    requiresAuth: true,
    middleware: [Middleware_1.p2pAdminDisputeRateLimit],
    logModule: "ADMIN_P2P",
    logTitle: "Update P2P dispute",
    parameters: [
        {
            index: 0,
            name: "id",
            in: "path",
            description: "Dispute ID",
            required: true,
            schema: { type: "string" },
        },
    ],
    requestBody: {
        description: "Dispute update data",
        required: true,
        content: {
            "application/json": {
                schema: {
                    type: "object",
                    properties: {
                        status: { type: "string", enum: ["PENDING", "IN_PROGRESS", "RESOLVED"] },
                        resolution: {
                            type: "object",
                            properties: {
                                outcome: {
                                    type: "string",
                                    enum: ["BUYER_WINS", "SELLER_WINS", "SPLIT", "CANCELLED"],
                                    description: "Resolution outcome - determines how funds are handled"
                                },
                                notes: { type: "string" },
                            },
                        },
                        message: { type: "string", description: "Admin message to add to dispute" },
                    },
                },
            },
        },
    },
    responses: {
        200: { description: "Dispute updated successfully." },
        401: errors_1.unauthorizedResponse,
        404: (0, errors_1.notFoundResponse)("P2P resource"),
        500: errors_1.serverErrorResponse,
    },
    permission: "edit.p2p.dispute",
};
exports.default = async (data) => {
    var _a, _b, _c;
    const { params, body, user, ctx } = data;
    const { id } = params;
    const { status, resolution, message } = body;
    // Import utilities
    const { sanitizeInput } = await Promise.resolve().then(() => __importStar(require("../../../../p2p/utils/validation")));
    const { notifyTradeEvent } = await Promise.resolve().then(() => __importStar(require("../../../../p2p/utils/notifications")));
    const { broadcastP2PTradeEvent } = await Promise.resolve().then(() => __importStar(require("../../../../p2p/trade/[id]/index.ws")));
    const { getWalletSafe } = await Promise.resolve().then(() => __importStar(require("@b/api/finance/wallet/utils")));
    const { parseAmountConfig } = await Promise.resolve().then(() => __importStar(require("../../../../p2p/utils/json-parser")));
    const transaction = await db_1.sequelize.transaction();
    try {
        ctx === null || ctx === void 0 ? void 0 : ctx.step("Fetching dispute");
        const dispute = await db_1.models.p2pDispute.findByPk(id, {
            include: [{
                    model: db_1.models.p2pTrade,
                    as: "trade",
                    include: [{
                            model: db_1.models.p2pOffer,
                            as: "offer",
                            attributes: ["currency", "walletType"],
                        }],
                }],
            lock: true,
            transaction,
        });
        if (!dispute) {
            await transaction.rollback();
            ctx === null || ctx === void 0 ? void 0 : ctx.fail("Dispute not found");
            throw (0, error_1.createError)({ statusCode: 404, message: "Dispute not found" });
        }
        const trade = dispute.trade;
        let tradeUpdated = false;
        let fundsHandled = false;
        ctx === null || ctx === void 0 ? void 0 : ctx.step("Processing dispute update");
        // Validate status transition if changing status
        if (status) {
            const validStatuses = ["PENDING", "IN_PROGRESS", "RESOLVED"];
            if (!validStatuses.includes(status)) {
                await transaction.rollback();
                throw (0, error_1.createError)({
                    statusCode: 400,
                    message: "Invalid status. Must be PENDING, IN_PROGRESS, or RESOLVED"
                });
            }
            dispute.status = status;
        }
        // Handle resolution with fund management
        if (resolution && resolution.outcome) {
            ctx === null || ctx === void 0 ? void 0 : ctx.step(`Resolving dispute with outcome: ${resolution.outcome}`);
            const sanitizedNotes = resolution.notes ? sanitizeInput(resolution.notes) : "";
            const outcome = resolution.outcome;
            // Validate outcome
            const validOutcomes = ["BUYER_WINS", "SELLER_WINS", "SPLIT", "CANCELLED"];
            if (!validOutcomes.includes(outcome)) {
                await transaction.rollback();
                throw (0, error_1.createError)({
                    statusCode: 400,
                    message: "Invalid resolution outcome"
                });
            }
            // CRITICAL: Only handle funds if trade is in DISPUTED status
            // This prevents double-processing if the trade was already resolved
            if (trade && trade.status === "DISPUTED" && trade.offer) {
                let finalTradeStatus = "COMPLETED";
                if (outcome === "BUYER_WINS" || outcome === "SPLIT") {
                    // Release funds to buyer (with escrow fee deduction - same as normal release)
                    const sellerWallet = await getWalletSafe(trade.sellerId, trade.offer.walletType, trade.offer.currency);
                    if (sellerWallet) {
                        // CRITICAL: Calculate safe amounts to prevent negative values
                        const safeUnlockAmount = Math.min(trade.amount, sellerWallet.inOrder);
                        if (safeUnlockAmount > 0) {
                            // Calculate escrow fee - same as normal trade release
                            const escrowFeeAmount = parseFloat(trade.escrowFee || "0");
                            const platformFee = Math.min(escrowFeeAmount, trade.amount);
                            const buyerNetAmount = Math.max(0, trade.amount - platformFee);
                            // Use wallet service to execute from hold (deducts inOrder)
                            const sellerIdempotencyKey = `p2p_dispute_seller_${trade.id}_${Date.now()}`;
                            await wallet_1.walletService.executeFromHold({
                                idempotencyKey: sellerIdempotencyKey,
                                userId: trade.sellerId,
                                walletId: sellerWallet.id,
                                walletType: trade.offer.walletType,
                                currency: trade.offer.currency,
                                amount: trade.amount,
                                operationType: "P2P_DISPUTE_RESOLVE",
                                fee: platformFee,
                                description: `P2P dispute resolved (${outcome}) - seller debit`,
                                metadata: {
                                    tradeId: trade.id,
                                    disputeId: dispute.id,
                                    resolution: outcome,
                                    adminId: user.id,
                                },
                                transaction,
                            });
                            // Log if amounts don't match (indicates potential issue)
                            if (safeUnlockAmount < trade.amount) {
                                console_1.logger.warn("P2P_DISPUTE", `Partial fund handling for trade ${trade.id}: unlocked=${safeUnlockAmount}, expected=${trade.amount}`);
                            }
                            // Credit buyer with net amount (after platform fee deduction) using wallet service
                            const buyerWallet = await wallet_1.walletCreationService.getOrCreateWallet(trade.buyerId, trade.offer.walletType, trade.offer.currency);
                            const buyerIdempotencyKey = `p2p_dispute_buyer_${trade.id}_${Date.now()}`;
                            await wallet_1.walletService.credit({
                                idempotencyKey: buyerIdempotencyKey,
                                userId: trade.buyerId,
                                walletId: buyerWallet.id,
                                walletType: trade.offer.walletType,
                                currency: trade.offer.currency,
                                amount: buyerNetAmount,
                                operationType: "P2P_DISPUTE_RECEIVE",
                                description: `P2P dispute resolved (${outcome}) - buyer credit`,
                                metadata: {
                                    tradeId: trade.id,
                                    disputeId: dispute.id,
                                    resolution: outcome,
                                    adminId: user.id,
                                    originalAmount: trade.amount,
                                    platformFee,
                                },
                                transaction,
                            });
                            // Record platform commission if there's a fee
                            if (platformFee > 0) {
                                // Get system admin ID for commission recording
                                const systemAdmin = await db_1.models.user.findOne({
                                    include: [{
                                            model: db_1.models.role,
                                            as: "role",
                                            where: { name: "Super Admin" },
                                        }],
                                    order: [["createdAt", "ASC"]], // Get the oldest super admin
                                    transaction,
                                });
                                if (systemAdmin) {
                                    // Record the commission in p2pCommission table
                                    await db_1.models.p2pCommission.create({
                                        adminId: systemAdmin.id,
                                        amount: platformFee,
                                        description: `P2P escrow fee for disputed trade #${trade.id.slice(0, 8)}... - ${trade.amount} ${trade.offer.currency} (${outcome})`,
                                        tradeId: trade.id,
                                    }, { transaction });
                                    console_1.logger.info("P2P_DISPUTE", `Platform commission recorded for trade ${trade.id}: ${platformFee} ${trade.offer.currency}`);
                                }
                                else {
                                    console_1.logger.warn("P2P_DISPUTE", "No super admin found to assign commission");
                                }
                            }
                            fundsHandled = true;
                            console_1.logger.success("P2P_DISPUTE", `Funds transferred to buyer for trade ${trade.id}: ${buyerNetAmount} ${trade.offer.currency} (fee: ${platformFee})`);
                        }
                        else {
                            console_1.logger.warn("P2P_DISPUTE", `No funds available to transfer for trade ${trade.id}`);
                        }
                    }
                    finalTradeStatus = "COMPLETED";
                }
                else if (outcome === "SELLER_WINS" || outcome === "CANCELLED") {
                    // Return funds to seller (unlock from inOrder)
                    const sellerWallet = await getWalletSafe(trade.sellerId, trade.offer.walletType, trade.offer.currency);
                    if (sellerWallet) {
                        // CRITICAL: Calculate safe unlock amount to prevent negative inOrder
                        const safeUnlockAmount = Math.min(trade.amount, sellerWallet.inOrder);
                        if (safeUnlockAmount > 0) {
                            // Use wallet service to release hold
                            const releaseIdempotencyKey = `p2p_dispute_release_${trade.id}_${Date.now()}`;
                            await wallet_1.walletService.release({
                                idempotencyKey: releaseIdempotencyKey,
                                userId: trade.sellerId,
                                walletId: sellerWallet.id,
                                walletType: trade.offer.walletType,
                                currency: trade.offer.currency,
                                amount: safeUnlockAmount,
                                operationType: "P2P_DISPUTE_RESOLVE",
                                description: `P2P dispute resolved (${outcome}) - funds returned to seller`,
                                metadata: {
                                    tradeId: trade.id,
                                    disputeId: dispute.id,
                                    resolution: outcome,
                                    adminId: user.id,
                                },
                                transaction,
                            });
                            fundsHandled = true;
                            // Log if amounts don't match
                            if (safeUnlockAmount < trade.amount) {
                                console_1.logger.warn("P2P_DISPUTE", `Partial unlock for trade ${trade.id}: ${safeUnlockAmount}/${trade.amount}`);
                            }
                        }
                        else {
                            console_1.logger.warn("P2P_DISPUTE", `No funds to unlock for trade ${trade.id}`);
                        }
                    }
                    // Restore offer available amount since trade was cancelled
                    // CRITICAL: Validate against original total to prevent over-restoration
                    if (trade.offerId) {
                        const offer = await db_1.models.p2pOffer.findByPk(trade.offerId, {
                            lock: true,
                            transaction,
                        });
                        if (offer && ["ACTIVE", "PAUSED"].includes(offer.status)) {
                            const amountConfig = parseAmountConfig(offer.amountConfig);
                            // Calculate safe restoration amount
                            const originalTotal = (_a = amountConfig.originalTotal) !== null && _a !== void 0 ? _a : (amountConfig.total + trade.amount);
                            const proposedTotal = amountConfig.total + trade.amount;
                            const safeTotal = Math.min(proposedTotal, originalTotal);
                            if (safeTotal > amountConfig.total) {
                                await offer.update({
                                    amountConfig: {
                                        ...amountConfig,
                                        total: safeTotal,
                                        originalTotal,
                                    },
                                }, { transaction });
                                console_1.logger.info("P2P_DISPUTE", `Restored offer ${offer.id} amount: ${amountConfig.total} -> ${safeTotal}`);
                            }
                            else {
                                console_1.logger.debug("P2P_DISPUTE", `Skipped offer ${offer.id} restoration - at or above limit`);
                            }
                        }
                    }
                    finalTradeStatus = "CANCELLED";
                }
                // Update trade timeline
                let timeline = trade.timeline || [];
                if (typeof timeline === "string") {
                    try {
                        timeline = JSON.parse(timeline);
                    }
                    catch (e) {
                        timeline = [];
                    }
                }
                if (!Array.isArray(timeline)) {
                    timeline = [];
                }
                timeline.push({
                    event: "DISPUTE_RESOLVED",
                    message: `Dispute resolved by admin: ${outcome}${sanitizedNotes ? ` - ${sanitizedNotes}` : ""}`,
                    userId: user.id,
                    adminName: `${user.firstName} ${user.lastName}`,
                    resolution: outcome,
                    createdAt: new Date().toISOString(),
                });
                // Update trade status
                await trade.update({
                    status: finalTradeStatus,
                    timeline,
                    resolution: { outcome, notes: sanitizedNotes, resolvedBy: user.id },
                    completedAt: finalTradeStatus === "COMPLETED" ? new Date() : null,
                    cancelledAt: finalTradeStatus === "CANCELLED" ? new Date() : null,
                }, { transaction });
                tradeUpdated = true;
            }
            // Update dispute resolution
            dispute.resolution = {
                outcome,
                notes: sanitizedNotes,
                resolvedBy: user.id,
                resolvedAt: new Date().toISOString(),
                fundsHandled,
            };
            dispute.resolvedOn = new Date();
            dispute.status = "RESOLVED";
        }
        // Handle message
        let sanitizedMessage;
        if (message) {
            sanitizedMessage = sanitizeInput(message);
            if (!sanitizedMessage || sanitizedMessage.length === 0) {
                await transaction.rollback();
                throw (0, error_1.createError)({
                    statusCode: 400,
                    message: "Message cannot be empty"
                });
            }
            const messageId = `msg-${Date.now()}-${user.id}`;
            const messageTimestamp = new Date().toISOString();
            // Add to dispute messages
            let existingMessages = dispute.messages;
            if (!Array.isArray(existingMessages)) {
                existingMessages = [];
            }
            existingMessages.push({
                id: messageId,
                sender: user.id,
                senderName: `${user.firstName} ${user.lastName}`,
                content: sanitizedMessage,
                createdAt: messageTimestamp,
                isAdmin: true,
            });
            dispute.messages = existingMessages;
            // Also add message to trade timeline for WebSocket broadcast
            if (trade) {
                let timeline = trade.timeline || [];
                if (typeof timeline === "string") {
                    try {
                        timeline = JSON.parse(timeline);
                    }
                    catch (e) {
                        timeline = [];
                    }
                }
                if (!Array.isArray(timeline)) {
                    timeline = [];
                }
                timeline.push({
                    id: messageId,
                    event: "MESSAGE",
                    message: sanitizedMessage,
                    senderId: user.id,
                    senderName: `${user.firstName} ${user.lastName}`,
                    isAdminMessage: true,
                    createdAt: messageTimestamp,
                });
                await trade.update({ timeline }, { transaction });
                // Broadcast the message via WebSocket
                broadcastP2PTradeEvent(trade.id, {
                    type: "MESSAGE",
                    data: {
                        id: messageId,
                        message: sanitizedMessage,
                        senderId: user.id,
                        senderName: `${user.firstName} ${user.lastName}`,
                        isAdminMessage: true,
                        createdAt: messageTimestamp,
                    },
                });
                // Notify users about admin message
                notifyTradeEvent(trade.id, "ADMIN_MESSAGE", {
                    buyerId: trade.buyerId,
                    sellerId: trade.sellerId,
                    amount: trade.amount,
                    currency: ((_b = trade.offer) === null || _b === void 0 ? void 0 : _b.currency) || trade.currency,
                    message: sanitizedMessage,
                }).catch((err) => console_1.logger.error("P2P_DISPUTE", `Notification error: ${err}`));
            }
        }
        await dispute.save({ transaction });
        ctx === null || ctx === void 0 ? void 0 : ctx.step("Logging activity");
        // Log activity
        await db_1.models.p2pActivityLog.create({
            userId: user.id,
            type: "ADMIN_DISPUTE_UPDATE",
            action: "ADMIN_DISPUTE_UPDATE",
            relatedEntity: "DISPUTE",
            relatedEntityId: dispute.id,
            details: JSON.stringify({
                status: dispute.status,
                hasResolution: !!resolution,
                resolution: resolution === null || resolution === void 0 ? void 0 : resolution.outcome,
                hasMessage: !!message,
                tradeUpdated,
                fundsHandled,
                adminId: user.id,
                adminName: `${user.firstName} ${user.lastName}`,
            }),
        }, { transaction });
        // Log admin action
        await (0, ownership_1.logP2PAdminAction)(user.id, "DISPUTE_UPDATE", "DISPUTE", dispute.id, {
            status: status || dispute.status,
            hasResolution: !!resolution,
            resolution: resolution === null || resolution === void 0 ? void 0 : resolution.outcome,
            hasMessage: !!message,
            tradeUpdated,
            fundsHandled,
            adminName: `${user.firstName} ${user.lastName}`,
        });
        await transaction.commit();
        ctx === null || ctx === void 0 ? void 0 : ctx.step("Broadcasting updates");
        // Broadcast WebSocket event if trade was updated
        if (tradeUpdated && trade) {
            const finalStatus = (resolution === null || resolution === void 0 ? void 0 : resolution.outcome) === "BUYER_WINS" || (resolution === null || resolution === void 0 ? void 0 : resolution.outcome) === "SPLIT"
                ? "COMPLETED"
                : "CANCELLED";
            broadcastP2PTradeEvent(trade.id, {
                type: "STATUS_CHANGE",
                data: {
                    status: finalStatus,
                    previousStatus: "DISPUTED",
                    disputeResolved: true,
                    resolution: resolution === null || resolution === void 0 ? void 0 : resolution.outcome,
                },
            });
            // Send notification about resolution
            notifyTradeEvent(trade.id, finalStatus === "COMPLETED" ? "TRADE_COMPLETED" : "TRADE_CANCELLED", {
                buyerId: trade.buyerId,
                sellerId: trade.sellerId,
                amount: trade.amount,
                currency: ((_c = trade.offer) === null || _c === void 0 ? void 0 : _c.currency) || trade.currency,
                disputeResolved: true,
                resolution: resolution === null || resolution === void 0 ? void 0 : resolution.outcome,
            }).catch((err) => console_1.logger.error("P2P_DISPUTE", `Trade notification error: ${err}`));
        }
        // Reload dispute with all associations for proper response
        const updatedDispute = await db_1.models.p2pDispute.findByPk(id, {
            include: [
                {
                    model: db_1.models.p2pTrade,
                    as: "trade",
                    include: [
                        {
                            model: db_1.models.p2pOffer,
                            as: "offer",
                            attributes: ["id", "type", "currency", "walletType"],
                        },
                        {
                            model: db_1.models.user,
                            as: "buyer",
                            attributes: ["id", "firstName", "lastName", "email", "avatar"],
                        },
                        {
                            model: db_1.models.user,
                            as: "seller",
                            attributes: ["id", "firstName", "lastName", "email", "avatar"],
                        },
                    ],
                },
                {
                    model: db_1.models.user,
                    as: "reportedBy",
                    attributes: ["id", "firstName", "lastName", "email", "avatar"],
                },
                {
                    model: db_1.models.user,
                    as: "against",
                    attributes: ["id", "firstName", "lastName", "email", "avatar"],
                },
            ],
        });
        const plainDispute = (updatedDispute === null || updatedDispute === void 0 ? void 0 : updatedDispute.get({ plain: true })) || dispute.toJSON();
        // Transform messages for frontend compatibility
        const messages = Array.isArray(plainDispute.messages) ? plainDispute.messages.map((msg) => ({
            id: msg.id || `${msg.createdAt}-${msg.sender}`,
            sender: msg.senderName || msg.sender || "Unknown",
            senderId: msg.sender,
            content: msg.content || msg.message || "",
            timestamp: msg.createdAt || msg.timestamp,
            isAdmin: msg.isAdmin || false,
            avatar: msg.avatar,
            senderInitials: msg.senderName ? msg.senderName.split(" ").map((n) => n[0]).join("").toUpperCase() : "?",
        })) : [];
        // Transform admin notes from activityLog
        const activityLog = Array.isArray(plainDispute.activityLog) ? plainDispute.activityLog : [];
        const adminNotes = activityLog
            .filter((entry) => entry.type === "note")
            .map((entry) => ({
            content: entry.content || entry.note,
            createdAt: entry.createdAt,
            createdBy: entry.adminName || "Admin",
            adminId: entry.adminId,
        }));
        // Transform evidence for frontend compatibility
        const evidence = Array.isArray(plainDispute.evidence) ? plainDispute.evidence.map((e) => ({
            ...e,
            submittedBy: e.submittedBy || "admin",
            timestamp: e.createdAt || e.timestamp,
        })) : [];
        ctx === null || ctx === void 0 ? void 0 : ctx.success("Dispute updated successfully");
        return {
            ...plainDispute,
            messages,
            adminNotes,
            evidence,
        };
    }
    catch (err) {
        await transaction.rollback();
        if (err.statusCode) {
            throw err;
        }
        ctx === null || ctx === void 0 ? void 0 : ctx.fail("Failed to update dispute");
        throw (0, error_1.createError)({
            statusCode: 500,
            message: "Internal Server Error: " + err.message,
        });
    }
};
