import { describe, it, expect } from 'vitest';
import {
  resolveChannelByPackage,
  resolveChannelByName,
  BUILTIN_CHANNELS,
  type ChannelConfig,
} from '../src/domain/platform/channelConfig';

describe('resolveChannelByPackage', () => {
  const customChannels: ChannelConfig[] = [
    {
      id: 'custom_bank',
      name: 'CustomBank',
      defaultAccount: 'Assets:CustomBank',
      packageNames: ['com.custom.bank'],
    },
  ];

  it('finds built-in channel by package name', () => {
    const result = resolveChannelByPackage('com.eg.android.AlipayGphone', []);
    expect(result).toBeDefined();
    expect(result?.name).toBe('Alipay');
    expect(result?.defaultAccount).toBe('Assets:支付宝余额');
  });

  it('finds WeChat channel', () => {
    const result = resolveChannelByPackage('com.tencent.mm', []);
    expect(result).toBeDefined();
    expect(result?.name).toBe('WeChat');
  });

  it('finds Bank channel', () => {
    const result = resolveChannelByPackage('com.unionpay', []);
    expect(result).toBeDefined();
    expect(result?.name).toBe('Bank');
  });

  it('prefers user channel over built-in', () => {
    const result = resolveChannelByPackage('com.eg.android.AlipayGphone', customChannels);
    // Custom channels don't have Alipay package, so falls back to built-in
    expect(result?.name).toBe('Alipay');
  });

  it('finds user custom channel', () => {
    const result = resolveChannelByPackage('com.custom.bank', customChannels);
    expect(result).toBeDefined();
    expect(result?.name).toBe('CustomBank');
    expect(result?.defaultAccount).toBe('Assets:CustomBank');
  });

  it('returns undefined for unknown package', () => {
    const result = resolveChannelByPackage('com.unknown.app', []);
    expect(result).toBeUndefined();
  });

  it('returns undefined for empty string', () => {
    const result = resolveChannelByPackage('', []);
    expect(result).toBeUndefined();
  });
});

describe('resolveChannelByName', () => {
  const customChannels: ChannelConfig[] = [
    {
      id: 'custom_channel',
      name: 'MyWallet',
      defaultAccount: 'Assets:MyWallet',
    },
  ];

  it('finds built-in channel by name', () => {
    const result = resolveChannelByName('Alipay', []);
    expect(result).toBeDefined();
    expect(result?.id).toBe('ch_alipay');
  });

  it('finds channel case-insensitively', () => {
    const result = resolveChannelByName('alipay', []);
    expect(result).toBeDefined();
    expect(result?.id).toBe('ch_alipay');
  });

  it('finds WeChat by name', () => {
    const result = resolveChannelByName('WeChat', []);
    expect(result).toBeDefined();
    expect(result?.id).toBe('ch_wechat');
  });

  it('prefers user channel over built-in', () => {
    const result = resolveChannelByName('MyWallet', customChannels);
    expect(result).toBeDefined();
    expect(result?.id).toBe('custom_channel');
  });

  it('falls back to built-in if user channel not found', () => {
    const result = resolveChannelByName('Bank', customChannels);
    expect(result).toBeDefined();
    expect(result?.id).toBe('ch_bank');
  });

  it('returns undefined for unknown name', () => {
    const result = resolveChannelByName('UnknownChannel', []);
    expect(result).toBeUndefined();
  });
});

describe('BUILTIN_CHANNELS', () => {
  it('has Alipay channel', () => {
    const alipay = BUILTIN_CHANNELS.find(ch => ch.name === 'Alipay');
    expect(alipay).toBeDefined();
    expect(alipay?.packageNames).toContain('com.eg.android.AlipayGphone');
  });

  it('has WeChat channel', () => {
    const wechat = BUILTIN_CHANNELS.find(ch => ch.name === 'WeChat');
    expect(wechat).toBeDefined();
    expect(wechat?.packageNames).toContain('com.tencent.mm');
  });

  it('has Bank channel', () => {
    const bank = BUILTIN_CHANNELS.find(ch => ch.name === 'Bank');
    expect(bank).toBeDefined();
    expect(bank?.packageNames).toContain('com.unionpay');
  });
});
