"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.metadata = void 0;
const db_1 = require("@b/db");
const error_1 = require("@b/utils/error");
const errors_1 = require("@b/utils/schema/errors");
exports.metadata = {
    summary: "Add admin note to P2P dispute",
    description: "Adds an internal admin note to a P2P dispute activity log. Notes are timestamped and attributed to the admin user for tracking purposes.",
    operationId: "addNoteToAdminP2PDispute",
    tags: ["Admin", "P2P", "Dispute"],
    requiresAuth: true,
    permissions: ["p2p.dispute.note.add"],
    logModule: "ADMIN_P2P",
    logTitle: "Add note to dispute",
    parameters: [
        {
            index: 0,
            name: "id",
            in: "path",
            description: "Dispute ID",
            required: true,
            schema: { type: "string" },
        },
    ],
    requestBody: {
        description: "Note data",
        required: true,
        content: {
            "application/json": {
                schema: {
                    type: "object",
                    properties: {
                        note: { type: "string" },
                    },
                    required: ["note"],
                },
            },
        },
    },
    responses: {
        200: { description: "Note added successfully." },
        401: errors_1.unauthorizedResponse,
        404: (0, errors_1.notFoundResponse)("Resource"),
        500: errors_1.serverErrorResponse,
    },
    permission: "edit.p2p.dispute",
};
exports.default = async (data) => {
    const { params, body, user, ctx } = data;
    const { id } = params;
    const { note } = body;
    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: ["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"],
                },
            ],
        });
        if (!dispute) {
            ctx === null || ctx === void 0 ? void 0 : ctx.fail("Dispute not found");
            throw (0, error_1.createError)({ statusCode: 404, message: "Dispute not found" });
        }
        ctx === null || ctx === void 0 ? void 0 : ctx.step("Adding note to activity log");
        let existingNotes = dispute.activityLog;
        if (!Array.isArray(existingNotes)) {
            existingNotes = [];
        }
        const adminName = user.firstName && user.lastName
            ? `${user.firstName} ${user.lastName}`
            : user.email || "Admin";
        existingNotes.push({
            type: "note",
            content: note,
            createdAt: new Date().toISOString(),
            adminId: user.id,
            adminName,
        });
        ctx === null || ctx === void 0 ? void 0 : ctx.step("Updating dispute");
        // Use update to ensure Sequelize detects the change to JSON field
        await dispute.update({ activityLog: existingNotes });
        // Reload the dispute to get fresh data
        await dispute.reload();
        const plainDispute = dispute.get({ plain: true });
        // 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 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 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("Note added successfully");
        return {
            ...plainDispute,
            messages,
            adminNotes,
            evidence,
        };
    }
    catch (err) {
        if (err.statusCode) {
            throw err;
        }
        ctx === null || ctx === void 0 ? void 0 : ctx.fail("Failed to add note");
        throw (0, error_1.createError)({
            statusCode: 500,
            message: "Internal Server Error: " + err.message,
        });
    }
};
