import { expect, test } from 'bun:test';

import type { PlanRules } from '@repo/api-contract';

const DEFAULT_PRICE = 29.99;

function makeV1Rules(): PlanRules {
  return {
    version: 'v1',
    limits: { maxCourts: 3, maxBookingsPerDay: 50 },
    policies: { allowOverbooking: false },
    features: {
      onlinePayments: false,
      publicBookingPage: false,
      advancedReports: false,
      whatsappReminders: false,
      fixedSlots: false,
    },
  };
}

function makeV2Rules(overrides?: Record<string, { amount: number; currency: string }>): PlanRules {
  return {
    version: 'v2',
    limits: { maxCourts: 5, maxBookingsPerDay: 100 },
    policies: { allowOverbooking: false },
    features: {
      onlinePayments: true,
      publicBookingPage: true,
      advancedReports: true,
      whatsappReminders: true,
      fixedSlots: true,
    },
    ...(overrides ? { pricing: { overrides } } : {}),
  };
}

const { resolvePlanPrice } = await import('@/modules/plan/services/plan-pricing.service');

test('returns default price in USD when no country code is provided', () => {
  const result = resolvePlanPrice(
    DEFAULT_PRICE,
    makeV2Rules({ AR: { amount: 14999, currency: 'ARS' } })
  );
  expect(result).toEqual({ amount: DEFAULT_PRICE, currency: 'USD' });
});

test('returns overridden price for matching country code', () => {
  const rules = makeV2Rules({ AR: { amount: 14999, currency: 'ARS' } });
  const result = resolvePlanPrice(DEFAULT_PRICE, rules, 'AR');
  expect(result).toEqual({ amount: 14999, currency: 'ARS' });
});

test('returns default price for country without override', () => {
  const rules = makeV2Rules({ AR: { amount: 14999, currency: 'ARS' } });
  const result = resolvePlanPrice(DEFAULT_PRICE, rules, 'CL');
  expect(result).toEqual({ amount: DEFAULT_PRICE, currency: 'USD' });
});

test('returns default price for v1 rules (no pricing config)', () => {
  const result = resolvePlanPrice(DEFAULT_PRICE, makeV1Rules(), 'AR');
  expect(result).toEqual({ amount: DEFAULT_PRICE, currency: 'USD' });
});

test('returns default price for v2 rules with empty overrides', () => {
  const rules = makeV2Rules({});
  const result = resolvePlanPrice(DEFAULT_PRICE, rules, 'AR');
  expect(result).toEqual({ amount: DEFAULT_PRICE, currency: 'USD' });
});

test('returns default price when v2 rules has no pricing section', () => {
  const rules = makeV2Rules();
  const result = resolvePlanPrice(DEFAULT_PRICE, rules, 'AR');
  expect(result).toEqual({ amount: DEFAULT_PRICE, currency: 'USD' });
});

test('selects correct override among multiple countries', () => {
  const rules = makeV2Rules({
    AR: { amount: 24999, currency: 'ARS' },
    BR: { amount: 59.99, currency: 'USD' },
  });

  expect(resolvePlanPrice(DEFAULT_PRICE, rules, 'AR')).toEqual({ amount: 24999, currency: 'ARS' });
  expect(resolvePlanPrice(DEFAULT_PRICE, rules, 'BR')).toEqual({ amount: 59.99, currency: 'USD' });
  expect(resolvePlanPrice(DEFAULT_PRICE, rules, 'US')).toEqual({
    amount: DEFAULT_PRICE,
    currency: 'USD',
  });
});
