import { getAlbumsByGenre } from '../api/subsonicGenres';
import type { SubsonicAlbum } from '../api/subsonicTypes';
import React, { useEffect, useState, useCallback } from 'react';
import { useParams, useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { ArrowLeft, Disc3 } from 'lucide-react';
import { useAuthStore } from '../store/authStore';
import AlbumCard from '../components/AlbumCard';
import { usePerfProbeFlags } from '../utils/perf/perfFlags';
import { VirtualCardGrid } from '../components/VirtualCardGrid';

const PAGE_SIZE = 50;

export default function GenreDetail() {
  const { name } = useParams<{ name: string }>();
  const genre = decodeURIComponent(name ?? '');
  const { t } = useTranslation();
  const perfFlags = usePerfProbeFlags();
  const navigate = useNavigate();
  const [albums, setAlbums] = useState<SubsonicAlbum[]>([]);
  const [loading, setLoading] = useState(true);
  const [loadingMore, setLoadingMore] = useState(false);
  const [hasMore, setHasMore] = useState(true);
  const [offset, setOffset] = useState(0);
  const musicLibraryFilterVersion = useAuthStore(s => s.musicLibraryFilterVersion);

  useEffect(() => {
    setAlbums([]);
    setOffset(0);
    setHasMore(true);
    setLoading(true);
    getAlbumsByGenre(genre, PAGE_SIZE, 0)
      .then(data => {
        setAlbums(data);
        setHasMore(data.length === PAGE_SIZE);
        setOffset(PAGE_SIZE);
      })
      .finally(() => setLoading(false));
  }, [genre]);

  const loadMore = useCallback(() => {
    if (loadingMore || !hasMore) return;
    setLoadingMore(true);
    getAlbumsByGenre(genre, PAGE_SIZE, offset)
      .then(data => {
        setAlbums(prev => [...prev, ...data]);
        setHasMore(data.length === PAGE_SIZE);
        setOffset(prev => prev + PAGE_SIZE);
      })
      .finally(() => setLoadingMore(false));
  }, [genre, offset, loadingMore, hasMore]);

  return (
    <div className="content-body animate-fade-in">
      <button
        className="btn btn-ghost"
        onClick={() => navigate(-1)}
        style={{ marginBottom: '1.5rem', display: 'flex', alignItems: 'center', gap: '0.5rem' }}
      >
        <ArrowLeft size={16} />
        <span>{t('genres.back')}</span>
      </button>

      <div style={{ display: 'flex', alignItems: 'center', gap: '1rem', marginBottom: '1.5rem', flexWrap: 'wrap' }}>
        <h1 className="page-title" style={{ marginBottom: 0 }}>{genre}</h1>
        {!loading && albums.length > 0 && (
          <span style={{ display: 'flex', alignItems: 'center', gap: '0.4rem', color: 'var(--text-secondary)', fontSize: '0.9rem', fontWeight: 500 }}>
            <Disc3 size={14} style={{ color: 'var(--accent)' }} />
            {t('genres.albumCount', { count: albums.length })}{hasMore ? '+' : ''}
          </span>
        )}
      </div>

      {loading && <p className="loading-text">{t('genres.albumsLoading')}</p>}
      {!loading && albums.length === 0 && <p className="loading-text">{t('genres.albumsEmpty')}</p>}

      {albums.length > 0 && (
        <VirtualCardGrid
          items={albums}
          itemKey={(a, _i) => a.id}
          rowVariant="album"
          disableVirtualization={perfFlags.disableMainstageVirtualLists}
          layoutSignal={albums.length}
          renderItem={album => <AlbumCard album={album} />}
        />
      )}

      {hasMore && !loading && (
        <div style={{ display: 'flex', justifyContent: 'center', padding: '2rem 0' }}>
          <button className="btn btn-surface" onClick={loadMore} disabled={loadingMore}>
            {loadingMore ? t('common.loadingMore') : t('genres.loadMore')}
          </button>
        </div>
      )}
    </div>
  );
}
