/**
 * HomeView Component
 * Client-side home page view with search and filtering
 */

'use client';

import { useSearchParams } from 'next/navigation';
import { FilterBar } from './FilterBar';
import { SchoolCard } from './SchoolCard';
import { Pagination } from './Pagination';
import { EmptyState } from './EmptyState';
import { useComparisonContext } from '@/context/ComparisonContext';
import type { SchoolsResponse, Filters } from '@/lib/types';
import styles from './HomeView.module.css';

interface HomeViewProps {
  initialSchools: SchoolsResponse;
  filters: Filters;
}

export function HomeView({ initialSchools, filters }: HomeViewProps) {
  const searchParams = useSearchParams();
  const { addSchool } = useComparisonContext();

  const hasSearch = searchParams.get('search') || searchParams.get('postcode');
  const isLocationSearch = !!searchParams.get('postcode');

  return (
    <div className={styles.homeView}>
      {/* Combined Hero + Search and Filters */}
      <FilterBar
        filters={filters}
        showLocationSearch
        heroTitle="Compare Primary School Performance"
        heroDescription="Search and compare KS2 results for thousands of schools across England"
      />

      {/* Location Info Banner */}
      {isLocationSearch && initialSchools.location_info && (
        <div className={styles.locationBanner}>
          <span>
            Showing schools within {(initialSchools.location_info.radius / 1.60934).toFixed(1)} miles of{' '}
            <strong>{initialSchools.location_info.postcode}</strong>
          </span>
        </div>
      )}

      {/* Results Section */}
      <section className={styles.results}>
        {!hasSearch && initialSchools.schools.length > 0 && (
          <div className={styles.sectionHeader}>
            <h2>Featured Schools</h2>
            <p className={styles.sectionDescription}>
              Explore schools from across England
            </p>
          </div>
        )}

        {hasSearch && (
          <div className={styles.resultsHeader}>
            <h2>
              {initialSchools.total.toLocaleString()} school
              {initialSchools.total !== 1 ? 's' : ''} found
            </h2>
          </div>
        )}

        {initialSchools.schools.length === 0 ? (
          <EmptyState
            title={hasSearch ? "No schools found" : "Search for Schools"}
            message={
              hasSearch
                ? "Try adjusting your search criteria or filters to find schools."
                : "Use the search above to find schools by name or search by location to discover schools near a postcode."
            }
            action={
              hasSearch
                ? {
                    label: 'Clear Filters',
                    onClick: () => {
                      window.location.href = '/';
                    },
                  }
                : undefined
            }
          />
        ) : (
          <>
            <div className={styles.grid}>
              {initialSchools.schools.map((school) => (
                <SchoolCard
                  key={school.urn}
                  school={school}
                  onAddToCompare={addSchool}
                />
              ))}
            </div>

            {initialSchools.total_pages > 1 && (
              <Pagination
                currentPage={initialSchools.page}
                totalPages={initialSchools.total_pages}
                total={initialSchools.total}
              />
            )}
          </>
        )}
      </section>
    </div>
  );
}
