import 'package:flutter/material.dart';
import '../hr_section.dart';
import '../widgets/hr_workspace_header.dart';

class HROverviewPage extends StatelessWidget {
  const HROverviewPage({super.key, required this.showEmployeeCvs, required this.onOpen});

  final bool showEmployeeCvs;
  final ValueChanged<HRSection> onOpen;

  @override
  Widget build(BuildContext context) {
    final width = MediaQuery.sizeOf(context).width;
    final sections = <({HRSection section, String title, String description, IconData icon})>[
      (section: HRSection.leave, title: 'Leave Management',
        description: 'Submit, review and track staff leave requests, balances and approvals.',
        icon: Icons.event_available_outlined),
      (section: HRSection.myCv, title: 'My Curriculum Vitae',
        description: 'Build and update your professional CV, photograph and employment record.',
        icon: Icons.badge_outlined),
      if (showEmployeeCvs)
        (section: HRSection.employeeCvs, title: 'Employee CVs',
          description: 'Browse employee profiles and review recorded CV changes.',
          icon: Icons.groups_outlined),
    ];

    return SingleChildScrollView(
      padding: EdgeInsets.all(width < 600 ? 14 : 22),
      child: Center(
        child: ConstrainedBox(
          constraints: const BoxConstraints(maxWidth: 1200),
          child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
            Container(
              padding: EdgeInsets.all(width < 600 ? 17 : 24),
              decoration: const BoxDecoration(color: hrGreen),
              child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                const Text('HUMAN RESOURCES',
                    style: TextStyle(color: Color(0xFFDDEEE4), fontSize: 11, fontWeight: FontWeight.w900, letterSpacing: 1.5)),
                const SizedBox(height: 8),
                Text('Staff services and records',
                    style: TextStyle(color: Colors.white, fontSize: width < 600 ? 22 : 28, fontWeight: FontWeight.w900)),
                const SizedBox(height: 7),
                const Text('Manage leave activity and maintain official staff curriculum vitae records.',
                    style: TextStyle(color: Color(0xFFEAF3EE), fontSize: 13, height: 1.5)),
                const SizedBox(height: 18),
                Container(height: 3, width: 76, color: hrGold),
              ]),
            ),
            const SizedBox(height: 18),
            Text('HR WORKSPACES', style: TextStyle(color: hrDarkGreen, fontSize: 13, fontWeight: FontWeight.w900, letterSpacing: .9)),
            const SizedBox(height: 10),
            LayoutBuilder(builder: (context, constraints) {
              final columns = constraints.maxWidth >= 900 ? 3 : (constraints.maxWidth >= 580 ? 2 : 1);
              final tileWidth = (constraints.maxWidth - (columns - 1) * 12) / columns;
              return Wrap(spacing: 12, runSpacing: 12, children: sections.map((item) => SizedBox(
                width: tileWidth,
                child: _WorkspaceCard(item: item, onTap: () => onOpen(item.section)),
              )).toList());
            }),
          ]),
        ),
      ),
    );
  }
}

class _WorkspaceCard extends StatelessWidget {
  const _WorkspaceCard({required this.item, required this.onTap});
  final ({HRSection section, String title, String description, IconData icon}) item;
  final VoidCallback onTap;

  @override
  Widget build(BuildContext context) => Material(
        color: Colors.white,
        child: InkWell(
          onTap: onTap,
          child: Container(
            constraints: const BoxConstraints(minHeight: 156),
            padding: const EdgeInsets.all(17),
            decoration: BoxDecoration(border: Border.all(color: hrLine)),
            child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
              Row(children: [
                Container(width: 42, height: 42, color: const Color(0xFFEAF3EE),
                    child: Icon(item.icon, color: hrGreen, size: 21)),
                const Spacer(),
                const Icon(Icons.arrow_forward, color: hrGold, size: 19),
              ]),
              const SizedBox(height: 14),
              Text(item.title, style: const TextStyle(color: hrDarkGreen, fontSize: 15, fontWeight: FontWeight.w900)),
              const SizedBox(height: 5),
              Text(item.description, style: const TextStyle(color: hrMuted, fontSize: 12, height: 1.45)),
            ]),
          ),
        ),
      );
}
