import 'package:flutter/material.dart';
import '../../../../../core/services/it_support_service.dart';
import '../it_support_widgets.dart';

class ITSupportOverviewPage extends StatelessWidget {
  const ITSupportOverviewPage({super.key, required this.tickets, required this.members,
    required this.notices, required this.status, required this.loading, required this.onTickets,
    required this.onTeam, required this.onInbox});
  final List<SupportJson> tickets, members, notices;
  final String status;
  final bool loading;
  final VoidCallback onTickets, onTeam, onInbox;

  @override
  Widget build(BuildContext context) {
    final screenWidth = MediaQuery.sizeOf(context).width;
    final metricWidth = screenWidth < 390 ? screenWidth - 48 : screenWidth < 600 ? (screenWidth - 46) / 2 : 210.0;
    final active = tickets.where((row) => !['resolved', 'closed'].contains(row['status'])).length;
    final urgent = tickets.where((row) => row['priority'] == 'urgent' && row['status'] != 'closed').length;
    final resolved = tickets.where((row) => ['resolved', 'closed'].contains(row['status'])).length;
    return ListView(padding: const EdgeInsets.fromLTRB(16, 8, 16, 28), children: [
      const Text('IT Service Desk', style: TextStyle(color: supportInk, fontSize: 24, fontWeight: FontWeight.w800)),
      const SizedBox(height: 5),
      const Text('University support activity at a glance.', style: TextStyle(color: Color(0xFF718078), fontSize: 13)),
      const SizedBox(height: 16),
      Wrap(spacing: 10, runSpacing: 10, children: [
        _MetricCard(width: metricWidth, label: 'OPEN TICKETS', value: loading ? '—' : '$active', icon: Icons.confirmation_number_outlined),
        _MetricCard(width: metricWidth, label: 'URGENT', value: loading ? '—' : '$urgent', icon: Icons.priority_high_rounded, highlight: urgent > 0),
        _MetricCard(width: metricWidth, label: 'RESOLVED', value: loading ? '—' : '$resolved', icon: Icons.task_alt_rounded),
        _MetricCard(width: metricWidth, label: 'TEAM MEMBERS', value: loading ? '—' : '${members.length}', icon: Icons.groups_outlined),
      ]),
      const SizedBox(height: 14),
      Container(padding: const EdgeInsets.all(14), color: Colors.white,
        child: Row(children: [const Icon(Icons.offline_pin_outlined, color: supportGreen, size: 19),
          const SizedBox(width: 9), Expanded(child: Text(status, style: const TextStyle(color: Color(0xFF66746C), fontSize: 12)))])),
      const SizedBox(height: 14),
      Wrap(spacing: 9, runSpacing: 9, children: [
        _ActionCard(label: 'Review tickets', icon: Icons.list_alt_rounded, onTap: onTickets),
        _ActionCard(label: 'View IT team', icon: Icons.groups_outlined, onTap: onTeam),
        _ActionCard(label: 'Open inbox', icon: Icons.notifications_none_rounded, onTap: onInbox),
      ]),
      const SizedBox(height: 18),
      const Text('Recent tickets', style: TextStyle(color: supportInk, fontSize: 16, fontWeight: FontWeight.w800)),
      const SizedBox(height: 8),
      if (tickets.isEmpty) Container(color: Colors.white, padding: const EdgeInsets.all(18),
        child: const Text('No cached tickets yet. Connect and refresh to load the latest list.',
          style: TextStyle(color: Color(0xFF718078), fontSize: 13))),
      ...tickets.take(5).map((ticket) => Container(margin: const EdgeInsets.only(bottom: 7), padding: const EdgeInsets.all(12),
        color: Colors.white, child: Row(children: [
          Container(width: 3, height: 37, color: ticket['priority'] == 'urgent' ? const Color(0xFFB73C35) : supportGreen),
          const SizedBox(width: 11), Expanded(child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
            Text('#${ticket['id']}  ${ticket['subject']}', maxLines: 1, overflow: TextOverflow.ellipsis,
              style: const TextStyle(color: supportInk, fontWeight: FontWeight.w700, fontSize: 13)),
            const SizedBox(height: 3), Text('${supportLabel(ticket['status'])} · ${ticket['campus']}',
              maxLines: 1, overflow: TextOverflow.ellipsis, style: const TextStyle(color: Color(0xFF718078), fontSize: 11)),
          ])),
        ]))),
    ]);
  }
}

class _MetricCard extends StatelessWidget {
  const _MetricCard({required this.width, required this.label, required this.value, required this.icon, this.highlight = false});
  final double width;
  final String label, value;
  final IconData icon;
  final bool highlight;
  @override
  Widget build(BuildContext context) => SizedBox(width: width, child: Container(padding: const EdgeInsets.all(12),
    decoration: const BoxDecoration(color: Colors.white, border: Border(bottom: BorderSide(color: supportBorder))),
    child: Row(children: [Container(width: 36, height: 36, color: highlight ? const Color(0xFFFFF0EE) : const Color(0xFFEAF3EE),
      child: Icon(icon, color: highlight ? const Color(0xFFB73C35) : supportGreen, size: 19)), const SizedBox(width: 11),
      Expanded(child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Text(label, maxLines: 1, overflow: TextOverflow.ellipsis, style: const TextStyle(color: Color(0xFF718078), fontSize: 9, fontWeight: FontWeight.w800, letterSpacing: .5)),
        const SizedBox(height: 3), Text(value, style: const TextStyle(color: supportInk, fontSize: 21, fontWeight: FontWeight.w800)),
      ]))])));
}

class _ActionCard extends StatelessWidget {
  const _ActionCard({required this.label, required this.icon, required this.onTap});
  final String label;
  final IconData icon;
  final VoidCallback onTap;
  @override
  Widget build(BuildContext context) => SizedBox(width: 205, child: Material(color: Colors.white, child: InkWell(onTap: onTap,
    child: Padding(padding: const EdgeInsets.all(13), child: Row(children: [Icon(icon, color: supportGreen, size: 19),
      const SizedBox(width: 9), Expanded(child: Text(label, style: const TextStyle(color: supportInk, fontSize: 12, fontWeight: FontWeight.w700))),
      const Icon(Icons.arrow_forward_ios_rounded, color: Color(0xFF87938B), size: 13)])))));
}
