//
//  MBIMDeleteConversationOperation.m
//  kordophoned
//
//  Created by James Magahern on 5/25/22.
//  Copyright © 2022 James Magahern. All rights reserved.
//

#import "MBIMDeleteConversationOperation.h"
#import "IMChat+Encoded.h"

#import "IMCore_ClassDump.h"

@implementation MBIMDeleteConversationOperation
+ (void)load { [super load]; }

+ (NSString *)endpointName
{
    return @"delete";
}

- (void)main
{
    __block NSObject<HTTPResponse> *response = nil;
    do {
        NSString *guid = [self valueForQueryItemWithName:@"guid"];
        
        if (!guid) {
            MBIMLogInfo(@"No conversation GUID provided.");
            response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
            break;
        }
        
        dispatch_sync([[self class] sharedIMAccessQueue], ^{
            IMChat *chat = [[IMChatRegistry sharedInstance] existingChatWithGUID:guid];
            if (!chat) {
                MBIMLogInfo(@"Chat with guid: %@ not found", guid);
                response = [[HTTPErrorResponse alloc] initWithErrorCode:500];
            } else {
                [chat remove];
            }
        });
        
        response = [[HTTPErrorResponse alloc] initWithErrorCode:200];
    } while (0);
    
    self.serverCompletionBlock(response);
}
@end
