// SPDX-License-Identifier: GPL-3.0-or-later

import { describe, it, expect } from 'vitest';
import {
  formatDateTime,
  estimateMonthlyCost,
  getStatusBadge,
} from './utils';

describe('formatDateTime', () => {
  it('returns dash for undefined', () => {
    expect(formatDateTime(undefined)).toBe('-');
  });

  it('returns dash for empty string', () => {
    // Empty string creates an Invalid Date but toLocaleString still runs
    const result = formatDateTime('');
    // Empty string is falsy, so returns '-'
    expect(result).toBe('-');
  });

  it('formats ISO date string', () => {
    const result = formatDateTime('2025-01-15T10:30:00Z');
    // Should contain date components (locale-dependent format)
    expect(result).toBeTruthy();
    expect(result).not.toBe('-');
    expect(result.length).toBeGreaterThan(5);
  });

  it('formats Date object', () => {
    const date = new Date('2025-06-20T14:00:00Z');
    const result = formatDateTime(date);
    expect(result).toBeTruthy();
    expect(result).not.toBe('-');
  });
});

describe('estimateMonthlyCost', () => {
  const TB = 1024 * 1024 * 1024 * 1024;

  it('returns 0 for 0 bytes', () => {
    expect(estimateMonthlyCost(0)).toBe(0);
  });

  it('calculates cost for exactly 1 TB', () => {
    // 1 credit/TB/month × $9.99 = $9.99
    expect(estimateMonthlyCost(TB)).toBeCloseTo(9.99, 2);
  });

  it('calculates cost for 10 TB', () => {
    // 10 credits × $9.99 = $99.90
    expect(estimateMonthlyCost(10 * TB)).toBeCloseTo(99.90, 1);
  });

  it('rounds up to nearest TB for partial usage', () => {
    // 1.5 TB → ceil(1.5) = 2 credits → $19.98
    expect(estimateMonthlyCost(1.5 * TB)).toBeCloseTo(19.98, 1);
  });

  it('charges minimum 1 credit for any non-zero usage', () => {
    // Even 1 MB rounds up to 1 TB credit = $9.99
    const cost = estimateMonthlyCost(1024 * 1024);
    expect(cost).toBeCloseTo(9.99, 2);
  });
});

describe('getStatusBadge', () => {
  it('returns badge with green for active', () => {
    const badge = getStatusBadge('active');
    expect(badge).toContain('bg-green');
    expect(badge).toContain('rounded-full');
    expect(badge).toContain('text-xs');
  });

  it('returns badge with green for completed', () => {
    expect(getStatusBadge('completed')).toContain('bg-green');
  });

  it('returns badge with green for success', () => {
    expect(getStatusBadge('success')).toContain('bg-green');
  });

  it('returns badge with green for healthy', () => {
    expect(getStatusBadge('healthy')).toContain('bg-green');
  });

  it('returns badge with green for connected', () => {
    expect(getStatusBadge('connected')).toContain('bg-green');
  });

  it('returns badge with blue for pending', () => {
    expect(getStatusBadge('pending')).toContain('bg-blue');
  });

  it('returns badge with blue for running', () => {
    expect(getStatusBadge('running')).toContain('bg-blue');
  });

  it('returns badge with blue for in_progress', () => {
    expect(getStatusBadge('in_progress')).toContain('bg-blue');
  });

  it('returns badge with yellow for warning', () => {
    expect(getStatusBadge('warning')).toContain('bg-yellow');
  });

  it('returns badge with yellow for waiting', () => {
    expect(getStatusBadge('waiting')).toContain('bg-yellow');
  });

  it('returns badge with red for error', () => {
    expect(getStatusBadge('error')).toContain('bg-red');
  });

  it('returns badge with red for failed', () => {
    expect(getStatusBadge('failed')).toContain('bg-red');
  });

  it('returns badge with red for cancelled', () => {
    expect(getStatusBadge('cancelled')).toContain('bg-red');
  });

  it('returns badge with slate for unknown', () => {
    expect(getStatusBadge('unknown')).toContain('bg-slate');
  });

  it('is case insensitive', () => {
    expect(getStatusBadge('ACTIVE')).toContain('bg-green');
    expect(getStatusBadge('Running')).toContain('bg-blue');
    expect(getStatusBadge('FAILED')).toContain('bg-red');
  });

  it('always includes base badge classes', () => {
    const statuses = ['active', 'pending', 'warning', 'error', 'unknown'];
    for (const status of statuses) {
      const badge = getStatusBadge(status);
      expect(badge).toContain('inline-flex');
      expect(badge).toContain('items-center');
      expect(badge).toContain('px-2.5');
      expect(badge).toContain('py-0.5');
      expect(badge).toContain('rounded-full');
      expect(badge).toContain('text-xs');
      expect(badge).toContain('font-medium');
    }
  });
});
