import { render, screen } from '@testing-library/react';
import { describe, it, expect } from 'vitest';
import {
  Card,
  CardHeader,
  CardFooter,
  CardTitle,
  CardAction,
  CardDescription,
  CardContent,
} from './card';
import React from 'react';

describe('Card', () => {
  it('renders correctly with all sub-components', () => {
    render(
      <Card>
        <CardHeader>
          <CardTitle>Title</CardTitle>
          <CardDescription>Description</CardDescription>
          <CardAction>Action</CardAction>
        </CardHeader>
        <CardContent>Content</CardContent>
        <CardFooter>Footer</CardFooter>
      </Card>
    );

    expect(screen.getByText('Title')).toBeInTheDocument();
    expect(screen.getByText('Description')).toBeInTheDocument();
    expect(screen.getByText('Action')).toBeInTheDocument();
    expect(screen.getByText('Content')).toBeInTheDocument();
    expect(screen.getByText('Footer')).toBeInTheDocument();
  });

  it('has correct data-slot attributes', () => {
    const { container } = render(
      <Card>
        <CardHeader>
          <CardTitle>Title</CardTitle>
        </CardHeader>
        <CardContent>Content</CardContent>
      </Card>
    );

    expect(container.querySelector('[data-slot="card"]')).toBeInTheDocument();
    expect(container.querySelector('[data-slot="card-header"]')).toBeInTheDocument();
    expect(container.querySelector('[data-slot="card-title"]')).toBeInTheDocument();
    expect(container.querySelector('[data-slot="card-content"]')).toBeInTheDocument();
  });

  it('applies custom classNames', () => {
    const { container } = render(<Card className="custom-card">Content</Card>);
    expect(container.querySelector('[data-slot="card"]')).toHaveClass('custom-card');
  });

  it('defaults to the theme-reactive card background and foreground', () => {
    const { container } = render(<Card>Content</Card>);
    expect(container.querySelector('[data-slot="card"]')).toHaveClass('bg-card', 'text-card-foreground');
  });

  it('pairs variant="white" with a fixed dark text color, not the theme-reactive one', () => {
    const { container } = render(<Card variant="white">Content</Card>);
    const card = container.querySelector('[data-slot="card"]');
    expect(card).toHaveClass('bg-white', 'text-neutral-900');
    expect(card).not.toHaveClass('text-card-foreground');
  });
});
