import { ChatType, Peer } from '@/core/entities';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import BaseAction from '../BaseAction';
import { ActionName } from '../types';
import { MessageUnique } from '@/common/message-unique';

const SchemaData = {
    type: 'object',
    properties: {
        user_id: { type: ['number', 'string'] },
        group_id: { type: ['number', 'string'] },
        message_id: { type: ['number', 'string'] },
    },
} as const satisfies JSONSchema;

type PlayloadType = FromSchema<typeof SchemaData>;

class MarkMsgAsRead extends BaseAction<PlayloadType, null> {
    async getPeer(payload: PlayloadType): Promise<Peer> {
        if (payload.message_id) {
            const s_peer = MessageUnique.getMsgIdAndPeerByShortId(+payload.message_id)?.Peer;
            if (s_peer) {
                return s_peer;
            }
            const l_peer = MessageUnique.getPeerByMsgId(payload.message_id.toString())?.Peer;
            if (l_peer) {
                return l_peer;
            }
        }
        if (payload.user_id) {
            const peerUid = await this.core.apis.UserApi.getUidByUinV2(payload.user_id.toString());
            if (!peerUid) {
                throw new Error( `私聊${payload.user_id}不存在`);
            }
            const isBuddy = await this.core.apis.FriendApi.isBuddy(peerUid);
            return { chatType: isBuddy ? ChatType.KCHATTYPEC2C : ChatType.KCHATTYPETEMPC2CFROMGROUP, peerUid };
        }
        if (!payload.group_id) {
            throw new Error('缺少参数 group_id 或 user_id');
        }
        return { chatType: ChatType.KCHATTYPEGROUP, peerUid: payload.group_id.toString() };
    }

    async _handle(payload: PlayloadType): Promise<null> {
        const ret = await this.core.apis.MsgApi.setMsgRead(await this.getPeer(payload));
        if (ret.result != 0) {
            throw new Error('设置已读失败,' + ret.errMsg);
        }
        return null;
    }
}

// 以下为非标准实现
export class MarkPrivateMsgAsRead extends MarkMsgAsRead {
    payloadSchema = SchemaData;
    actionName = ActionName.MarkPrivateMsgAsRead;
}

export class MarkGroupMsgAsRead extends MarkMsgAsRead {
    payloadSchema = SchemaData;
    actionName = ActionName.MarkGroupMsgAsRead;
}

export class GoCQHTTPMarkMsgAsRead extends MarkMsgAsRead {
    actionName = ActionName.GoCQHTTP_MarkMsgAsRead;
}

export class MarkAllMsgAsRead extends BaseAction<any, null> {
    actionName = ActionName._MarkAllMsgAsRead;

    async _handle(): Promise<null> {
        await this.core.apis.MsgApi.markAllMsgAsRead();
        return null;
    }
}
