"use strict";Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const cache_1=require("@b/utils/cache"),db_1=require("@b/db"),error_1=require("@b/utils/error"),query_1=require("@b/utils/query"),console_1=require("@b/utils/console");exports.metadata={summary:"Creates a new blog comment",description:"This endpoint creates a new blog comment.",operationId:"createComment",tags:["Blog"],logModule:"BLOG",logTitle:"Create comment",requiresAuth:!0,parameters:[{index:0,name:"postId",in:"path",description:"The ID of the post to comment on",required:!0,schema:{type:"string",description:"Post ID"}}],requestBody:{description:"Data for creating a new comment",content:{"application/json":{schema:{type:"object",properties:{content:{type:"string",description:"Name of the comment to create"}},required:["content"]}}},required:!0},responses:(0,query_1.createRecordResponses)("Comment")};exports.default=async e=>{const{user:t,body:r,params:o,ctx:n}=e;if(!(null==t?void 0:t.id))return(0,error_1.createError)(401,"Unauthorized, permission required to create comments");null==n||n.step("Checking comment moderation settings");const s=cache_1.CacheManager.getInstance(),a=await s.getSettings(),i=a.has("moderateComments")?a.get("moderateComments"):null,c="boolean"==typeof i?i:Boolean(i),{content:m}=r;if(!m)return(0,error_1.createError)(400,"Comment content is required");const{postId:d}=o;try{null==n||n.step("Creating comment");const e=await db_1.models.comment.create({content:m,userId:t.id,postId:d,status:c?"PENDING":"APPROVED"});null==n||n.step("Fetching comment with user details");if(!await db_1.models.comment.findOne({where:{id:e.id},include:[{model:db_1.models.user,as:"user",attributes:["id","firstName","lastName","email","avatar"]}]}))return(0,error_1.createError)(404,"Comment created but not found");null==n||n.success(`Comment created on post ${d} by user ${t.id} - ${c?"pending moderation":"approved"}`);return{message:"Comment created successfully"}}catch(e){console_1.logger.error("BLOG","Failed to create and retrieve comment",e);null==n||n.fail("Failed to create comment");return(0,error_1.createError)(500,"Internal server error")}};