import { beforeEach, describe, expect, it, vi } from 'vitest';

vi.mock('../../../../shared/state', async () => {
  const navigate = vi.fn();
  const setState = vi.fn();
  const sendMessage = vi.fn();
  const getState = vi.fn(() => ({
    view: 'add', entries: [], selectedId: null, selectedItem: null, selectedIndex: 0,
    searchQuery: '', activeGroup: null, error: null, loading: false,
    capturedTabId: null, capturedUrl: '', newType: 'identity',
  }));
  const escapeHtml = (s: string) => s
    .replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;').replace(/'/g, '&#39;');
  return { navigate, setState, sendMessage, getState, escapeHtml, popOutToTab: vi.fn(), isInTab: vi.fn(() => false), openVaultTab: vi.fn() };
});

import { renderForm } from '../identity';
import { sendMessage } from '../../../../shared/state';

describe('Identity save shape', () => {
  beforeEach(() => {
    document.body.innerHTML = '<div id="app"></div>';
    vi.mocked(sendMessage).mockReset();
    vi.mocked(sendMessage).mockResolvedValue({ ok: true, data: { id: 'fakeid0000000000', items: [] } });
  });

  it('builds an Item with all populated fields and undefined for blanks', async () => {
    const app = document.getElementById('app')!;
    renderForm(app, 'add', null);

    (document.getElementById('f-title') as HTMLInputElement).value = 'Aaron Lee · personal';
    (document.getElementById('f-full-name') as HTMLInputElement).value = 'Aaron Lee';
    (document.getElementById('f-email') as HTMLInputElement).value = 'aaron@example.com';
    (document.getElementById('f-phone') as HTMLInputElement).value = '+1 555 0100';
    (document.getElementById('f-address') as HTMLTextAreaElement).value = '1 Main St\nSpringfield';
    (document.getElementById('f-dob') as HTMLInputElement).value = '1985-05-23';

    document.getElementById('save-btn')!.click();
    await new Promise(r => setTimeout(r, 5));

    const calls = vi.mocked(sendMessage).mock.calls;
    const addCall = calls.find(([msg]) => (msg as { type: string }).type === 'add_item');
    const msg = addCall![0] as { type: 'add_item'; item: any };
    expect(msg.item.type).toBe('identity');
    expect(msg.item.core).toEqual({
      type: 'identity',
      full_name: 'Aaron Lee',
      email: 'aaron@example.com',
      phone: '+1 555 0100',
      address: '1 Main St\nSpringfield',
      date_of_birth: '1985-05-23',
    });
  });

  it('leaves empty fields out of core entirely', async () => {
    const app = document.getElementById('app')!;
    renderForm(app, 'add', null);

    (document.getElementById('f-title') as HTMLInputElement).value = 'name only';
    (document.getElementById('f-full-name') as HTMLInputElement).value = 'Bob';
    // Other fields left blank.

    document.getElementById('save-btn')!.click();
    await new Promise(r => setTimeout(r, 5));

    const calls = vi.mocked(sendMessage).mock.calls;
    const addCall = calls.find(([msg]) => (msg as { type: string }).type === 'add_item');
    const msg = addCall![0] as { type: 'add_item'; item: any };
    expect(msg.item.core.full_name).toBe('Bob');
    expect(msg.item.core.email).toBeUndefined();
    expect(msg.item.core.phone).toBeUndefined();
    expect(msg.item.core.address).toBeUndefined();
    expect(msg.item.core.date_of_birth).toBeUndefined();
  });
});
