/**
 * Test file for Sale Revenue Report component
 * This file contains basic tests to verify the component functionality
 */

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { mount } from '@vue/test-utils';
import SaleRevenue from './saleRevenue.vue';
import reportService from '@/services/reportService';

// Mock the report service
vi.mock('@/services/reportService', () => ({
  default: {
    getSaleRevenueStatistics: vi.fn()
  }
}));

// Mock vue-echarts
vi.mock('vue-echarts', () => ({
  default: {
    name: 'VChart',
    template: '<div class="mock-chart"></div>'
  }
}));

// Mock vue-datepicker
vi.mock('@vuepic/vue-datepicker', () => ({
  default: {
    name: 'VueDatePicker',
    template: '<div class="mock-datepicker"></div>'
  }
}));

// Mock stores
vi.mock('@/stores', () => ({
  useDatePickerStore: () => ({
    presetRanges: []
  })
}));

// Mock toast
vi.mock('vue-toastification', () => ({
  useToast: () => ({
    error: vi.fn(),
    info: vi.fn(),
    success: vi.fn()
  })
}));

describe('SaleRevenue Component', () => {
  let wrapper;
  
  const mockSaleRevenueData = {
    Result: 1,
    Message: '',
    Items: [
      {
        SaleId: 123,
        SaleName: 'Nguyễn Văn A',
        SalePhoneNumber: '0901234567',
        TotalOrders: 150,
        TotalRevenue: 50000000,
        TotalCollectAmount: 45000000,
        WaitPickup: 5,
        WaitPickupRevenue: 2000000,
        WaitPickupCollectAmount: 1800000,
        Picking: 3,
        PickingRevenue: 1200000,
        PickingCollectAmount: 1100000,
        Transporting: 8,
        TransportingRevenue: 3500000,
        TransportingCollectAmount: 3200000,
        Delivering: 4,
        DeliveringRevenue: 1800000,
        DeliveringCollectAmount: 1600000,
        DeliverySuccess: 120,
        DeliverySuccessRevenue: 38000000,
        DeliverySuccessCollectAmount: 35000000,
        PickupFailed: 2,
        PickupFailedRevenue: 800000,
        PickupFailedCollectAmount: 700000,
        DeliveryFailed: 3,
        DeliveryFailedRevenue: 1200000,
        DeliveryFailedCollectAmount: 1000000,
        Cancelled: 5,
        CancelledRevenue: 1500000,
        CancelledCollectAmount: 1300000,
        SuccessRate: 80.0,
        TotalInProgress: 20,
        TotalFailed: 10,
        TotalCompleted: 120,
        IsTotalRow: false
      }
    ],
    SaleRevenueTotalItem: {
      SaleId: 0,
      SaleName: 'TỔNG CỘNG',
      SalePhoneNumber: '',
      TotalOrders: 500,
      TotalRevenue: 200000000,
      TotalCollectAmount: 180000000,
      DeliverySuccess: 400,
      DeliverySuccessRevenue: 160000000,
      DeliverySuccessCollectAmount: 150000000,
      TotalInProgress: 50,
      TotalFailed: 50,
      WaitPickup: 10,
      Picking: 15,
      Transporting: 20,
      Delivering: 5,
      PickupFailed: 20,
      DeliveryFailed: 15,
      Cancelled: 15,
      IsTotalRow: true
    },
    TotalRecord: 25
  };

  beforeEach(() => {
    vi.clearAllMocks();
    reportService.getSaleRevenueStatistics.mockResolvedValue(mockSaleRevenueData);
  });

  it('should render the component correctly', () => {
    wrapper = mount(SaleRevenue, {
      global: {
        stubs: {
          VChart: true,
          VueDatePicker: true
        }
      }
    });

    expect(wrapper.find('h2').text()).toBe('Báo cáo doanh số theo Sale');
    expect(wrapper.find('.breadcrumb-item.active').text()).toBe('Doanh số theo Sale');
  });

  it('should have correct search form fields', () => {
    wrapper = mount(SaleRevenue, {
      global: {
        stubs: {
          VChart: true,
          VueDatePicker: true
        }
      }
    });

    // Check if search form exists
    expect(wrapper.find('input[type="number"]').exists()).toBe(true); // Sale ID input
    expect(wrapper.find('input[type="text"]').exists()).toBe(true); // Sale IDs input
  });

  it('should format currency correctly', async () => {
    wrapper = mount(SaleRevenue, {
      global: {
        stubs: {
          VChart: true,
          VueDatePicker: true
        }
      }
    });

    const vm = wrapper.vm;
    
    expect(vm.formatCurrency(50000000)).toBe('50.000.000 ₫');
    expect(vm.formatCurrency(0)).toBe('0 ₫');
    expect(vm.formatCurrency(null)).toBe('0 ₫');
  });

  it('should format numbers correctly', async () => {
    wrapper = mount(SaleRevenue, {
      global: {
        stubs: {
          VChart: true,
          VueDatePicker: true
        }
      }
    });

    const vm = wrapper.vm;
    
    expect(vm.formatNumber(150)).toBe('150');
    expect(vm.formatNumber(1500)).toBe('1.500');
    expect(vm.formatNumber(null)).toBe('0');
  });

  it('should format percentage correctly', async () => {
    wrapper = mount(SaleRevenue, {
      global: {
        stubs: {
          VChart: true,
          VueDatePicker: true
        }
      }
    });

    const vm = wrapper.vm;
    
    expect(vm.formatPercent(80.5)).toBe('80.50');
    expect(vm.formatPercent(100)).toBe('100.00');
    expect(vm.formatPercent(null)).toBe('0.00');
  });
});
