using MyTelegram.Handlers.Messages;
using MyTelegram.Schema.Messages;

namespace MyTelegram.Messenger.Handlers.LatestLayer.Impl.Messages;

///<summary>
/// Obtains information about a chat export file, generated by a foreign chat app, <a href="https://corefork.telegram.org/api/import">click here for more info about imported chats »</a>.
/// <para>Possible errors</para>
/// Code Type Description
/// 400 IMPORT_FORMAT_UNRECOGNIZED The specified chat export file was exported from an unsupported chat app.
/// See <a href="https://corefork.telegram.org/method/messages.checkHistoryImport" />
///</summary>
internal sealed class CheckHistoryImportHandler : RpcResultObjectHandler<MyTelegram.Schema.Messages.RequestCheckHistoryImport, MyTelegram.Schema.Messages.IHistoryImportParsed>,
    Messages.ICheckHistoryImportHandler
{
    protected override Task<MyTelegram.Schema.Messages.IHistoryImportParsed> HandleCoreAsync(IRequestInput input,
        MyTelegram.Schema.Messages.RequestCheckHistoryImport obj)
    {
        if (string.IsNullOrEmpty(obj.ImportHead))
        {
            RpcErrors.RpcErrors400.ImportFileInvalid.ThrowRpcError();
        }

        // Basic validation: Check if it looks like a text export
        // In a real scenario, we'd check for specific headers from WhatsApp/Line/etc.
        // For now, we accept any non-empty string as a "Generic" import.
        
        return Task.FromResult<MyTelegram.Schema.Messages.IHistoryImportParsed>(new THistoryImportParsed
        {
            Pm = true, 
            Title = "Imported Chat" 
        });
    }
}
