import { describe, test, expect } from 'vitest';
import { parseChannelList, parseChannelImport } from './channels.js';

// Mirrors the client's Google-Sheets export: junk/instruction rows (one with a
// quoted multi-line cell), a header row, then data rows whose channel handle is
// in 채널명/링크 columns — while OTHER columns hold post counts, 떡상 counts, and
// per-reel URLs that must NOT be imported as channels.
const SHEET_CSV = `,인스타그램 벤치마킹 채널 리스트,,,,,,,,,,
,,,,,,,,,,,
작업 설명,,"더블클릭 후
셀 안에 붙여넣기",최근 2주 활동만,,,,,,,,
작업자,날짜,채널명,링크,팔로워수,게시물수,대 카테고리(소 카테고리),수집추가,특이사항,최근 2주간 떡상 영상 갯수,1,2
,4/15,shopping_flavorr,https://www.instagram.com/shopping_flavorr/reels/,10.6만,1953,생활잡화,,,15,https://www.instagram.com/reel/ABC123/,https://www.instagram.com/reel/DEF456/
,,interior__home,https://www.instagram.com/interior__home_/reels/,14.9만,286,살림/요리,,,7,https://www.instagram.com/reel/GHI789/,
,,tem.duck,https://www.instagram.com/tem.duck/reels/,24.1만,620,생활잡화,,,16,,`;

describe('parseChannelImport', () => {
  test('structured sheet: extracts handles only from 채널명/링크 columns, with category', () => {
    expect(parseChannelImport(SHEET_CSV)).toEqual([
      { username: 'shopping_flavorr', category: '생활잡화' },
      { username: 'interior__home_', category: '살림/요리' }, // 링크 column wins over typo in 채널명
      { username: 'tem.duck', category: '생활잡화' },
    ]);
  });

  test('structured sheet: numeric cells (post counts, 떡상 counts, reel URLs) are NOT imported', () => {
    const names = parseChannelImport(SHEET_CSV).map((c) => c.username);
    for (const junk of ['1953', '286', '620', '15', '7', '16', '1', '2', 'reel', 'abc123']) {
      expect(names).not.toContain(junk);
    }
  });

  test('structured sheet: dedupes across rows (keeps first category)', () => {
    const csv = `작업자,채널명,링크,대 카테고리(소 카테고리)
,foo,https://www.instagram.com/foo/,A
,foo,https://www.instagram.com/foo/reels/,B`;
    expect(parseChannelImport(csv)).toEqual([{ username: 'foo', category: 'A' }]);
  });

  test('plain paste (no header) falls back to loose parsing with null category', () => {
    expect(parseChannelImport('@tem.duck\nfoo_bar, baz')).toEqual([
      { username: 'tem.duck', category: null },
      { username: 'foo_bar', category: null },
      { username: 'baz', category: null },
    ]);
  });
});

describe('parseChannelList', () => {
  test('parses @handles, URLs, and bare names across newlines/commas', () => {
    const raw = '@tem.duck\nhttps://www.instagram.com/foo_bar/\nBAZ, qux';
    expect(parseChannelList(raw)).toEqual(['tem.duck', 'foo_bar', 'baz', 'qux']);
  });
  test('dedupes case-insensitively', () => {
    expect(parseChannelList('Abc\nabc\n@ABC')).toEqual(['abc']);
  });
  test('drops invalid tokens and reserved IG paths', () => {
    expect(parseChannelList('has space\ninstagram.com/p/XYZ/\ninstagram.com/reel/AA/\nok_name'))
      .toEqual(['ok_name']);
  });
  test('splits tab-separated tokens (Excel multi-column paste)', () => {
    expect(parseChannelList('@alpha\tbeta\tgamma')).toEqual(['alpha', 'beta', 'gamma']);
  });
  test('a free-text line with spaces stays one token (not fragmented)', () => {
    expect(parseChannelList('my channel list\nok_name')).toEqual(['ok_name']);
  });
  test('empty input → empty array', () => {
    expect(parseChannelList('   \n , ')).toEqual([]);
  });
});
