import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { getZaloZnsTemplates } from '../services/zalo-zns-templates';

const ENV_KEYS = [
  'ZALO_ZNS_TEMPLATE_INQUIRY',
  'ZALO_ZNS_TEMPLATE_INQUIRY_REPLY',
  'ZALO_ZNS_TEMPLATE_PAYMENT',
  'ZALO_ZNS_TEMPLATE_LISTING_APPROVED',
  'ZALO_ZNS_TEMPLATE_LISTING_REJECTED',
  'ZALO_ZNS_TEMPLATE_LISTING_SOLD',
  'ZALO_ZNS_TEMPLATE_PRICE_DROP',
  'ZALO_ZNS_TEMPLATE_SUBSCRIPTION_RENEWAL',
];

describe('getZaloZnsTemplates', () => {
  const saved: Record<string, string | undefined> = {};

  beforeEach(() => {
    for (const k of ENV_KEYS) {
      saved[k] = process.env[k];
      delete process.env[k];
    }
  });

  afterEach(() => {
    for (const k of ENV_KEYS) {
      if (saved[k] === undefined) delete process.env[k];
      else process.env[k] = saved[k];
    }
  });

  it('returns an empty map when no template env vars are configured', () => {
    expect(getZaloZnsTemplates()).toEqual({});
  });

  it('registers R8.2 templates when their env vars are present', () => {
    process.env['ZALO_ZNS_TEMPLATE_INQUIRY_REPLY'] = 'tpl-inquiry-reply';
    process.env['ZALO_ZNS_TEMPLATE_PRICE_DROP'] = 'tpl-price-drop';
    process.env['ZALO_ZNS_TEMPLATE_SUBSCRIPTION_RENEWAL'] = 'tpl-sub-renewal';

    const tpls = getZaloZnsTemplates();

    expect(tpls['inquiry.reply']?.templateId).toBe('tpl-inquiry-reply');
    expect(tpls['listing.price_drop']?.templateId).toBe('tpl-price-drop');
    expect(tpls['subscription.renewal']?.templateId).toBe('tpl-sub-renewal');
  });

  it('maps inquiry.reply params correctly', () => {
    process.env['ZALO_ZNS_TEMPLATE_INQUIRY_REPLY'] = 'tpl-1';
    const tpl = getZaloZnsTemplates()['inquiry.reply']!;

    expect(
      tpl.mapParams({
        agentName: 'Nguyễn Văn A',
        listingTitle: 'Căn hộ Q.1',
        message: 'Chào bạn',
      }),
    ).toEqual({
      agent_name: 'Nguyễn Văn A',
      property_name: 'Căn hộ Q.1',
      message: 'Chào bạn',
    });
  });

  it('maps listing.price_drop params correctly', () => {
    process.env['ZALO_ZNS_TEMPLATE_PRICE_DROP'] = 'tpl-2';
    const tpl = getZaloZnsTemplates()['listing.price_drop']!;

    expect(
      tpl.mapParams({
        listingTitle: 'Nhà phố Q.7',
        oldPriceVND: 5_000_000_000,
        newPriceVND: 4_500_000_000,
      }),
    ).toEqual({
      listing_title: 'Nhà phố Q.7',
      old_price: '5000000000',
      new_price: '4500000000',
    });
  });

  it('maps subscription.renewal params correctly', () => {
    process.env['ZALO_ZNS_TEMPLATE_SUBSCRIPTION_RENEWAL'] = 'tpl-3';
    const tpl = getZaloZnsTemplates()['subscription.renewal']!;

    expect(
      tpl.mapParams({
        planTier: 'PRO',
        renewalDate: '2026-05-01',
        amountVND: 299000,
      }),
    ).toEqual({
      plan_tier: 'PRO',
      renewal_date: '2026-05-01',
      amount: '299000',
    });
  });

  it('falls back to empty strings for missing data keys', () => {
    process.env['ZALO_ZNS_TEMPLATE_PRICE_DROP'] = 'tpl-2';
    const tpl = getZaloZnsTemplates()['listing.price_drop']!;

    expect(tpl.mapParams({})).toEqual({
      listing_title: '',
      old_price: '',
      new_price: '',
    });
  });
});
