import { describe, expect, it } from 'vitest';
import {
  buildPackageComparisonRows,
  classifyPackageChange,
  getEnabledPackageFeatures,
  selectRecommendedPackageId,
} from '../lib/packageShop';

describe('classifyPackageChange', () => {
  const active = {
    id: 1,
    price: 200,
    max_photos: 100,
    max_guests: 50,
    gallery_days: 30,
    features: { advanced_analytics: false },
  } as any;

  it('returns neutral when no active package', () => {
    expect(classifyPackageChange(active, null)).toEqual({ isUpgrade: false, isDowngrade: false });
  });

  it('marks upgrade when candidate adds features', () => {
    const candidate = { ...active, id: 2, price: 150, features: { advanced_analytics: true } } as any;
    expect(classifyPackageChange(candidate, active)).toEqual({ isUpgrade: true, isDowngrade: false });
  });

  it('marks downgrade when candidate removes features or limits', () => {
    const candidate = { ...active, id: 3, max_photos: 50, features: { advanced_analytics: false } } as any;
    expect(classifyPackageChange(candidate, active)).toEqual({ isUpgrade: false, isDowngrade: true });
  });

  it('treats mixed changes as downgrade', () => {
    const candidate = { ...active, id: 4, max_photos: 200, gallery_days: 10, features: { advanced_analytics: false } } as any;
    expect(classifyPackageChange(candidate, active)).toEqual({ isUpgrade: false, isDowngrade: true });
  });
});

describe('selectRecommendedPackageId', () => {
  const packages = [
    { id: 1, price: 100, features: { advanced_analytics: false } },
    { id: 2, price: 150, features: { advanced_analytics: true } },
    { id: 3, price: 200, features: { advanced_analytics: true } },
  ] as any;

  it('returns null when no feature is requested', () => {
    expect(selectRecommendedPackageId(packages, null, 100)).toBeNull();
  });

  it('selects the cheapest upgrade with the feature', () => {
    const active = { id: 10, price: 120, max_photos: 100, max_guests: 50, gallery_days: 30, features: {} } as any;
    expect(selectRecommendedPackageId(packages, 'advanced_analytics', active)).toBe(2);
  });

  it('falls back to cheapest feature package if no upgrades exist', () => {
    const active = { id: 10, price: 250, max_photos: 999, max_guests: 999, gallery_days: 365, features: { advanced_analytics: true } } as any;
    expect(selectRecommendedPackageId(packages, 'advanced_analytics', active)).toBe(2);
  });

  it('selects the cheapest package with watermark access when requested', () => {
    const watermarkPackages = [
      { id: 1, price: 100, watermark_allowed: false, features: {} },
      { id: 2, price: 120, watermark_allowed: true, features: {} },
      { id: 3, price: 180, watermark_allowed: true, features: {} },
    ] as any;
    const active = { id: 1, price: 100, watermark_allowed: false, features: {} } as any;
    expect(selectRecommendedPackageId(watermarkPackages, 'watermark_allowed', active)).toBe(2);
  });
});

describe('buildPackageComparisonRows', () => {
  it('includes limit rows and enabled feature rows', () => {
    const rows = buildPackageComparisonRows([
      { features: { advanced_analytics: true, custom_branding: false } },
      { features: { custom_branding: true, watermark_removal: true } },
    ] as any);

    expect(rows.map((row) => row.id)).toEqual([
      'limit.max_photos',
      'limit.max_guests',
      'limit.gallery_days',
      'feature.advanced_analytics',
      'feature.custom_branding',
      'feature.watermark_removal',
    ]);
  });
});

describe('getEnabledPackageFeatures', () => {
  it('accepts array payloads', () => {
    expect(getEnabledPackageFeatures({ features: ['custom_branding', ''] } as any)).toEqual(['custom_branding']);
  });
});
