import { describe, expect, it } from 'bun:test';
import {
  addressToCellPosition,
  modelWorkbookToUniverSnapshot,
  univerSnapshotToModelWorkbook,
} from './modelWorkbookAdapter';
import type { ModelWorkbook } from '../types/research';

describe('modelWorkbookAdapter', () => {
  it('round trips app workbook cells through a Univer snapshot', () => {
    const workbook: ModelWorkbook = {
      version: 1,
      activeSheetId: 'dcf',
      sheets: [
        {
          id: 'dcf',
          name: 'DCF',
          cells: {
            A1: { value: 'Revenue' },
            B1: { value: 100, format: 'currency', cellId: 'dcf-b1' },
            C1: { formula: '=SUM(B1:B2)' },
          },
        },
        {
          id: 'balance_sheet',
          name: 'Balance Sheet',
          cells: {
            A1: { value: 'Cash' },
            B1: { formula: "='DCF'!B1" },
          },
        },
      ],
    };

    const snapshot = modelWorkbookToUniverSnapshot(workbook, {
      'dcf-b1': 'single_source',
    });
    const roundTripped = univerSnapshotToModelWorkbook(snapshot, workbook);

    expect(roundTripped.sheets[0]?.cells.A1.value).toBe('Revenue');
    expect(roundTripped.sheets[0]?.cells.B1.value).toBe(100);
    expect(roundTripped.sheets[0]?.cells.B1.cellId).toBe('dcf-b1');
    expect(roundTripped.sheets[0]?.cells.C1.formula).toBe('=SUM(B1:B2)');
    expect(roundTripped.sheets[1]?.name).toBe('Balance Sheet');
    expect(roundTripped.sheets[1]?.cells.B1.formula).toBe("='DCF'!B1");
    expect(roundTripped.activeSheetId).toBe('dcf');
  });

  it('parses absolute A1 addresses', () => {
    expect(addressToCellPosition('$B$5')).toEqual({ row: 4, column: 1 });
  });
});
