import { describe, expect, it, vi } from 'vitest';
import { configSchema } from '../config/schema.js';
import { ChannelRegistry } from '../channels/index.js';
import { HookEngine } from '../hooks/index.js';
import { registerChannels } from './channels.js';

describe('registerChannels', () => {
  it('registers Mattermost adapter when configured', () => {
    const config = configSchema.parse({
      telegram: { bot_token: 'test-token', allowed_chat_ids: [1] },
      models: { default: { provider: 'anthropic', model: 'claude-3' } },
      mattermost: {
        server_url: 'https://mattermost.example.com',
        bot_token: 'mm-token',
        allowed_channel_ids: ['chan-1'],
      },
    });

    const channelRegistry = new ChannelRegistry();
    const gateway = {
      setWebhookHandler: vi.fn(),
      setGmailHandler: vi.fn(),
      setTeamsHandler: vi.fn(),
      setGoogleChatHandler: vi.fn(),
      setBlueBubblesHandler: vi.fn(),
      setLineHandler: vi.fn(),
      setFeishuHandler: vi.fn(),
      setZaloHandler: vi.fn(),
    };

    registerChannels({
      config,
      channelRegistry,
      hookEngine: new HookEngine(config.hooks),
      gateway: gateway as unknown as Parameters<typeof registerChannels>[0]['gateway'],
    });

    const names = channelRegistry.list().map((adapter) => adapter.name);
    expect(names).toContain('mattermost');
    expect(names).toContain('webchat');
  });

  it('registers LINE adapter when configured', () => {
    const config = configSchema.parse({
      telegram: { bot_token: 'test-token', allowed_chat_ids: [1] },
      models: { default: { provider: 'anthropic', model: 'claude-3' } },
      line: {
        channel_access_token: 'line-token',
        channel_secret: 'line-secret',
        allowed_source_ids: ['U123'],
      },
    });

    const channelRegistry = new ChannelRegistry();
    const gateway = {
      setWebhookHandler: vi.fn(),
      setGmailHandler: vi.fn(),
      setTeamsHandler: vi.fn(),
      setGoogleChatHandler: vi.fn(),
      setBlueBubblesHandler: vi.fn(),
      setLineHandler: vi.fn(),
      setFeishuHandler: vi.fn(),
      setZaloHandler: vi.fn(),
    };

    registerChannels({
      config,
      channelRegistry,
      hookEngine: new HookEngine(config.hooks),
      gateway: gateway as unknown as Parameters<typeof registerChannels>[0]['gateway'],
    });

    const names = channelRegistry.list().map((adapter) => adapter.name);
    expect(names).toContain('line');
    expect(gateway.setLineHandler).toHaveBeenCalledTimes(1);
  });

  it('registers Feishu adapter when configured', () => {
    const config = configSchema.parse({
      telegram: { bot_token: 'test-token', allowed_chat_ids: [1] },
      models: { default: { provider: 'anthropic', model: 'claude-3' } },
      feishu: {
        app_id: 'cli_a1b2c3',
        app_secret: 'secret',
        allowed_chat_ids: ['oc_123'],
      },
    });

    const channelRegistry = new ChannelRegistry();
    const gateway = {
      setWebhookHandler: vi.fn(),
      setGmailHandler: vi.fn(),
      setTeamsHandler: vi.fn(),
      setGoogleChatHandler: vi.fn(),
      setBlueBubblesHandler: vi.fn(),
      setLineHandler: vi.fn(),
      setFeishuHandler: vi.fn(),
    };

    registerChannels({
      config,
      channelRegistry,
      hookEngine: new HookEngine(config.hooks),
      gateway: gateway as unknown as Parameters<typeof registerChannels>[0]['gateway'],
    });

    const names = channelRegistry.list().map((adapter) => adapter.name);
    expect(names).toContain('feishu');
    expect(gateway.setFeishuHandler).toHaveBeenCalledTimes(1);
  });

  it('registers Zalo adapter when configured', () => {
    const config = configSchema.parse({
      telegram: { bot_token: 'test-token', allowed_chat_ids: [1] },
      models: { default: { provider: 'anthropic', model: 'claude-3' } },
      zalo: {
        oa_access_token: 'oa-token',
        allowed_user_ids: ['uid-1'],
      },
    });

    const channelRegistry = new ChannelRegistry();
    const gateway = {
      setWebhookHandler: vi.fn(),
      setGmailHandler: vi.fn(),
      setTeamsHandler: vi.fn(),
      setGoogleChatHandler: vi.fn(),
      setBlueBubblesHandler: vi.fn(),
      setLineHandler: vi.fn(),
      setFeishuHandler: vi.fn(),
      setZaloHandler: vi.fn(),
    };

    registerChannels({
      config,
      channelRegistry,
      hookEngine: new HookEngine(config.hooks),
      gateway: gateway as unknown as Parameters<typeof registerChannels>[0]['gateway'],
    });

    const names = channelRegistry.list().map((adapter) => adapter.name);
    expect(names).toContain('zalo');
    expect(gateway.setZaloHandler).toHaveBeenCalledTimes(1);
  });

  it('registers cron scheduler when daily briefing preset is enabled', () => {
    const config = configSchema.parse({
      telegram: { bot_token: 'test-token', allowed_chat_ids: [1] },
      models: { default: { provider: 'anthropic', model: 'claude-3' } },
      automation: {
        daily_briefing: {
          enabled: true,
          schedule: '0 8 * * *',
          output: { channel: 'telegram', peer: '1' },
        },
      },
    });

    const channelRegistry = new ChannelRegistry();
    const gateway = {
      setWebhookHandler: vi.fn(),
      setGmailHandler: vi.fn(),
      setTeamsHandler: vi.fn(),
      setGoogleChatHandler: vi.fn(),
      setBlueBubblesHandler: vi.fn(),
      setLineHandler: vi.fn(),
      setFeishuHandler: vi.fn(),
      setZaloHandler: vi.fn(),
    };

    registerChannels({
      config,
      channelRegistry,
      hookEngine: new HookEngine(config.hooks),
      gateway: gateway as unknown as Parameters<typeof registerChannels>[0]['gateway'],
    });

    const names = channelRegistry.list().map((adapter) => adapter.name);
    expect(names).toContain('cron');
  });
});
