import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import SessionPaymentAccordion, { type SessionPaymentData } from './SessionPaymentAccordion';

const paid: SessionPaymentData = {
  uuid: 's1', services: [{ service_name: 'روکش' }], final_price_rials: 2500000,
  doctor_name: 'دکتر فتحی', payment_method: 'cash', is_paid: true,
  created_at: 1700000000, updated_at: 1700003600,
};
const unpaid: SessionPaymentData = {
  uuid: 's2', services: [{ service_name: 'اسکیلینگ' }], final_price_rials: 1800000,
  doctor_name: 'دکتر راد', payment_method: 'pending', is_paid: false, created_at: 1700000000,
};

describe('SessionPaymentAccordion', () => {
  it('shows the settled badge + settlement line (method/personnel) when expanded', () => {
    render(<SessionPaymentAccordion session={paid} expanded onToggle={() => {}} />);
    expect(screen.getByText('روکش')).toBeInTheDocument();
    expect(screen.getByText('پرداخت شده')).toBeInTheDocument();
    expect(screen.getByText('نحوه پرداخت:')).toBeInTheDocument();
    expect(screen.getByText('نقدی')).toBeInTheDocument();          // cash → نقدی
    expect(screen.getByText('دکتر فتحی')).toBeInTheDocument();     // personnel = doctor
  });

  it('shows the unsettled badge + empty message for an unpaid session', () => {
    render(<SessionPaymentAccordion session={unpaid} expanded onToggle={() => {}} />);
    expect(screen.getByText('تسویه نشده')).toBeInTheDocument();
    expect(screen.getByText('هیچ پرداختی ثبت نشده است.')).toBeInTheDocument();
  });

  it('hides the details when collapsed', () => {
    render(<SessionPaymentAccordion session={paid} expanded={false} onToggle={() => {}} />);
    expect(screen.getByText('پرداخت شده')).toBeInTheDocument();     // header still visible
    expect(screen.queryByText('نحوه پرداخت:')).not.toBeInTheDocument();
  });

  it('calls onToggle when the summary is clicked', () => {
    const onToggle = vi.fn();
    render(<SessionPaymentAccordion session={paid} expanded={false} onToggle={onToggle} />);
    screen.getByRole('button').click();
    expect(onToggle).toHaveBeenCalledOnce();
  });
});
