import 'package:flutter/material.dart';
import 'package:flutter_lucide/flutter_lucide.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';

import '../shared/widgets/main_app_bar_actions.dart';
import '../theme/theme_extension.dart';
import 'admin_providers.dart';
import 'widgets/admin_section_card.dart';

class AdminLandingScreen extends ConsumerWidget {
  const AdminLandingScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final fs = Theme.of(context).extension<FabledSwordTheme>()!;
    final counts = ref.watch(adminCountsProvider);
    return Scaffold(
      backgroundColor: fs.obsidian,
      appBar: AppBar(
        backgroundColor: fs.obsidian,
        elevation: 0,
        title: Text('Admin', style: TextStyle(color: fs.parchment)),
        actions: const [MainAppBarActions(currentRoute: '/admin')],
      ),
      body: SafeArea(
        child: counts.when(
          loading: () => const Center(child: CircularProgressIndicator()),
          error: (e, _) => Center(
            child: Text('$e', style: TextStyle(color: fs.error)),
          ),
          data: (c) => RefreshIndicator(
            onRefresh: () async => ref.refresh(adminCountsProvider.future),
            child: ListView(
              children: [
                AdminSectionCard(
                  key: const Key('admin_card_requests'),
                  icon: LucideIcons.inbox,
                  title: 'Requests',
                  subtitle: 'Approve or reject Lidarr requests',
                  count: c.requests,
                  onTap: () => context.push('/admin/requests'),
                ),
                AdminSectionCard(
                  key: const Key('admin_card_quarantine'),
                  icon: LucideIcons.triangle_alert,
                  title: 'Quarantine',
                  subtitle: 'Resolve scan failures',
                  count: c.quarantine,
                  onTap: () => context.push('/admin/quarantine'),
                ),
                AdminSectionCard(
                  key: const Key('admin_card_users'),
                  icon: LucideIcons.users,
                  title: 'Users',
                  subtitle: 'Manage accounts and invites',
                  count: c.users,
                  onTap: () => context.push('/admin/users'),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
