import { describe, it, expect } from 'vitest';
import {
  resolveAttachmentPreviewUrl,
  resolveAttachmentUrl,
} from '@/shared/utils/attachmentUrl';
import type { TicketAttachment } from '@/features/tickets/types/ticket.types';

const base: TicketAttachment = {
  id: 'att-1',
  file_name: 'photo.png',
  file_size: 1024,
  mime_type: 'image/png',
};

describe('attachmentUrl', () => {
  it('uses authenticated download route for storage paths instead of /api/v1/storage', () => {
    const url = resolveAttachmentUrl({
      ...base,
      url: '/storage/attachments/2026/05/12364f5a-48c1-4e1c-a034-2f49dacf8c7b.png',
    });

    expect(url).toBe('/api/v1/attachments/att-1/download');
    expect(url).not.toContain('/api/v1/storage');
  });

  it('appends inline query for image previews', () => {
    const url = resolveAttachmentPreviewUrl({
      ...base,
      url: '/storage/attachments/2026/05/file.png',
    });

    expect(url).toBe('/api/v1/attachments/att-1/download?inline=1');
  });

  it('respects download_url from API when provided', () => {
    const url = resolveAttachmentUrl({
      ...base,
      download_url: '/api/v1/attachments/att-99/download',
      url: '/storage/attachments/ignored.png',
    });

    expect(url).toBe('/api/v1/attachments/att-99/download');
  });

  it('does not double-prefix api paths', () => {
    const url = resolveAttachmentUrl({
      ...base,
      url: '/api/v1/attachments/att-1/download',
    });

    expect(url).toBe('/api/v1/attachments/att-1/download');
  });

  it('normalizes absolute API download_url to a relative path', () => {
    const url = resolveAttachmentUrl({
      ...base,
      download_url: 'https://mvhelpdesk.local:8890/api/v1/attachments/att-99/download',
      url: '/storage/attachments/ignored.png',
    }, { inline: true });

    expect(url).toBe('/api/v1/attachments/att-99/download?inline=1');
  });
});
