// src/components/AdminRoute.test.tsx
import React from 'react';
import { render, screen } from '@testing-library/react';
import { describe, it, expect } from 'vitest';
import { MemoryRouter, Routes, Route } from 'react-router-dom';
import { AdminRoute } from './AdminRoute';
import type { Profile } from '../types'; // Path remains the same

const AdminContent = () => <div>Admin Page Content</div>;
const HomePage = () => <div>Home Page</div>;

const renderWithRouter = (profile: Profile | null, initialPath: string) => {
  render(
    <MemoryRouter initialEntries={[initialPath]}>
      <Routes>
        <Route path="/" element={<HomePage />} />
        <Route path="/admin" element={<AdminRoute profile={profile} />}>
          <Route index element={<AdminContent />} />
        </Route>
      </Routes>
    </MemoryRouter>
  );
};

describe('AdminRoute', () => {
  it('should render the admin content when user has admin role', () => {
    const adminProfile: Profile = { user_id: '1', role: 'admin', points: 0 };
    renderWithRouter(adminProfile, '/admin');

    expect(screen.getByText('Admin Page Content')).toBeInTheDocument();
    expect(screen.queryByText('Home Page')).not.toBeInTheDocument();
  });

  it('should redirect to home page when user does not have admin role', () => {
    const userProfile: Profile = { user_id: '2', role: 'user', points: 0 };
    renderWithRouter(userProfile, '/admin');

    // The user is redirected, so we should see the home page content
    expect(screen.getByText('Home Page')).toBeInTheDocument();
    expect(screen.queryByText('Admin Page Content')).not.toBeInTheDocument();
  });

  it('should redirect to home page when profile is null', () => {
    renderWithRouter(null, '/admin');

    // The user is redirected, so we should see the home page content
    expect(screen.getByText('Home Page')).toBeInTheDocument();
    expect(screen.queryByText('Admin Page Content')).not.toBeInTheDocument();
  });
});