import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';

import '../dashboard/application/dashboard_controller.dart';
import '../dashboard/domain/dashboard_summary.dart';

/// A mobile-optimized dashboard page showing aggregated summary data.
///
/// Uses a single-column scrollable layout suitable for smaller screens.
class MobileDashboardPage extends StatefulWidget {
  final DashboardController controller;

  const MobileDashboardPage({super.key, required this.controller});

  @override
  State<MobileDashboardPage> createState() => _MobileDashboardPageState();
}

class _MobileDashboardPageState extends State<MobileDashboardPage> {
  @override
  void initState() {
    super.initState();
    widget.controller.addListener(_onControllerChanged);
    widget.controller.load();
  }

  @override
  void didUpdateWidget(MobileDashboardPage oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.controller != widget.controller) {
      oldWidget.controller.removeListener(_onControllerChanged);
      widget.controller.addListener(_onControllerChanged);
      widget.controller.load();
    }
  }

  @override
  void dispose() {
    widget.controller.removeListener(_onControllerChanged);
    super.dispose();
  }

  void _onControllerChanged() => setState(() {});

  @override
  Widget build(BuildContext context) {
    final controller = widget.controller;

    if (controller.isLoading) {
      return const Center(child: CircularProgressIndicator());
    }

    if (controller.error != null) {
      return Center(
        child: Text('Failed to load dashboard data.', style: Theme.of(context).textTheme.bodyLarge),
      );
    }

    final summary = controller.summary;

    return ListView(
      padding: const EdgeInsets.all(KcSpacing.md),
      children: [
        const KcSectionHeader(title: 'Overview'),
        const SizedBox(height: KcSpacing.sm),
        _buildSummaryGrid(context, summary),
        const SizedBox(height: KcSpacing.xl),
        const KcSectionHeader(title: 'Recent Activity'),
        const SizedBox(height: KcSpacing.sm),
        const KcEmptyState(
          icon: Icons.history,
          message:
              'No recent activity yet.\nActivity will appear here once orders and updates are tracked.',
        ),
      ],
    );
  }

  Widget _buildSummaryGrid(BuildContext context, DashboardSummary summary) {
    final cards = [
      KcSummaryCard(
        icon: Icons.inventory_2,
        iconColor: KcColors.denimBlue,
        label: 'Total Products',
        value: '${summary.totalProducts}',
      ),
      KcSummaryCard(
        icon: Icons.check_circle_outline,
        iconColor: KcColors.success,
        label: 'In Stock',
        value: '${summary.inStock}',
      ),
      KcSummaryCard(
        icon: Icons.warning_amber_rounded,
        iconColor: KcColors.warning,
        label: 'Low Stock',
        value: '${summary.lowStock}',
      ),
      KcSummaryCard(
        icon: Icons.edit_note,
        iconColor: KcColors.neutral,
        label: 'Draft',
        value: '${summary.draftProducts}',
      ),
      KcSummaryCard(
        icon: Icons.receipt_long,
        iconColor: KcColors.denimBlue,
        label: 'Total Orders',
        value: '${summary.totalOrders}',
      ),
      KcSummaryCard(
        icon: Icons.hourglass_empty,
        iconColor: KcColors.warning,
        label: 'Pending Orders',
        value: '${summary.pendingOrders}',
      ),
      KcSummaryCard(
        icon: Icons.local_shipping_outlined,
        iconColor: KcColors.success,
        label: 'Active Orders',
        value: '${summary.activeOrders}',
      ),
      KcSummaryCard(
        icon: Icons.attach_money,
        iconColor: KcColors.success,
        label: 'Revenue',
        value: '\$${summary.deliveredRevenue.toStringAsFixed(2)}',
      ),
    ];

    return GridView.count(
      crossAxisCount: 2,
      crossAxisSpacing: KcSpacing.sm,
      mainAxisSpacing: KcSpacing.sm,
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      childAspectRatio: 1.6,
      children: cards,
    );
  }
}
