import { describe, it, expect } from 'vitest';
import { OutboundAttachmentCollector } from '../../backends/native/attachments.js';
import { createMediaSendTool } from './media-send.js';

describe('media.send tool', () => {
  it('has correct metadata', () => {
    const collector = new OutboundAttachmentCollector();
    const tool = createMediaSendTool(collector);

    expect(tool.name).toBe('media.send');
    expect(tool.inputSchema.required).toEqual(['mime_type']);
  });

  it('queues attachment with base64 data', async () => {
    const collector = new OutboundAttachmentCollector();
    const tool = createMediaSendTool(collector);

    const result = await tool.execute({
      data: 'aGVsbG8=',
      mime_type: 'image/png',
      filename: 'hello.png',
    });

    expect(result.success).toBe(true);
    expect(result.output).toContain('image/png');
    expect(result.output).toContain('hello.png');
    expect(collector.count).toBe(1);

    const drained = collector.drain();
    expect(drained[0]).toEqual({
      mimeType: 'image/png',
      data: 'aGVsbG8=',
      url: undefined,
      filename: 'hello.png',
    });
  });

  it('queues attachment with URL', async () => {
    const collector = new OutboundAttachmentCollector();
    const tool = createMediaSendTool(collector);

    const result = await tool.execute({
      url: 'https://example.com/photo.jpg',
      mime_type: 'image/jpeg',
    });

    expect(result.success).toBe(true);
    expect(result.output).toContain('image/jpeg');
    expect(collector.count).toBe(1);

    const drained = collector.drain();
    expect(drained[0]).toEqual({
      mimeType: 'image/jpeg',
      data: undefined,
      url: 'https://example.com/photo.jpg',
      filename: undefined,
    });
  });

  it('fails when neither data nor url is provided', async () => {
    const collector = new OutboundAttachmentCollector();
    const tool = createMediaSendTool(collector);

    const result = await tool.execute({ mime_type: 'image/png' });

    expect(result.success).toBe(false);
    expect(result.error).toContain('Either data or url must be provided');
    expect(collector.count).toBe(0);
  });

  it('queues multiple attachments', async () => {
    const collector = new OutboundAttachmentCollector();
    const tool = createMediaSendTool(collector);

    await tool.execute({ data: 'img1', mime_type: 'image/png' });
    await tool.execute({ url: 'https://example.com/doc.pdf', mime_type: 'application/pdf' });
    await tool.execute({ data: 'img2', mime_type: 'image/jpeg', filename: 'photo.jpg' });

    expect(collector.count).toBe(3);

    const drained = collector.drain();
    expect(drained).toHaveLength(3);
    expect(drained[0].mimeType).toBe('image/png');
    expect(drained[1].mimeType).toBe('application/pdf');
    expect(drained[2].mimeType).toBe('image/jpeg');
    expect(drained[2].filename).toBe('photo.jpg');
  });

  it('collector drains correctly after tool use', async () => {
    const collector = new OutboundAttachmentCollector();
    const tool = createMediaSendTool(collector);

    await tool.execute({ data: 'abc', mime_type: 'image/png' });
    expect(collector.count).toBe(1);

    const first = collector.drain();
    expect(first).toHaveLength(1);
    expect(collector.count).toBe(0);

    // Second drain should be empty
    const second = collector.drain();
    expect(second).toHaveLength(0);
  });

  it('output includes filename when provided', async () => {
    const collector = new OutboundAttachmentCollector();
    const tool = createMediaSendTool(collector);

    const result = await tool.execute({
      data: 'abc',
      mime_type: 'application/pdf',
      filename: 'report.pdf',
    });

    expect(result.output).toContain('report.pdf');
  });

  it('output omits filename when not provided', async () => {
    const collector = new OutboundAttachmentCollector();
    const tool = createMediaSendTool(collector);

    const result = await tool.execute({
      data: 'abc',
      mime_type: 'image/png',
    });

    expect(result.output).toBe('Attachment queued (image/png)');
  });
});
